Use a map to cache the valueOf functions for enums
#1047
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
(This is cl/658131451 ported to open source, not my CL)
CL description: (slightly edited)
This is a generated-code size optimization.
The main change is to generate the valueOf functions for protobuf enums programatically, rather than (1) use the per-enum
FooPbEnum.valueOfstatic function tear-foo which (2) uses a per-enum static variable that is lazily initialized to map from int value to the protobuf enum value.With ~1000 protobuf enums, this allows most of the
valueOfstatic function tear-off closures to tree-shaken, together with their functions and the code for the initializer of the map.The secondary change is to use more specialized 'add' methods to
BuilderInfo, allowing many fields to be 'defined' with a call with fewer arguments.Together, these changes reduce the size of the main unit of an app by about 1.3%, and would have a similar effect on native targets.
This change requires dart-lang/sdk@175dc05, which was released with Dart 3.6.0.