Skip to content

Worker index from the thread's TLS slot in place on Windows - #242

Merged
nicolas-maman merged 1 commit into
mainfrom
tls-inline
Nov 25, 2026
Merged

nicolas-maman merged 1 commit into
mainfrom
tls-inline

Conversation

@nicolas-maman

Copy link
Copy Markdown
Collaborator

Progress at a glance

  • aephysics_worker_index_inline: one gs load on 64-bit Windows, with the existing function as fallback
  • TEB.TlsSlots offset checked against winternl.h at compile time
  • native.worker_index() and native.scratch() use it; Linux and macOS unchanged
  • Full local suite on Aether 0.945.0: green, golden trace bit for bit, no warnings

Why

Every per-worker scratch block and the speculative distance are chosen through the calling worker's index. That's 85 million lookups in a convex pile run and 11 million in trees25 (counted). On Windows the index lives in a native TLS slot, and each read was a TlsGetValue call into kernel32 at 1.14 ns. Compiler thread-locals aren't an option, because MinGW emulates them through __emutls_get_address (aefd8c9).

For an index under 64, TlsGetValue just reads the calling thread's TEB.TlsSlots[index], which Microsoft documents in winternl.h. The inline version reads it in place: 0.28 ns per lookup. Three cases still go through the original function: a slot not yet created, an index of 64 or more, and a thread that hasn't claimed a slot.

Numbers

Pinned, best of five (median), ms per step:

Scene main this PR
convex pile 9.71 (9.81) 9.62 (9.77)
trees25 1.381 (1.414) 1.371 (1.394)
sleep 1.925 (1.950) 1.908 (1.947)

Every per-worker scratch block and the speculative distance are chosen
through the calling worker's index, tens of millions of times a run of
the convex pile (85 million) and trees25 (11 million). On Windows it is
a native TLS slot, and every read was TlsGetValue, a call into kernel32
of 1.14 ns. For an index under 64 TlsGetValue reads the thread's
TEB.TlsSlots[index]: aephysics_worker_index_inline (aephysics_inline.h)
reads it in place with one load from gs, 0.28 ns, the offset checked
against winternl.h when aephysics_native.c compiles; a slot not yet made,
an index past 64 or a thread that has not claimed one still goes through
the function. Elsewhere nothing changes. Pinned, best of five: the convex
pile 9.71 to 9.62 ms a step, trees25 1.381 to 1.371 (median 1.414 to
1.394), sleep 1.925 to 1.908. Bits unchanged.
@nicolas-maman
nicolas-maman merged commit c2d53dd into main Nov 25, 2026
3 checks passed
@nicolas-maman
nicolas-maman deleted the tls-inline branch November 25, 2026 20:01
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