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
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 23 additions & 15 deletions protobuf/lib/src/protobuf/generated_message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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>;
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

@mkustermann mkustermann Feb 7, 2022

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.

}
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> {
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.

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.
Expand Down