-
Notifications
You must be signed in to change notification settings - Fork 585
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cesarvr/mixed implementation #3501
Conversation
…ough a need of polish is required.
… the node_value_return.hpp class
Just a comment to the motivation. It's not (originally) intended to defer type decisions but to allow storing mixed values when there is a need for that. |
Thanks for the comment @bmunkholm, but I think I used the word inference, instead of defer, meaning that the framework will figure out the type for you. And of course the side effect of that is that it will store everything you trow at it as long as we support the type. |
It doesn't have any effect to the feature itself (that's all good), I'm merely pointing out that above is not really the motivation for this feature or how we will promote the use of it. It shouldn't make you "lazy" or defer decisions about your data types you want. |
src/common/type_deduction.hpp
Outdated
@@ -0,0 +1,37 @@ | |||
//////////////////////////////////////////////////////////////////////////// | |||
// | |||
// Copyright 2016 Realm Inc. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] *2021 ;)
src/js_mixed.hpp
Outdated
|
||
#pragma once | ||
|
||
#include <common/type_deduction.hpp> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[question] Would you not need the #if REALM_PLATFORM_NODE
guard here as well?
src/js_mixed.hpp
Outdated
}; | ||
|
||
template <typename Context, typename Value, typename Utils> | ||
class HandleString : public MixedWrapper<Context, Value> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[code style] I think these classes would be better named Mixed* (e.g., MixedString) rather than Handle*. Just to make it clear that they deal with Mixed values only.
Alternatively, wrapping them in a mixed
namespace or, even better, nested as classes within `MixedWrapper.
Another note on the description of the PR: you can't use those two schemas interchangeably because:
|
src/js_mixed.hpp
Outdated
template <typename Context, typename Value, typename Utils> | ||
class HandleBinary : public MixedWrapper<Context, Value> { | ||
Mixed wrap(Context context, Value const &value) { | ||
auto owned_binary_data = Utils::to_binary(context, value); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BinaryData
and StringData
are very similar in Core - they don't hold a reference to their contents. So it's surprising that you need the cache for the string case, but not in the binary case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, thanks for the heads up 👍
Mixed wrap(Context context, Value const &value) { | ||
auto date = Utils::to_date(context, value); | ||
|
||
double milliseconds = Utils::to_number(context, date); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[q] Isn't it better to std::move
the date
(and the ts
at the end) variables here?
src/js_mixed.hpp
Outdated
if (strategy == nullptr) { | ||
throw std::runtime_error( | ||
"The " + TypeDeduction::realm_typeof(mixed.get_type()) + | ||
" value is not supported for the mixed type."); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this is hit, it would be a bug in the SDK, wouldn't it? There should be no type in Mixed
that can't be wrapped to Value
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good observation, but even in that unlikely case it would be nice to fail gracefully.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we did want to fail gracefully, I would suggest rewording the error message to something that makes it clear to the developer that this is a bug on our end and they should file a ticket.
src/node/node_type_deduction.hpp
Outdated
if (value.IsUndefined()) { | ||
return type_TypedLink; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is undefined returned as typedlink?
src/node/node_type_deduction.hpp
Outdated
if (value.IsObject()) { | ||
return type_Link; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be TypedLink
?
src/node/node_type_deduction.hpp
Outdated
return type_Link; | ||
} | ||
|
||
return type_Link; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like a weird default - perhaps hitting this should be an error or return type_Invalid
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few nitpicks about the explicitness of the Mixed handler types, and a possible memory leak we need to look at.
Also, some tests are needed.
Otherwise, good work.
Co-authored-by: Nikola Irinchev <[email protected]>
…-js into cesarvr/mixed_implementation
What, How & Why?
Mixed Type
It provide integration between the realm::Mixed type into our Realm JS SDK.
Usage
As mentioned before the Mixed type basically allows the user to delegate the type inference at runtime to Realm-JS, so instead of defining the types in the schema like this:
Which require the user to remember the types, with the new Mixed type this is not longer necessary because this type deduct the type automatically, you can use the below schema interchangeably with the one above:
This in combination with Dictionaries can facilitate a more agile way of development where the users are not constrained by initial schema rules or mistakes.
Performance
To make sure we don’t degrade the user experience, we tested the performance by creating two tables with 2 Million rows using a wide variety of types, then we use the fixed schema as the reference for the performance.
Profiling
To understand this slowdown we can take a look at the profiling information:
We expend the majority of CPU time is spend deducting types and parsing complex types as we can see in the figure below:
Another explanation for the performance degradation vs the fixed schema option is the time spent by the CPU dealing with the Mixed type in Realm Core:
Those are the points where we need to focus for further speed improvements.
This closes # RJS-544
☑️ ToDos
Compatibility
label is updated or copied from previous entryBreaking
label has been applied or is not necessaryIf this PR adds or changes public API's: