Skip to content

Don't walk the main module's table in pthread workers - #28105

Open
clementperon wants to merge 2 commits into
emscripten-core:mainfrom
clementperon:dylink-worker-export-slots
Open

clementperon wants to merge 2 commits into
emscripten-core:mainfrom
clementperon:dylink-worker-export-slots

Conversation

@clementperon

Copy link
Copy Markdown
Contributor

The first call to getFunctionAddress walks the whole table and keeps a JS function object for every entry. With MAIN_MODULE and pthreads, every worker makes that call when it instantiates the main module (updateGOT), so each worker holds a wrapper for every address-taken function of the main module for its whole lifetime.

The dynamic linker only looks up the main module's exported functions, so the main thread now records their table slots, before updateGOT, and passes them to workers in the load message next to sharedModules. A worker adds just those slots to its map, plus anything beyond the main module's initial table (side modules, later addFunction slots).

Impact

Kodi's WebAssembly port links Kodi as MAIN_MODULE=2 so binary add-ons can be dlopened. Its table has 50,551 slots, of which 102 hold exported functions. Measured in Chrome with the same wasm, applying this change to the Emscripten 6.0.9 glue:

before after
JS heap per worker, after an add-on is loaded 15.2 MiB 7.2 MiB
JS heap of 17 workers during playback 257 MiB 117 MiB

Behavior

Lookups in workers return the same slots as before:

  • A function that appears in several slots still resolves to its last slot, as the walk did.
  • A JS function that addFunction wrapped is still found through the entry addFunction records.

The difference is a non-exported main-module function passed to addFunction in a worker. That function is only reachable from JS through the table itself. Before, the walk found its existing slot; now it would get a new slot. With ASSERTIONS=2, addFunction already checks for this case ("function in Table but not functionsInTableMap").

The main thread still walks the table once. #27993 creates the main module's GOT entries lazily and doesn't change the walk, so the two changes are independent.

Testing

test_pthread_dylink now asserts that a side module sees the same &bar_main as the main module, on the main thread and in a worker. That assertion fails if workers don't add the recorded slots.

Passing: all test_pthread_dl*, test_pthread_dylink* and test_pthread_tls_dylink tests in core0, core2 and wasm64, other.test_dylink_pthread*, and the dylink codesize tests (unchanged). The core0 tests also pass with -sASSERTIONS=2.

🤖 Generated with Claude Code

clementperon and others added 2 commits October 29, 2026 23:04
The first call to getFunctionAddress walks the whole table, which creates a
JS function object for every entry.  With MAIN_MODULE and pthreads, every
worker does this when it instantiates the main module, and keeps those
objects for its lifetime: about 8 MiB per worker for a 50k-entry table.

The dynamic linker only looks up the main module's exported functions, so
the main thread now records their table slots and passes them to workers,
which add just those to the map.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@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 worker initialization in pthreads builds with MAIN_MODULE by passing only the table slots of the main module's exported functions to workers, rather than having them walk the entire function table. Feedback suggests adding a defensive check in src/lib/libaddfunction.js to ensure getWasmTableEntry(slot) returns a valid object before adding it to the WeakMap, preventing potential runtime crashes in workers.

Comment thread src/lib/libaddfunction.js

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