Repository navigation
Replace stripDefaultUndefined with Terser --compress - #27926
Conversation
690446a to
5198190
Compare
kripken
left a comment
There was a problem hiding this comment.
Nice!
This looks right to me. Maybe the only question I have is if we want to run --compress in -O2 or just -O3 (I would lean towards not in -O2).
e8578dc to
2a0a2d8
Compare
Previously, at `-O3`/`-Os`/`-Oz` (`expensive_optimizations`), `minify_wasm_js` invoked `acorn-optimizer.mjs` up to four separate times to parse and reprint JS: 1. Pre-`metadce` (`AJSDCE`, `stripDefaultUndefined`, `--minify-whitespace`) 2. Inside `metadce` (`applyDCEGraphRemovals`, `--minify-whitespace`) 3. Post-`metadce` (`AJSDCE`, `--minify-whitespace`) 4. Inside `minify_wasm_imports_and_exports` (`applyImportAndExportNameChanges`, `--minify-whitespace`) Have `metadce` and `minify_wasm_imports_and_exports` return their removal/renaming metadata instead of invoking `acorn_optimizer` directly, and consolidate `applyDCEGraphRemovals`, post-`metadce` `AJSDCE`, `applyImportAndExportNameChanges`, `stripDefaultUndefined`, and `--minify-whitespace` into a single final `acorn_optimizer` pass. Split out from #27926
2a0a2d8 to
585c375
Compare
Really? Why? Do you expect |
|
Also, we also run whitespace minification on |
|
Rebased on top of #27928 .. much smaller change now |
585c375 to
45b8b6c
Compare
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
45b8b6c to
339d9e0
Compare
|
Should be good to go now. |
I meant compile time, not readability. That is, I am usually ok with making |
kripken
left a comment
There was a problem hiding this comment.
But this seems fast enough for -O2 as well.
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.
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.
Instead of a dedicated
stripDefaultUndefinedAcorn AST pass (added incompression (
{defaults: false, evaluate: true, keep_fargs: false})when minifying JS without Closure Compiler. In addition to stripping
= undefineddefault parameter values, this performs lightweight Tersercleanups (
undefined->void 0, single-statement block braceremoval, 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.cwith-sINCLUDE_FULL_LIBRARY,~252 KB minified JS):
minify_syncwithcompress: false: 105.5 msminify_syncwithcompress: {defaults: false, evaluate: true, keep_fargs: false}: 188.3 ms (+82.8 ms, or ~+63 ms net afterremoving the
stripDefaultUndefinedAST walk), saving ~1.4 KB.-O2hello_world.cbuild (~8.5 KB JS),minify_syncgoes 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