-
Notifications
You must be signed in to change notification settings - Fork 185
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
Avoid runtime function type check in lazily created singleton creator functions #574
Avoid runtime function type check in lazily created singleton creator functions #574
Conversation
/cc @mraleph wdyt? |
@mraleph Who would be good to review this? |
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.
DBC
T Function() fun) { | ||
final oldMaker = _defaultMakers[fun]; | ||
if (oldMaker != null) { | ||
return oldMaker as _SingletonMaker<T>; |
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.
To avoid check in dart2js production code, use an implicit downcast via:
return oldMaker as dynamic;
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.
Thanks for the suggestion. Done.
/// For generated code only. | ||
static T $_defaultFor<T extends GeneratedMessage>(T Function() createFn) => | ||
_defaultMakerFor<T>(createFn)(); | ||
class _SingletonMaker<T extends GeneratedMessage> { |
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.
You should probably add a comment here explaining why we go this route (e.g. explain that we want to avoid any type checks against a function type and hence you an object container for functions).
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.
Done.
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.
LGTM
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.
Thanks for the review!
/// For generated code only. | ||
static T $_defaultFor<T extends GeneratedMessage>(T Function() createFn) => | ||
_defaultMakerFor<T>(createFn)(); | ||
class _SingletonMaker<T extends GeneratedMessage> { |
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.
Done.
In order to avoid a runtime function type check (which is complex to perform and is implemented inefficiently in the Dart VM atm) we restructure the code that creates frozen singleton creator functions.
Issue #573