-
Notifications
You must be signed in to change notification settings - Fork 182
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
Changes from 2 commits
39c3249
19a4a20
94e6b91
62780fd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -506,27 +506,35 @@ abstract class GeneratedMessage { | |
|
||
// Support for generating a read-only default singleton instance. | ||
|
||
static final Map<Function?, Function> _defaultMakers = {}; | ||
static final Map<Function?, _SingletonMaker<GeneratedMessage>> | ||
_defaultMakers = {}; | ||
|
||
static T Function() _defaultMakerFor<T extends GeneratedMessage>( | ||
T Function()? createFn) { | ||
return (_defaultMakers[createFn] ??= _createDefaultMakerFor<T>(createFn!)) | ||
as T Function(); | ||
} | ||
T Function()? createFn) => | ||
_getSingletonMaker(createFn!)._frozenSingletonCreator; | ||
|
||
static T Function() _createDefaultMakerFor<T extends GeneratedMessage>( | ||
T Function() createFn) { | ||
T? defaultValue; | ||
T defaultMaker() { | ||
return defaultValue ??= createFn()..freeze(); | ||
} | ||
/// For generated code only. | ||
static T $_defaultFor<T extends GeneratedMessage>(T Function() createFn) => | ||
_getSingletonMaker(createFn)._frozenSingleton; | ||
|
||
return defaultMaker; | ||
static _SingletonMaker<T> _getSingletonMaker<T extends GeneratedMessage>( | ||
T Function() fun) { | ||
final oldMaker = _defaultMakers[fun]; | ||
if (oldMaker != null) { | ||
return oldMaker as _SingletonMaker<T>; | ||
} | ||
return _defaultMakers[fun] = _SingletonMaker<T>(fun); | ||
} | ||
} | ||
|
||
/// 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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
final T Function() _creator; | ||
|
||
_SingletonMaker(this._creator); | ||
|
||
late final T _frozenSingleton = _creator()..freeze(); | ||
// ignore: prefer_function_declarations_over_variables | ||
late final T Function() _frozenSingletonCreator = () => _frozenSingleton; | ||
} | ||
|
||
/// The package name of a protobuf message. | ||
|
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:
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.