dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.07k stars 1.56k forks source link

consider detecting and eagerly initializing static fields which contain small data objects #41291

Open mraleph opened 4 years ago

mraleph commented 4 years ago

For example in our core libraries:

static final List<int> _inverseAlphabet = Int8List.fromList([
    __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, //
    __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, //
    __, __, __, __, __, _p, __, __, __, __, __, 62, __, 62, __, 63, //
    52, 53, 54, 55, 56, 57, 58, 59, 60, 61, __, __, __, _p, __, __, //
    __, 00, 01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12, 13, 14, //
    15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, __, __, __, __, 63, //
    __, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, //
    41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, __, __, __, __, __, //
  ]);

We could theoretically include Int8List into the snapshot instead of including code to build it - which would be much larger.

We could probably start by collecting some statistics of how initializers in typical Flutter applications look like.

This might have additional benefit of improving performance by removing InitStaticField in AOT.

mraleph commented 4 years ago

/cc @mkustermann

rakudrama commented 4 years ago

FWIW dart2js does a limited amount of eager initialization, with syntactic recognition of effect-free initializers (it won't detect this example). https://dart-review.googlesource.com/c/sdk/+/94749

mraleph commented 4 years ago

@rakudrama we also have some limited support (essentially limited to literal initializers right now) - but it would be good to expand it.