Skip to content

Make Map.of more efficient, and make Map.from delegate to Map.of where possible #47856

@dnfield

Description

@dnfield

Today, [Hash]Map.of requires recomputing the buckets: https://github.com/dart-lang/sdk/blob/main/sdk/lib/collection/hash_map.dart#L210

It should be able to specialize this method so that the private implementations in the SDK of the Map types do something more efficient, like copy buckets.

It should also be possible to specialize [Hash]Map.from similar to List.from so that it delegates to [Hash]Map.of when a cast isn't actually necessary (because the other is HashMap<K, V> in a HashMap.from for example).

Flutter runs into this problem when it tries to pass along copies of hash maps to child elements, which can happen frequently when lots of inherited widgets are involved - for example, in a large internal app it can be observed as being called 127 times, taking ~36ms of a 293ms build.

/cc @a-siva @goderbauer

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-core-librarySDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries.customer-flutterlibrary-collectiontype-enhancementA request for a change that isn't a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions