Skip to content

Optimize compiler.mjs for __const dependencies and isDecorator - #28202

Open
sbc100 wants to merge 1 commit into
emscripten-core:mainfrom
sbc100:optimize_js_compiler
Open

sbc100 wants to merge 1 commit into
emscripten-core:mainfrom
sbc100:optimize_js_compiler

Conversation

@sbc100

@sbc100 sbc100 commented Nov 7, 2026

Copy link
Copy Markdown
Collaborator

Since #28144, every C define in struct_info.json is added to LibraryManager.library as three entries ($FOO, $FOO__const, and $FOO__internal). Adding hundreds more constants to struct_info.json (as in #28199) would otherwise incur two compile-time performance costs in compiler.mjs:

  1. In --symbols-only mode we only need to discover transitive dependencies on native symbols. As JS library functions replace magic numbers with struct_info.json constants, addImplicitDeps (src/jsifier.mjs) would otherwise record every referenced constant in __deps and repeatedly traverse them in getTransitiveDeps. Skipping __const symbols in --symbols-only mode avoids this work.

  2. Each struct_info.json constant adds three keys to LibraryManager.library, which is iterated over in multiple passes that call isDecorator (src/utility.mjs) on every key. Using lastIndexOf('__') and a Set lookup instead of decoratorSuffixes.some with endsWith avoids scanning all 15 decorator suffixes for every key in the library.

See: #28199

Since emscripten-core#28144, every C define in `struct_info.json` is added to
`LibraryManager.library` as three entries (`$FOO`, `$FOO__const`, and
`$FOO__internal`). Adding hundreds more constants to `struct_info.json`
(as in emscripten-core#28199) would otherwise incur two compile-time performance costs
in `compiler.mjs`:

1. In `--symbols-only` mode we only need to discover transitive
   dependencies on native symbols. As JS library functions replace magic
   numbers with `struct_info.json` constants, `addImplicitDeps`
   (`src/jsifier.mjs`) would otherwise record every referenced constant
   in `__deps` and repeatedly traverse them in `getTransitiveDeps`.
   Skipping `__const` symbols in `--symbols-only` mode avoids this work.

2. Each `struct_info.json` constant adds three keys to
   `LibraryManager.library`, which is iterated over in multiple passes
   that call `isDecorator` (`src/utility.mjs`) on every key. Using
   `lastIndexOf('__')` and a `Set` lookup instead of
   `decoratorSuffixes.some` with `endsWith` avoids scanning all 15
   decorator suffixes for every key in the library.

See: emscripten-core#28199
@sbc100
sbc100 requested a review from kripken November 7, 2026 02:42
@sbc100

sbc100 commented Nov 7, 2026

Copy link
Copy Markdown
Collaborator Author

I'd like to land this before #28199

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request optimizes dependency resolution and decorator suffix checks. In src/jsifier.mjs, a check is added to skip implicit dependencies when symbolsOnly is active and a constant variant of the dependency exists. In src/utility.mjs, the isDecorator function is optimized by converting the decoratorSuffixes array into a Set and checking the suffix using lastIndexOf instead of iterating with some. There are no review comments, and I have no feedback to provide.

@sbc100
sbc100 requested a review from dschuff November 7, 2026 03:00
@sbc100
sbc100 enabled auto-merge (squash) November 7, 2026 03:49

This branch has not been deployed

No deployments
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.

1 participant