Repository navigation
Make dlsym O(1) in the number of exports. NFC - #28125
Conversation
`_dlsym_js` enumerated every export of the library on each call, `Object.keys(lib.exports).indexOf(symbol)`, before doing anything else. For `RTLD_DEFAULT` on a main module that is `wasmImports`, which holds every export of the main module, so each `dlsym` call cost O(N log N) in the number of exported symbols, whether the symbol exists or not. The index is only needed when `addFunction` allocates a new table slot and the other threads replay the insertion by index (`_dlsym_catchup_js`). Test membership with `hasOwnProperty`, which gives the same answer as `Object.keys(...).indexOf(symbol) != -1` for these plain objects, and compute the enumeration index only in the branch that stores it into `symbolIndex`. A main module with 250,000 exported functions goes from 80.9 ms to 0.009 ms per `dlsym` call. Co-Authored-By: Claude Fable 5.1 <[email protected]>
There was a problem hiding this comment.
Code Review
This pull request optimizes dlsym in src/lib/libdylink.js by avoiding the enumeration of all library exports on every lookup. Instead, it uses hasOwnProperty for the existence check and only computes the symbol's index when a table slot is actually allocated. The reviewer suggested a further optimization to cache the keys array on the library object to prevent repeated enumeration during multiple allocations.
sbc100
left a comment
There was a problem hiding this comment.
I imagine you will need to rebaseline to codesize tests here.
Review feedback: compute the enumeration index into `newSymIndex` so the `makeSetValue` line stays simple. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This is an automatic change generated by tools/maint/rebaseline_tests.py. The following (1) test expectation files were updated by running the tests with `--rebaseline`: ``` codesize/test_codesize_hello_dylink_all.json: 862336 => 862390 [+54 bytes / +0.01%] Average change: +0.01% (+0.01% - +0.01%) ``` Co-Authored-By: Claude Opus 5.5 <[email protected]>
In compile-time JS code (`src/`, `tools/`, and compile-time helpers in `libwebgl.js`), use `Object.hasOwn` directly instead of `hasOwnProperty`. In runtime JS library code, introduce an internal `$hasOwn` helper in `libcore.js` that aliases `Object.hasOwn` when targeting modern browser versions (`MIN_FIREFOX_VERSION >= 92`, `MIN_CHROME_VERSION >= 93`, and `MIN_SAFARI_VERSION >= 150400`) and falls back to `Object.prototype.hasOwnProperty.call` otherwise. Note that this change comes with minor codesize change, in some cases an improvement and in some a regression. Note that if we were to bump our default min browser versions we would always see an improvement (perhaps we should always target modern browser in our codesize tests?) See emscripten-core#28125
In compile-time JS code (`src/`, `tools/`, and compile-time helpers in `libwebgl.js`), use `Object.hasOwn` directly instead of `hasOwnProperty`. In runtime JS library code, introduce an internal `$hasOwn` helper in `libcore.js` that aliases `Object.hasOwn` when targeting modern browser versions (`MIN_FIREFOX_VERSION >= 92`, `MIN_CHROME_VERSION >= 93`, and `MIN_SAFARI_VERSION >= 150400`) and falls back to `Object.prototype.hasOwnProperty.call` otherwise. Note that this change comes with minor codesize change, in some cases an improvement and in some a regression. Note that if we were to bump our default min browser versions we would always see an improvement (perhaps we should always target modern browser in our codesize tests?) See emscripten-core#28125
…ration Resolve the conflict in test/codesize/test_codesize_hello_dylink_all.json by taking main's version; the next commit rebaselines it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This is an automatic change generated by tools/maint/rebaseline_tests.py. The following (1) test expectation files were updated by running the tests with `--rebaseline`: ``` codesize/test_codesize_hello_dylink_all.json: 862405 => 862459 [+54 bytes / +0.01%] Average change: +0.01% (+0.01% - +0.01%) ``` Co-Authored-By: Claude Opus 5.5 <[email protected]>
In compile-time JS code (`src/`, `tools/`, and compile-time helpers in `libwebgl.js`), use `Object.hasOwn` directly instead of `hasOwnProperty`. In runtime JS library code, introduce an internal `$hasOwn` helper in `libcore.js` that aliases `Object.hasOwn` when targeting modern browser versions (`MIN_FIREFOX_VERSION >= 92`, `MIN_CHROME_VERSION >= 93`, and `MIN_SAFARI_VERSION >= 150400`) and falls back to `Object.prototype.hasOwnProperty.call` otherwise. Note that this change comes with minor codesize change, in some cases an improvement and in some a regression. Note that if we were to bump our default min browser versions we would always see an improvement (perhaps we should always target modern browser in our codesize tests?) See emscripten-core#28125
In compile-time JS code (`src/`, `tools/`, and compile-time helpers in `libwebgl.js`), use `Object.hasOwn` directly instead of `hasOwnProperty`. In runtime JS library code, introduce an internal `$hasOwn` helper in `libcore.js` that aliases `Object.hasOwn` when targeting modern browser versions (`MIN_FIREFOX_VERSION >= 92`, `MIN_CHROME_VERSION >= 93`, and `MIN_SAFARI_VERSION >= 150400`) and falls back to `Object.prototype.hasOwnProperty.call` otherwise. Note that this change comes with minor codesize change, in some cases an improvement and in some a regression. Note that if we were to bump our default min browser versions we would always see an improvement (perhaps we should always target modern browser in our codesize tests?) See #28125
|
I just landed #28129, so maybe you can rebase and use the |
|
Will do! |
…ration Resolve the conflict in test/codesize/test_codesize_hello_dylink_all.json by taking main's version; a later commit rebaselines it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
emscripten-core#28129 added `$hasOwn` to libcore.js. It aliases `Object.hasOwn` when the minimum browser versions support it and falls back to `Object.prototype.hasOwnProperty.call` otherwise. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This is an automatic change generated by tools/maint/rebaseline_tests.py. The following (1) test expectation files were updated by running the tests with `--rebaseline`: ``` codesize/test_codesize_hello_dylink_all.json: 862442 => 862462 [+20 bytes / +0.00%] Average change: +0.00% (+0.00% - +0.00%) ``` Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Can you come up with proposed comment message for this change? Something little more compact the the full PR description? Also, perhaps add |
I removed some details. WDYT?
Done. What does "NFC" stand for? |
Bump the default target browser versions in `src/settings.js`: - `MIN_CHROME_VERSION`: 85 -> 93 - `MIN_FIREFOX_VERSION`: 79 -> 92 - `MIN_SAFARI_VERSION`: 150000 -> 150400 This allows `$hasOwn` in `libcore.js` (introduced in emscripten-core#28129) to use the `Object.hasOwn` fast path by default. See: emscripten-core#28125
|
Sorry, that a convension we borrowed from llvm: NFC => Non-Functional Change. |
Bump the default target browser versions in `src/settings.js`: - `MIN_CHROME_VERSION`: 85 -> 93 - `MIN_FIREFOX_VERSION`: 79 -> 92 - `MIN_SAFARI_VERSION`: 150000 -> 150400 This allows `$hasOwn` in `libcore.js` (introduced in emscripten-core#28129) to use the `Object.hasOwn` fast path by default. See: emscripten-core#28125
Bump the default target browser versions in `src/settings.js`: - `MIN_CHROME_VERSION`: 85 (Aug 2020) -> 93 (Aug 2021) - `MIN_FIREFOX_VERSION`: 79 (Jul 2020) -> 92 (Sep 2021) - `MIN_SAFARI_VERSION`: 150000 (Sep 2021) -> 150400 (Mar 2022) This allows `$hasOwn` in `libcore.js` (introduced in emscripten-core#28129) to use the `Object.hasOwn` fast path by default. Note: This does not effect the minimum browsers versions that we support or the behaviour -sLEGACY_VM_SUPPORT. See: emscripten-core#28125
Bump the default target browser versions in `src/settings.js`: - `MIN_CHROME_VERSION`: 85 (Aug 2020) -> 93 (Aug 2021) - `MIN_FIREFOX_VERSION`: 79 (Jul 2020) -> 92 (Sep 2021) - `MIN_SAFARI_VERSION`: 150000 (Sep 2021) -> 150400 (Mar 2022) This allows `$hasOwn` in `libcore.js` (introduced in emscripten-core#28129) to use the `Object.hasOwn` fast path by default. Note: This does not effect the minimum browsers versions that we support or the behaviour -sLEGACY_VM_SUPPORT. See: emscripten-core#28125
Bump the default target browser versions in `src/settings.js`: - `MIN_CHROME_VERSION`: 85 (Aug 2020) -> 93 (Aug 2021) - `MIN_FIREFOX_VERSION`: 79 (Jul 2020) -> 92 (Sep 2021) - `MIN_SAFARI_VERSION`: 150000 (Sep 2021) -> 150400 (Mar 2022) This allows `$hasOwn` in `libcore.js` (introduced in emscripten-core#28129) to use the `Object.hasOwn` fast path by default. Note: This does not effect the minimum browsers versions that we support or the behaviour -sLEGACY_VM_SUPPORT. See: emscripten-core#28125
Bump the default target browser versions in `src/settings.js`: - `MIN_CHROME_VERSION`: 85 (Aug 2020) -> 93 (Aug 2021) - `MIN_FIREFOX_VERSION`: 79 (Jul 2020) -> 92 (Sep 2021) - `MIN_SAFARI_VERSION`: 150000 (Sep 2021) -> 150400 (Mar 2022) This allows `$hasOwn` in `libcore.js` (introduced in emscripten-core#28129) to use the `Object.hasOwn` fast path by default, and allows `$waitAsyncPolyfilled` in `libatomic.js` (updated in emscripten-core#28172) to omit the Chrome < 91 user-agent check by default. Note: This does not effect the minimum browsers versions that we support or the behaviour -sLEGACY_VM_SUPPORT. See: emscripten-core#28125
Bump the default target browser versions in `src/settings.js`: - `MIN_CHROME_VERSION`: 85 (Aug 2020) -> 93 (Aug 2021) - `MIN_FIREFOX_VERSION`: 79 (Jul 2020) -> 92 (Sep 2021) - `MIN_SAFARI_VERSION`: 150000 (Sep 2021) -> 150400 (Mar 2022) This allows `$hasOwn` in `libcore.js` (introduced in #28129) to use the `Object.hasOwn` fast path by default, and allows `$waitAsyncPolyfilled` in `libatomic.js` (updated in #28172) to omit the Chrome < 91 user-agent check by default. Note: This does not effect the minimum browsers versions that we support or the behaviour -sLEGACY_VM_SUPPORT. See: #28125
_dlsym_jsenumerated every export of the library on each call,Object.keys(lib.exports).indexOf(symbol), before doing anything else. ForRTLD_DEFAULTon a main module that iswasmImports, which holds every export of the main module, so eachdlsymcall cost O(N log N) in the number of exported symbols (V8 sorts the keys of a dictionary-mode object on everyObject.keys), whether the symbol exists or not. The index is only needed whenaddFunctionallocates a new table slot and the other threads replay the insertion by index (_dlsym_catchup_js, #18590).This change tests membership with the
hasOwnhelper (Object.hasOwn) and gives the same answer asObject.keys(...).indexOf(symbol) != -1for these plain objects. It computes the enumeration index only in the branch that stores it intosymbolIndex. The thread catch-up protocol is unchanged.Measured with a main module of N exported functions calling
dlsym(RTLD_DEFAULT, name)(emcc -O2 -pthread -sMAIN_MODULE=1 -sEXIT_RUNTIME=1, Node 26.13.0, ms per call, hits):Fixes #28124.
🤖 Generated with Claude Code