From 7f777086b0475a7d136a6f1902eb9b60eb01a62f Mon Sep 17 00:00:00 2001 From: Nicolas Maman Date: Wed, 25 Nov 2026 16:42:07 -0300 Subject: [PATCH] The worker index read from the thread's TLS slot in place on Windows 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. --- aephysics/native/aephysics_inline.h | 26 ++++++++++++++++++++++++++ aephysics/native/aephysics_native.c | 12 +++++++++++- aephysics/native/module.ae | 5 +++-- 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/aephysics/native/aephysics_inline.h b/aephysics/native/aephysics_inline.h index f812ec0..66480cb 100644 --- a/aephysics/native/aephysics_inline.h +++ b/aephysics/native/aephysics_inline.h @@ -21,6 +21,32 @@ static inline void aephysics_prefetch(const void *p) #endif } +// Which worker the calling thread is (aephysics_worker_index in +// aephysics_native.c), without a call where that can be had. On 64-bit +// Windows the worker's slot is a native TLS index, and TlsGetValue for an +// index under 64 reads the thread's TEB.TlsSlots[index]: read here in place, +// one load from gs (the offset is checked against winternl.h in +// aephysics_native.c). Every per-worker scratch block is chosen through +// this, tens of millions of times a run of the convex pile, where the call +// into kernel32 was 2% of the step. A slot not yet made, an index past 64 +// or a thread that has not claimed one goes through the function. +int aephysics_worker_index(void); +#if defined(_WIN32) && defined(__x86_64__) +extern unsigned long aephysics_tls_slot; +static inline int aephysics_worker_index_inline(void) +{ + unsigned long slot = aephysics_tls_slot; + if (slot < 64) { + unsigned long long stored; + __asm__ __volatile__("movq %%gs:0x1480(,%1,8), %0" : "=r"(stored) : "r"((unsigned long long)slot)); + if (stored != 0) return (int)(stored - 1); + } + return aephysics_worker_index(); +} +#else +static inline int aephysics_worker_index_inline(void) { return aephysics_worker_index(); } +#endif + // A spin's pause: the processor's hint that the thread is waiting. static inline void aephysics_pause(void) { diff --git a/aephysics/native/aephysics_native.c b/aephysics/native/aephysics_native.c index a2a3061..6dc4698 100644 --- a/aephysics/native/aephysics_native.c +++ b/aephysics/native/aephysics_native.c @@ -97,7 +97,17 @@ void aephysics_release_slots(int start, int count) // exits. Elsewhere the compiler's TLS is native already, and a pthread // key's destructor gives the slot back. #ifdef _WIN32 -static DWORD g_worker_slot = TLS_OUT_OF_INDEXES; +// The slot's index, read by aephysics_worker_index_inline +// (aephysics_inline.h) as well, which takes the thread's value straight +// from its TEB.TlsSlots, the array TlsGetValue reads for an index under 64: +// the offset it uses is checked here against winternl.h. +#include +#include +#if defined(__x86_64__) +_Static_assert(offsetof(TEB, TlsSlots) == 0x1480, "TEB.TlsSlots moved: aephysics_worker_index_inline reads it at gs:0x1480"); +#endif +DWORD aephysics_tls_slot = TLS_OUT_OF_INDEXES; +#define g_worker_slot aephysics_tls_slot static DWORD g_owned_slot = FLS_OUT_OF_INDEXES; static void WINAPI release_owned_slot(void *value) diff --git a/aephysics/native/module.ae b/aephysics/native/module.ae index f08d3a7..402596b 100644 --- a/aephysics/native/module.ae +++ b/aephysics/native/module.ae @@ -20,6 +20,7 @@ exports ( const MAX_WORKERS = 64 extern aephysics_worker_index() -> int +extern aephysics_worker_index_inline() -> int @c_import extern aephysics_set_worker_index(index: int) extern aephysics_claim_slots(count: int) -> int extern aephysics_release_slots(start: int, count: int) @@ -77,7 +78,7 @@ name_text(name: ptr) -> string { return aephysics_name_text(name) } // world's step, else the slot the thread claimed the first time it asked // (given back when it exits). Every module's scratch is a block per slot // chosen by this, so threads querying a world at once never share one. -worker_index() -> int { return aephysics_worker_index() } +worker_index() -> int { return aephysics_worker_index_inline() } set_worker_index(index: int) { aephysics_set_worker_index(index) } // A run of `count` worker slots claimed (the first, or -1 when none is @@ -120,4 +121,4 @@ semaphore_signal(s: ptr, count: int) { aephysics_semaphore_signal(s, count) } // MAX_WORKERS blocks of `bytes`: what the reference keeps on the stack // of each thread, kept here as one block per worker since Aether has // neither stack arrays nor thread-local variables. -scratch(blocks: @into ptr, bytes: int) -> ptr { return blocks + aephysics_worker_index() * bytes } +scratch(blocks: @into ptr, bytes: int) -> ptr { return blocks + aephysics_worker_index_inline() * bytes }