Skip to content

Make it easy/efficient to create immutable collections via literals #117

Open
@kevmoo

Description

@kevmoo

See

https://github.com/dart-lang/sdk/blob/031e77eea11a16a5e486a3673549adbd68862c8e/sdk/lib/core/uri.dart#L2233-L2237

      // ASCII, a single percent encoded sequence.
      codeUnits = new List(3);
      codeUnits[0] = _PERCENT;
      codeUnits[1] = _hexDigits.codeUnitAt(char >> 4);
      codeUnits[2] = _hexDigits.codeUnitAt(char & 0xf);

Also in the SDK,
2x in this file https://github.com/dart-lang/sdk/blob/031e77eea11a16a5e486a3673549adbd68862c8e/sdk/lib/vmservice/message.dart#L176-L182

4x in this file https://github.com/dart-lang/sdk/blob/031e77eea11a16a5e486a3673549adbd68862c8e/runtime/bin/vmservice/loader.dart#L1107-L1112

This entire file in pkg:isolate - https://github.com/dart-lang/isolate/blob/master/lib/src/util.dart

In the Dart sources in Google (some of these duplicate the above)

  • List.unmodifiable\(\[ - 135 hits
  • List\<.+\>\.unmodifiable\(\[ - 96 times
  • Map\<.+,.+>\.unmodifiable\(\{ - 25 times
  • Map\.unmodifiable\(\{ - 28 times
  • UnmodifiableSetView from pkg:collection is used dozens of times, not as a view over mutable data, but as a immutable set.

Benefits:

  • Less copying!
  • Immutable collections are more efficient
    • iteration doesn't need to track version, etc
    • no need to have indirection to a fixed-sized backing store to support growing
  • Enables special-casing implementations - e.g. immutable([1,2,3]) could be created as Uint8List.

Metadata

Metadata

Assignees

No one assigned

    Labels

    requestRequests to resolve a particular developer problem

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions