Skip to content

[pthread] Embed em_task_queue mailbox directly in struct pthread - #28449

Merged
sbc100 merged 1 commit into
emscripten-core:mainfrom
sbc100:inline_pthread_mailbox
Dec 1, 2026
Merged

sbc100 merged 1 commit into
emscripten-core:mainfrom
sbc100:inline_pthread_mailbox

Conversation

@sbc100

@sbc100 sbc100 commented Nov 30, 2026

Copy link
Copy Markdown
Collaborator

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
sbc100 requested a review from tlively as a code owner November 30, 2026 20:32
@sbc100

sbc100 commented Nov 30, 2026

Copy link
Copy Markdown
Collaborator Author

I noticed this optimization opportunity while reviewing #28402

@sbc100

sbc100 commented Nov 30, 2026

Copy link
Copy Markdown
Collaborator Author

Updated codesize expectations

@sbc100
sbc100 force-pushed the inline_pthread_mailbox branch 3 times, most recently from f3aa8ee to 0f06742 Compare December 1, 2026 18:06

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

LGTM % suggestions for comments.

Comment thread system/lib/pthread/em_task_queue.h Resolved
Comment thread system/lib/pthread/library_pthread.c Resolved
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
sbc100 force-pushed the inline_pthread_mailbox branch from 0f06742 to 41a4b56 Compare December 1, 2026 22:04
@sbc100
sbc100 merged commit 6d2e4f9 into emscripten-core:main Dec 1, 2026
19 of 26 checks passed
@sbc100
sbc100 deleted the inline_pthread_mailbox branch December 1, 2026 22:12
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.
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