Repository navigation
[pthread] Embed em_task_queue mailbox directly in struct pthread - #28449
Merged
Merged
Conversation
Collaborator
Author
|
I noticed this optimization opportunity while reviewing #28402 |
sbc100
force-pushed
the
inline_pthread_mailbox
branch
from
November 30, 2026 22:01
0d91e74 to
64062d9
Compare
Collaborator
Author
|
Updated codesize expectations |
sbc100
force-pushed
the
inline_pthread_mailbox
branch
3 times, most recently
from
December 1, 2026 18:06
f3aa8ee to
0f06742
Compare
tlively
approved these changes
Dec 1, 2026
tlively
left a comment
Member
There was a problem hiding this comment.
LGTM % suggestions for comments.
Previously `_emscripten_thread_mailbox_init` called `em_task_queue_create`, which performed two `malloc` calls (one for the `em_task_queue` struct and one for its `tasks` ring buffer). Unlike proxying queues, a thread's mailbox never gets placed on the deferred-free zombie list because notifications do not carry a pointer to it and `_emscripten_thread_mailbox_shutdown` waits for all senders to finish and cancels pending notifications before teardown. Split out in-place `em_task_queue_init` / `em_task_queue_deinit` helpers and embed `em_task_queue mailbox` directly in `struct pthread` to avoid the `em_task_queue` heap allocation on thread creation. As a followup, we would also consider making the remaining allocation in `em_task_queue_init` optional/lazy in some way.
sbc100
force-pushed
the
inline_pthread_mailbox
branch
from
December 1, 2026 22:04
0f06742 to
41a4b56
Compare
sbc100
added a commit
to sbc100/emscripten
that referenced
this pull request
Dec 2, 2026
Following emscripten-core#28449, each `em_task_queue` only ever carries one pending notification in its target thread's mailbox at a time (coalesced via `NOTIFICATION_PENDING`) and already stays alive on the zombie list until `notification` transitions back to `NOTIFICATION_NONE`. Replace `em_task_queue mailbox` in `struct pthread` with a lightweight `em_thread_mailbox` intrusive FIFO linked via `em_task_queue::mailbox_next`: - Eliminates the remaining `malloc`/`free` on thread creation and shutdown, saving 1,536 bytes (wasm32) or 3,072 bytes (wasm64) of heap allocation per thread (plus allocator overhead) and making `_emscripten_thread_mailbox_init` infallible. - Shrinks `struct pthread` from 176 to 152 bytes on wasm32 (312 to 272 on wasm64) and drops the `em_task_queue_init`/`deinit` split and indirect notification wrapper functions.
sbc100
added a commit
to sbc100/emscripten
that referenced
this pull request
Dec 2, 2026
Following emscripten-core#28449, each `em_task_queue` only ever carries one pending notification in its target thread's mailbox at a time (coalesced via `NOTIFICATION_PENDING`) and already stays alive on the zombie list until `notification` transitions back to `NOTIFICATION_NONE`. Replace `em_task_queue mailbox` in `struct pthread` with a lightweight `em_thread_mailbox` intrusive FIFO linked via `em_task_queue::mailbox_next`: - Eliminates the remaining `malloc`/`free` on thread creation and shutdown, saving 1,536 bytes (wasm32) or 3,072 bytes (wasm64) of heap allocation per thread (plus allocator overhead) and making `_emscripten_thread_mailbox_init` infallible. - Shrinks `struct pthread` from 176 to 152 bytes on wasm32 (312 to 272 on wasm64) and drops the `em_task_queue_init`/`deinit` split and indirect notification wrapper functions.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Previously
_emscripten_thread_mailbox_initcalledem_task_queue_create, which performed twomalloccalls (one for theem_task_queuestruct and one for itstasksring buffer).Unlike proxying queues, a thread's mailbox never gets placed on the deferred-free zombie list because notifications do not carry a pointer to it and
_emscripten_thread_mailbox_shutdownwaits for all senders to finish and cancels pending notifications before teardown.Split out in-place
em_task_queue_init/em_task_queue_deinithelpers and embedem_task_queue mailboxdirectly instruct pthreadto avoid theem_task_queueheap allocation on thread creation.As a followup, we would also consider making the remaining allocation in
em_task_queue_initoptional/lazy in some way.