Skip to content

Enable toplevel: true in acorn-optimizer.mjs Terser pass - #27987

Merged
sbc100 merged 1 commit into
emscripten-core:mainfrom
sbc100:acorn_toplevel
Oct 15, 2026
Merged

sbc100 merged 1 commit into
emscripten-core:mainfrom
sbc100:acorn_toplevel

Conversation

@sbc100

@sbc100 sbc100 commented Oct 15, 2026 •

Copy link
Copy Markdown
Collaborator

Terser's reduce_vars: true inlines top-level const declarations into
their use sites during minify_sync, which runs after JSDCE. Without
toplevel: true, Terser's unused: true pass refuses to drop top-level
declarations, leaving inlined top-level const declarations and other
unused top-level variables in the output.

This has negligible link-time performance impact (~1-3 ms on
minify_sync), since reduce_vars and unused already walk the full
AST_Toplevel tree regardless of the toplevel setting.

This should really have been part of #27949.

Summary by CodeRabbit

  • Performance
    • Generated JavaScript output is smaller, including compressed output. Recorded WebAssembly sizes are unchanged.
  • Tests
    • Updated code-size baselines to reflect the reduced output sizes.

Terser's `reduce_vars: true` inlines top-level `const` declarations into
their use sites during `minify_sync`, which runs after `JSDCE`. Without
`toplevel: true`, Terser's `unused: true` pass refuses to drop top-level
declarations, leaving inlined top-level `const` declarations and other
unused top-level variables in the output.

This should really have been part of emscripten-core#27949.
@sbc100
sbc100 requested review from dschuff and kripken October 15, 2026 16:49
@sbc100
sbc100 enabled auto-merge (squash) October 15, 2026 17:14
@sbc100

sbc100 commented Oct 15, 2026

Copy link
Copy Markdown
Collaborator Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Oct 15, 2026 •

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai

coderabbitai Bot commented Oct 15, 2026

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 992aba46-a406-44f4-8b31-1050d10fc7e1
📥 Commits

Reviewing files that changed from the base of the PR and between 61b8d61 and 59c2922.

📒 Files selected for processing: 4
  • test/codesize/test_codesize_hello_esm_integration.json
  • test/codesize/test_codesize_minimal_esm_integration.json
  • test/codesize/test_no_closure_code_size.json
  • tools/acorn-optimizer.mjs

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

Walkthrough

The optimizer now enables Terser top-level compression. Recorded JavaScript and support-module size baselines decrease, while recorded WASM sizes remain unchanged.

Changes

Compression and size baselines

Layer / File(s) Summary
Enable top-level compression and update baselines
tools/acorn-optimizer.mjs, test/codesize/*
The enabled compression options now include toplevel: true. Recorded JavaScript and support-module sizes and totals decrease. Recorded WASM sizes remain unchanged.

Priority: ⬇️ Low

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Merge Risk: ⚪ Minimal · up to 59c29

The recorded JavaScript sizes decrease while WASM sizes remain unchanged, and no material behavior or merge risk is evident.

Pre-merge checks: Passed 5
Passed Passed checks: 5 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: enabling Terser’s toplevel: true option in acorn-optimizer.mjs.
Docstring Coverage Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 1…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@sbc100
sbc100 merged commit 84a0bde into emscripten-core:main Oct 15, 2026
42 checks passed
@sbc100
sbc100 deleted the acorn_toplevel branch October 15, 2026 18:21
@sbc100

sbc100 commented Oct 15, 2026

Copy link
Copy Markdown
Collaborator Author

oops, coderabbit just reviews this change and auto-commit landed it. I guess we are in the future already.

@kripken can you review this still please?

@kripken

kripken commented Oct 15, 2026

Copy link
Copy Markdown
Member

Wait... is coderabbit a committer in this repo? Isn't that needed for lgtm to allow landing?

(I don't think it should be a committer, if so)

@kripken kripken left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm from me too...

@dschuff

dschuff commented Oct 15, 2026

Copy link
Copy Markdown
Member

Is it necessary for Coderabbit to both modify the OP and also post a comment? Seems like only the latter would be preferable.

@sbc100

sbc100 commented Oct 16, 2026

Copy link
Copy Markdown
Collaborator Author

There are a lot of options / settings we have for controlling that kind of thing. Right now I'm mostly concerned about the quality of reviews themselves not being as good as, e.g. gemini

sbc100 added a commit to sbc100/emscripten that referenced this pull request Nov 18, 2026
After `FS.filesystems` was removed in emscripten-core#27940 and `toplevel: true` was
enabled for Terser in emscripten-core#27987, optional filesystem backends linked in via
`-lidbfs.js`, `-lworkerfs.js`, `-lproxyfs.js`, `-lopfs.js`, etc. had no
remaining references in the generated JS and were eliminated in `-O2`
and `-O3` builds when referenced only from external JS (such as
`Module.preRun` in HTML).

Mark these optional filesystem backend objects with `__export: true` so
that when `$FS` pulls them in, they are exported and preserved across
JS optimizations (including `--closure=1`, `-sMODULARIZE`, and
`-sMODULARIZE=instance`), without adding any code size when `$FS` is not
used or under default settings.

Fixes: emscripten-core#28072
sbc100 added a commit to sbc100/emscripten that referenced this pull request Nov 18, 2026
After `FS.filesystems` was removed in emscripten-core#27940 and `toplevel: true` was
enabled for Terser in emscripten-core#27987, optional filesystem backends linked in via
`-lidbfs.js`, `-lworkerfs.js`, `-lproxyfs.js`, `-lopfs.js`, etc. had no
remaining references in the generated JS and were eliminated in `-O2`
and `-O3` builds when referenced only from external JS (such as
`Module.preRun` in HTML).

Mark these optional filesystem backend objects with `__export: true` so
that when `$FS` pulls them in, they are exported and preserved across
JS optimizations (including `--closure=1`, `-sMODULARIZE`, and
`-sMODULARIZE=instance`), without adding any code size when `$FS` is not
used or under default settings.

Fixes: emscripten-core#28072
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants