google / google/built_collection.dart

Avoid dynamic calls, especially dynamic calls to collection getters/methods

Open
#308 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Dart
Stars
285
Forks
54
Avg merge
23m
Merged PRs (30d)
1

Description

To illustrate the point, let's look at code from [BuiltListMultiMap](https://github.com/google/built_collection.dart/blob/9d0a6e560594c9e950280a162a6a0d24fae44b95/lib/src/list_multimap/built_list_multimap.dart#L30):
```dart
factory BuiltListMultimap([multimap = const {}]) {
if (multimap is _BuiltListMultimap &&
multimap.hasExactKeyAndValueTypes(K, V)) {
return multimap as BuiltListMultimap;
} else if (multimap is Map) {
return _BuiltListMultimap.copy(multimap.keys, (k) => multimap[k]);
} else if (multimap is BuiltListMultimap) {
return _BuiltListMultimap.copy(multimap.keys, (k) => multimap[k]);
} else {
return _BuiltListMultimap.copy(multimap.keys, (k) => multimap[k]);
}
}
```

Here the parameter is typed `dynamic multimap = const {}`. As a result, the `multimap.keys` in the last `else {}` block is a dynamic `get:keys` call.

Now some applications may make classes implement the `Map` interface (e.g. proto classes with map mixins is common in g3).

=> This can now lead to thousands of small dynamic getter functions in production builds of an app.
=> Typing the parameter as `Map multimap` would avoid this
=> Similar dynamic calls are also in other places in the `package:built_collection`

/cc @davidmorgan

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.