Repository navigation
Conversation
aadf21e to
6bf76cc
Compare
6bf76cc to
462e44f
Compare
inlineConstants acorn pass to inline primitive JS constants
|
Let me know if you would like me to land the pass first, before making the widespread usage withing the library files? |
c652d23 to
46993d7
Compare
|
What is the codesize cost you are concerned about here - the |
There are two consts:
With this inlining pass we don't pay either of these costs! |
|
I see, thanks. Can we get both of those benefits by just renaming |
Maybe, that seems like a rather odd way to achieve the goal though. What is it about the inlining approach the you don't like? |
46993d7 to
b5f892a
Compare
|
Just the complexity. It has to consider scoping, and it is around 100 lines. I think const=>var would be almost a one-liner? |
|
One other motivation I should mention is that this is needed for another change I have in the pipeline: cd9fbb5 |
|
More generally, these passes are not simple to read or write, so fewer and simpler ones seems worthwhile to me. |
|
The other reason we cannot just do An alternative is that we could run a full terser minifier in these most? I think terser will do constant inlining by default. |
Doesn't const=>var fix that? Or do you mean we'd need to also fuse together the original var and the former-const-but-now-var? edit: oh, I see, you mean adding more for code clarity, so we'd really need to fuse them all to save size
Yeah, that sort of makes sense to me. I'd say that Acorn passes are necessary when we are doing something that depends on Emscripten semantics (like safe-heap rewrite or export parsing/removal in metadce). But it feels a little wrong to start to replicate standard compiler optimizations in Terser and Closure? I'm not totally opposed to trivial ones, though. Especially if Terser is slow (but I didn't check if it is) |
But I think we really want to fully inline these, not just fuse them.. so the cost of adding a new
Ok, maybe I'll look into integrating terser for |
This allows is to track the size of our builds for users who are not using closure compiler.Add a codesize test that does not use closure compiler Split out from #27871
|
I agree that running Terser makes sense. Seems better than working on our own JS opts. My only concern is if it makes compile times a lot slower (but hopefully not, or hopefully there is a flag to control how much it does?) |
I imagine it would only be fore -O3/-Oz/-Os/etc users so they already have a slow link time IIUC. And yes I imagine you could disable it completely with |
Instead of a dedicated `stripDefaultUndefined` Acorn AST pass (added in
compression (`{defaults: false, evaluate: true, keep_fargs: false}`)
when minifying JS without Closure Compiler. In addition to stripping
`= undefined` default parameter values, this performs lightweight Terser
cleanups (`undefined` -> `void 0`, single-statement block brace
removal, and constant folding).
In general, this prevents us from needing to replicate compression
passes that already exist in upstream Terser (see emscripten-core#27871).
Timing highlights (`test/hello_world.c` with `-sINCLUDE_FULL_LIBRARY`,
~252 KB minified JS):
- `minify_sync` with `compress: false`: 105.5 ms
- `minify_sync` with `compress: {defaults: false, evaluate: true,
keep_fargs: false}`: 188.3 ms (+82.8 ms, or ~+63 ms net after
removing the `stripDefaultUndefined` AST walk), saving ~1.4 KB.
- On a standard `-O2` `hello_world.c` build (~8.5 KB JS), `minify_sync`
goes from 3.2 ms to 5.5 ms (+2.3 ms).
This change, in general, should prevent us from being tempted to
replicate compression passes that already exist in upstream terser
(See emscripten-core#27871, for example).
See: emscripten-core#27839
Instead of a dedicated `stripDefaultUndefined` Acorn AST pass (added in
compression (`{defaults: false, evaluate: true, keep_fargs: false}`)
when minifying JS without Closure Compiler. In addition to stripping
`= undefined` default parameter values, this performs lightweight Terser
cleanups (`undefined` -> `void 0`, single-statement block brace
removal, and constant folding).
In general, this prevents us from needing to replicate compression
passes that already exist in upstream Terser (see emscripten-core#27871).
Timing highlights (`test/hello_world.c` with `-sINCLUDE_FULL_LIBRARY`,
~252 KB minified JS):
- `minify_sync` with `compress: false`: 105.5 ms
- `minify_sync` with `compress: {defaults: false, evaluate: true,
keep_fargs: false}`: 188.3 ms (+82.8 ms, or ~+63 ms net after
removing the `stripDefaultUndefined` AST walk), saving ~1.4 KB.
- On a standard `-O2` `hello_world.c` build (~8.5 KB JS), `minify_sync`
goes from 3.2 ms to 5.5 ms (+2.3 ms).
This change, in general, should prevent us from being tempted to
replicate compression passes that already exist in upstream terser
(See emscripten-core#27871, for example).
See: emscripten-core#27839
Instead of a dedicated `stripDefaultUndefined` Acorn AST pass (added in
compression (`{defaults: false, evaluate: true, keep_fargs: false}`)
when minifying JS without Closure Compiler. In addition to stripping
`= undefined` default parameter values, this performs lightweight Terser
cleanups (`undefined` -> `void 0`, single-statement block brace
removal, and constant folding).
In general, this prevents us from needing to replicate compression
passes that already exist in upstream Terser (see emscripten-core#27871).
Timing highlights (`test/hello_world.c` with `-sINCLUDE_FULL_LIBRARY`,
~252 KB minified JS):
- `minify_sync` with `compress: false`: 105.5 ms
- `minify_sync` with `compress: {defaults: false, evaluate: true,
keep_fargs: false}`: 188.3 ms (+82.8 ms, or ~+63 ms net after
removing the `stripDefaultUndefined` AST walk), saving ~1.4 KB.
- On a standard `-O2` `hello_world.c` build (~8.5 KB JS), `minify_sync`
goes from 3.2 ms to 5.5 ms (+2.3 ms).
This change, in general, should prevent us from being tempted to
replicate compression passes that already exist in upstream terser
(See emscripten-core#27871, for example).
See: emscripten-core#27839
Instead of a dedicated `stripDefaultUndefined` Acorn AST pass (added in
compression (`{defaults: false, evaluate: true, keep_fargs: false}`)
when minifying JS without Closure Compiler. In addition to stripping
`= undefined` default parameter values, this performs lightweight Terser
cleanups (`undefined` -> `void 0`, single-statement block brace
removal, and constant folding).
In general, this prevents us from needing to replicate compression
passes that already exist in upstream Terser (see emscripten-core#27871).
Timing highlights (`test/hello_world.c` with `-sINCLUDE_FULL_LIBRARY`,
~252 KB minified JS):
- `minify_sync` with `compress: false`: 105.5 ms
- `minify_sync` with `compress: {defaults: false, evaluate: true,
keep_fargs: false}`: 188.3 ms (+82.8 ms, or ~+63 ms net after
removing the `stripDefaultUndefined` AST walk), saving ~1.4 KB.
- On a standard `-O2` `hello_world.c` build (~8.5 KB JS), `minify_sync`
goes from 3.2 ms to 5.5 ms (+2.3 ms).
This change, in general, should prevent us from being tempted to
replicate compression passes that already exist in upstream terser
(See emscripten-core#27871, for example).
See: emscripten-core#27839
Instead of a dedicated `stripDefaultUndefined` Acorn AST pass (added in
compression (`{defaults: false, evaluate: true, keep_fargs: false}`)
when minifying JS without Closure Compiler. In addition to stripping
`= undefined` default parameter values, this performs lightweight Terser
cleanups (`undefined` -> `void 0`, single-statement block brace
removal, and constant folding).
In general, this prevents us from needing to replicate compression
passes that already exist in upstream Terser (see #27871).
Timing highlights (`test/hello_world.c` with `-sINCLUDE_FULL_LIBRARY`,
~252 KB minified JS):
- `minify_sync` with `compress: false`: 105.5 ms
- `minify_sync` with `compress: {defaults: false, evaluate: true,
keep_fargs: false}`: 188.3 ms (+82.8 ms, or ~+63 ms net after
removing the `stripDefaultUndefined` AST walk), saving ~1.4 KB.
- On a standard `-O2` `hello_world.c` build (~8.5 KB JS), `minify_sync`
goes from 3.2 ms to 5.5 ms (+2.3 ms).
This change, in general, should prevent us from being tempted to
replicate compression passes that already exist in upstream terser
(See #27871, for example).
See: #27839
b5f892a to
fcf1363
Compare
inlineConstants acorn pass to inline primitive JS constantsconst in JS library code
|
OK, this change now just starts using JS |
fcf1363 to
5fc14dc
Compare
const in JS library codeconst in JS library code and enable Terser reduce_vars/unused
Enable `reduce_vars` and `unused` in Terser `--compress` (added in emscripten-core#27926) in `tools/acorn-optimizer.mjs`. This inlines `const` values into their expression uses and drops the resulting unused local `const` declarations (along with unused function parameters and single-use inner functions) in optimized builds that do not run Closure Compiler. This allows us to use `const` in JS library code without paying a code size cost, while reducing total JS code size in `test_no_closure_code_size` by 612 bytes. Timing highlights (`test/hello_world.c` with `-sINCLUDE_FULL_LIBRARY`, ~247 KB minified JS): - `minify_sync` with `compress: {defaults: false, evaluate: true, keep_fargs: false}`: 165.1 ms (250,514 B) - `minify_sync` adding `reduce_vars: true, unused: true`: 224.3 ms (+59.2 ms, or +26.7 ms for `unused` and +28.8 ms for `reduce_vars`), saving ~3.2 KB (247,346 B). - On a standard `-O2` `hello_world.c` build (~8.4 KB JS), `minify_sync` goes from 3.9 ms to 6.2 ms (+2.3 ms), saving 96 B.
5fc14dc to
55b8b32
Compare
|
I think I might split out the terser config changes and land them first.. |
Enable
reduce_varsandunusedin Terser--compress(added in#27926) in
tools/acorn-optimizer.mjs.This inlines
constvalues into their expression uses and drops theresulting unused local
constdeclarations (along with unused functionparameters and single-use inner functions) in optimized builds that do
not run Closure Compiler.
This allows us to use
constin JS library code without paying a codesize cost, while reducing total JS code size in
test_no_closure_code_sizeby 612 bytes.Timing highlights (
test/hello_world.cwith-sINCLUDE_FULL_LIBRARY,~247 KB minified JS):
minify_syncwithcompress: {defaults: false, evaluate: true, keep_fargs: false}: 165.1 ms (250,514 B)minify_syncaddingreduce_vars: true, unused: true: 224.3 ms(+59.2 ms, or +26.7 ms for
unusedand +28.8 ms forreduce_vars),saving ~3.2 KB (247,346 B).
-O2hello_world.cbuild (~8.4 KB JS),minify_syncgoes from 3.9 ms to 6.2 ms (+2.3 ms), saving 96 B.