Sdk: Tree-shaking and inlining

Created on 25 Feb 2017  路  3Comments  路  Source: dart-lang/sdk

Compile this code with dart2js and --trust-type-annotations

import 'dart:html';

void main() {
  HtmlElement element = new DivElement();
  element.classes.add('my-class-name');
}

Main function is compiled to:

var t1 = document;
t1 = t1.createElement("div");
t1.classList.add("my-class-name");

However compiled file size in around 100 kb and contains code for _ElementCssClassSet. However, all used code of this class is inlined. It seems that tree-shaking and code exclusion doesn't work well with inlining/@JS() annotations. Ideally compiler should be able to eliminate code for all dart classes in this example. Resolving this issue might result in huge improvements in compiled dart to js file sizes.

area-web web-dart2js

Most helpful comment

@rakudrama Small example is just to demonstrate that tree-shaking can do much better. In my opinion algorithm for finding unused methods/classes should be the same for small apps and large apps. If something is not used in compiled code it should not be there.

All 3 comments

@rakudrama Please have a look.

This is what I see.
I see this at the latest revision, and something similar at 1.22.

$ cat bug23016.dart
import 'dart:html';

void main() {
  HtmlElement element = new DivElement();
  element.classes.add('my-class-name');
}
$ sdk/bin/dart2js --trust-type-annotations bug23016.dart --out=o1.js
Dart file (bug23016.dart) compiled to JavaScript: o1.js
$ grep ElementCss o1.js
$ grep -i Css o1.js
$ wc -c o1.js
76140 o1.js
$ 

75k is annoying.
Most of that is the same for all apps, and is actually needed when the app uses tear-offs, catches exceptions, creates an object, does interpolation, etc.
We could do better here but we have been spending our effort on 50 page angular apps rather than 5 small examples.

@rakudrama Small example is just to demonstrate that tree-shaking can do much better. In my opinion algorithm for finding unused methods/classes should be the same for small apps and large apps. If something is not used in compiled code it should not be there.

Was this page helpful?
0 / 5 - 0 ratings