Skip to content

Optimize JS compiler caching for standard builds#27026

Merged
brendandahl merged 1 commit into
emscripten-core:mainfrom
brendandahl:cache-fix
Jun 9, 2026
Merged

Optimize JS compiler caching for standard builds#27026
brendandahl merged 1 commit into
emscripten-core:mainfrom
brendandahl:cache-fix

Conversation

@brendandahl

Copy link
Copy Markdown
Collaborator

Linker system stubs, such as libpthread_stub.js, are dynamically appended to settings.JS_LIBRARIES for all standard C/C++ builds. Previously, the JS output cache check did not distinguish between these system stubs and user-defined JS libraries (passed via --js-library). As a result, standard builds always bypassed the compilation cache, triggering a Node.js compilation run on every invocation.

Restrict the cache bypass condition to check only for user libraries (libraries located outside the Emscripten source tree). Standard compilations now successfully hit the JS output cache, decreasing baseline compilation wall clock time of a hello world C program by 52% (from 472 ms down to 282 ms).

Add an integration test to validate JavaScript compilation caching (covering hits, misses, user library bypasses, and option-based cache entries).

@brendandahl brendandahl requested review from kripken and sbc100 May 28, 2026 22:11
Comment thread tools/emscripten.py
# Sadly we have to skip the caching whenever we have user JS libraries. This is because
# these libraries can import arbitrary other JS files (either vis node's `import` or via #include)
if DEBUG or settings.BOOTSTRAPPING_STRUCT_INFO or config.FROZEN_CACHE or settings.JS_LIBRARIES:
has_user_libs = any(not lib.startswith(utils.path_from_root('src/')) for lib in settings.JS_LIBRARIES)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Alternatively, we could stick system libs in a separate setting, but that seems like a bigger change.

@sbc100 sbc100 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nice!

Comment thread test/test_sanity.py Outdated
Comment thread test/test_sanity.py Outdated
Linker system stubs, such as libpthread_stub.js, are dynamically
appended to settings.JS_LIBRARIES for all standard C/C++ builds.
Previously, the JS output cache check did not distinguish between
these system stubs and user-defined JS libraries (passed via
--js-library). As a result, standard builds always bypassed the
compilation cache, triggering a Node.js compilation run
on every invocation.

Restrict the cache bypass condition to check only for user
libraries (libraries located outside the Emscripten source tree).
Standard compilations now successfully hit the JS output cache,
decreasing baseline compilation wall clock time of a hello world C
program by 52% (from 472 ms down to 282 ms).

Add an integration test to validate JavaScript
compilation caching (covering hits, misses, user library bypasses,
and option-based cache entries).

@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.

Neat!

@brendandahl brendandahl merged commit e11edd8 into emscripten-core:main Jun 9, 2026
30 checks passed
@sbc100

sbc100 commented Jun 10, 2026

Copy link
Copy Markdown
Collaborator

It looks like this broke repeated runs of other.test_undefined_symbols_error_GL2. The first time you run it (with a cold cache), it works.

The second time it fails because it expects to see the warning generated during the processing.

I think the solution here is the avoid cache if there are any warnings during generation... but I don't know of an easy way to do that.

@brendandahl

Copy link
Copy Markdown
Collaborator Author

I think we can capture stderr and replay it. I'll put up a pr soon.

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