Skip to content

dylink: Synchronize dlsym (as well as dlopen) calls between threads - #18590

Merged
sbc100 merged 1 commit into
mainfrom
dlsync_dlsym
Jan 26, 2023
Merged

sbc100 merged 1 commit into
mainfrom
dlsync_dlsym

Conversation

@sbc100

@sbc100 sbc100 commented Jan 25, 2023 •

Copy link
Copy Markdown
Collaborator

Prior to this change we were tracking the sequence of dlopen events
and replaying them on each thread during dlsync.

Now we also track dlsym events. A new data structure is used here to
abstract over these two events (dlevent). During dlsync we then reply
both the dlopen and the dlsym events. For dlsym events we
serialized them as "dso+index", i.e. which dso did the symbol come from
and what is the index of the symbol within that dso's symbol table.

This is important for #18376.

@sbc100 sbc100 changed the title dylink: Synchronize dlsym calls (as well as dlopen) between threads dylink: Synchronize dlsym (as well as dlopen) calls between threads Jan 25, 2023
@sbc100
sbc100 force-pushed the unify_symbol_table branch from 9d2f486 to f7e4555 Compare January 25, 2023 18:04
Base automatically changed from unify_symbol_table to main January 25, 2023 21:30
@sbc100
sbc100 requested a review from kripken January 25, 2023 22:08
@sbc100
sbc100 force-pushed the dlsync_dlsym branch 3 times, most recently from 18b30a6 to f93937f Compare January 25, 2023 22:31
@sbc100
sbc100 requested a review from dschuff January 25, 2023 22:31
Comment thread src/library_addfunction.js Resolved
Comment thread src/library_dylink.js Resolved
Comment thread src/library_dylink.js Resolved
Comment thread src/library_dylink.js Resolved
Comment thread src/library_dylink.js Resolved
Comment thread system/lib/libc/dynlink.c Resolved
Comment thread system/lib/libc/dynlink.c Resolved
Comment thread system/lib/libc/dynlink.c Resolved
Prior to this change we were tracking the sequence of `dlopen` events
and replaying them on each thread during `dlsync`.

Now we also track `dlsym` events.  A new data structure is used here to
abstract over these two events (`dlevent`).  During dlsync we then reply
both the `dlopen` and the `dlsym` events.  For `dlsym` events we
serialized them as "dso+index", i.e. which dso did the symbol come from
and what is the index of the symbol within that dso's symbol table.

This is important for #18376.
@sbc100
sbc100 requested a review from kripken January 26, 2023 06:20
@sbc100
sbc100 enabled auto-merge (squash) January 26, 2023 16:57
@sbc100
sbc100 merged commit e883361 into main Jan 26, 2023
@sbc100
sbc100 deleted the dlsync_dlsym branch January 26, 2023 19:10
sbc100 pushed a commit that referenced this pull request Nov 2, 2026
`_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 (V8 sorts the keys of a dictionary-mode
object on every `Object.keys`), 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`,
#18590).

This change tests membership with the `hasOwn` helper (`Object.hasOwn`)
and gives the same answer as `Object.keys(...).indexOf(symbol) != -1`
for these plain objects. It computes the enumeration index only in the
branch that stores it into `symbolIndex`. 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):

| exports | before | after |
|---|---|---|
| 10,000 | 1.70 | 0.008 |
| 250,000 | 80.9 | 0.009 |

Fixes #28124.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Fable 5.1 <[email protected]>
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.

2 participants