Skip to content
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

Merged

Conversation

mkustermann
Copy link
Collaborator

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

@mkustermann
Copy link
Collaborator Author

/cc @mraleph wdyt?

@mkustermann
Copy link
Collaborator Author

@mraleph Who would be good to review this?

Copy link
Contributor

@rakudrama rakudrama left a 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>;
Copy link
Contributor

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;

Copy link
Collaborator Author

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> {
Copy link
Member

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).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Copy link
Member

@mraleph mraleph left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Collaborator Author

@mkustermann mkustermann left a 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> {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@mraleph mraleph merged commit 782fd24 into google:master Feb 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants