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 all 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
45 changes: 30 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,42 @@ 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;

static _SingletonMaker<T> _getSingletonMaker<T extends GeneratedMessage>(
T Function() fun) {
final oldMaker = _defaultMakers[fun];
if (oldMaker != null) {
// The CFE will insert an implicit downcast to `_SingletonMaker<T>`. We
// avoid making that explicit because implicit downcasts are avoided by
// dart2js in production code.
return oldMaker as dynamic;
}

return defaultMaker;
return _defaultMakers[fun] = _SingletonMaker<T>(fun);
}
}

/// For generated code only.
static T $_defaultFor<T extends GeneratedMessage>(T Function() createFn) =>
_defaultMakerFor<T>(createFn)();
// We use a class that creates singletones instead of a closure function. We do
// so because the result of the lookup in [_defaultMakers] has to be downcasted.
// A downcast to a generic interface type is much easier to perform at runtime
// than a downcast to a generic function type.
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