Skip to content

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

@mkustermann

Description

@mkustermann

To illustrate the point, let's look at code from BuiltListMultiMap:

  factory BuiltListMultimap([multimap = const {}]) {
    if (multimap is _BuiltListMultimap &&
        multimap.hasExactKeyAndValueTypes(K, V)) {
      return multimap as BuiltListMultimap<K, V>;
    } else if (multimap is Map) {
      return _BuiltListMultimap<K, V>.copy(multimap.keys, (k) => multimap[k]);
    } else if (multimap is BuiltListMultimap) {
      return _BuiltListMultimap<K, V>.copy(multimap.keys, (k) => multimap[k]);
    } else {
      return _BuiltListMultimap<K, V>.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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions