From 059b16f1484c8a333113c523c8461c330d354161 Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Fri, 27 Mar 2026 12:55:10 -0700 Subject: [PATCH] Remove unused element from pthread struct. NFC This reduces the memory overhead of a pthread from 124 bytes to 100 bytes (~20%) saving. --- src/struct_info_generated.json | 10 +++++----- src/struct_info_generated_wasm64.json | 10 +++++----- system/lib/libc/musl/src/internal/pthread_impl.h | 10 ++++++++++ system/lib/libc/musl/src/network/h_errno.c | 8 ++++++++ system/lib/libc/musl/src/thread/pthread_getattr_np.c | 2 +- system/lib/pthread/pthread_create.c | 1 - system/lib/pthread/pthread_self_stub.c | 1 - test/codesize/test_codesize_cxx_ctors1.json | 8 ++++---- test/codesize/test_codesize_cxx_ctors2.json | 8 ++++---- test/codesize/test_codesize_cxx_except.json | 8 ++++---- test/codesize/test_codesize_cxx_except_wasm.json | 8 ++++---- .../test_codesize_cxx_except_wasm_legacy.json | 8 ++++---- test/codesize/test_codesize_cxx_lto.json | 8 ++++---- test/codesize/test_codesize_cxx_mangle.json | 8 ++++---- test/codesize/test_codesize_cxx_noexcept.json | 8 ++++---- test/codesize/test_codesize_cxx_wasmfs.json | 8 ++++---- test/codesize/test_codesize_hello_dylink.json | 8 ++++---- test/codesize/test_codesize_hello_dylink_all.json | 4 ++-- test/codesize/test_codesize_minimal_pthreads.json | 12 ++++++------ .../test_codesize_minimal_pthreads_memgrowth.json | 12 ++++++------ 20 files changed, 83 insertions(+), 67 deletions(-) diff --git a/src/struct_info_generated.json b/src/struct_info_generated.json index 163a1cf69779c..d8f7cb73a919b 100644 --- a/src/struct_info_generated.json +++ b/src/struct_info_generated.json @@ -1036,11 +1036,11 @@ "p_proto": 8 }, "pthread": { - "__size__": 124, - "profilerBlock": 104, - "stack": 48, - "stack_size": 52, - "waiting_async": 120 + "__size__": 100, + "profilerBlock": 80, + "stack": 40, + "stack_size": 44, + "waiting_async": 96 }, "pthread_attr_t": { "__size__": 44, diff --git a/src/struct_info_generated_wasm64.json b/src/struct_info_generated_wasm64.json index 8b857fef5dbcb..0c5d0724d6f56 100644 --- a/src/struct_info_generated_wasm64.json +++ b/src/struct_info_generated_wasm64.json @@ -1036,11 +1036,11 @@ "p_proto": 16 }, "pthread": { - "__size__": 216, - "profilerBlock": 184, - "stack": 80, - "stack_size": 88, - "waiting_async": 212 + "__size__": 176, + "profilerBlock": 144, + "stack": 64, + "stack_size": 72, + "waiting_async": 172 }, "pthread_attr_t": { "__size__": 88, diff --git a/system/lib/libc/musl/src/internal/pthread_impl.h b/system/lib/libc/musl/src/internal/pthread_impl.h index b2588344eb1ab..854ef261d8719 100644 --- a/system/lib/libc/musl/src/internal/pthread_impl.h +++ b/system/lib/libc/musl/src/internal/pthread_impl.h @@ -29,7 +29,9 @@ struct pthread { uintptr_t *dtv; #endif struct pthread *prev, *next; /* non-ABI */ +#ifndef __EMSCRIPTEN__ uintptr_t sysinfo; +#endif #ifndef TLS_ABOVE_TP #ifdef CANARY_PAD uintptr_t canary_pad; @@ -49,10 +51,14 @@ struct pthread { unsigned char tsd_used:1; unsigned char dlerror_flag:1; unsigned char *map_base; +#ifndef __EMSCRIPTEN__ size_t map_size; +#endif void *stack; size_t stack_size; +#ifndef __EMSCRIPTEN__ size_t guard_size; +#endif void *result; struct __ptcb *cancelbuf; void **tsd; @@ -61,13 +67,17 @@ struct pthread { long off; volatile void *volatile pending; } robust_list; +#ifndef __EMSCRIPTEN__ int h_errno_val; volatile int timer_id; +#endif #ifndef __EMSCRIPTEN__ // Emscripten uses C11 _Thread_local instead for locale locale_t locale; #endif +#ifndef __EMSCRIPTEN__ volatile int killlock[1]; +#endif char *dlerror_buf; void *stdio_locks; diff --git a/system/lib/libc/musl/src/network/h_errno.c b/system/lib/libc/musl/src/network/h_errno.c index 638f77180314a..0ded77ce996ce 100644 --- a/system/lib/libc/musl/src/network/h_errno.c +++ b/system/lib/libc/musl/src/network/h_errno.c @@ -4,8 +4,16 @@ #undef h_errno int h_errno; +#ifdef __EMSCRIPTEN__ +static _Thread_local int __h_errno_storage; +#endif + int *__h_errno_location(void) { +#ifdef __EMSCRIPTEN__ + return &__h_errno_storage; +#else if (!__pthread_self()->stack) return &h_errno; return &__pthread_self()->h_errno_val; +#endif } diff --git a/system/lib/libc/musl/src/thread/pthread_getattr_np.c b/system/lib/libc/musl/src/thread/pthread_getattr_np.c index 5f5246133bc32..d7a85975f65f7 100644 --- a/system/lib/libc/musl/src/thread/pthread_getattr_np.c +++ b/system/lib/libc/musl/src/thread/pthread_getattr_np.c @@ -7,11 +7,11 @@ int pthread_getattr_np(pthread_t t, pthread_attr_t *a) { *a = (pthread_attr_t){0}; a->_a_detach = t->detach_state>=DT_DETACHED; - a->_a_guardsize = t->guard_size; #ifdef __EMSCRIPTEN__ a->_a_stackaddr = (uintptr_t)t->stack; a->_a_stacksize = t->stack_size; #else + a->_a_guardsize = t->guard_size; if (t->stack) { a->_a_stackaddr = (uintptr_t)t->stack; a->_a_stacksize = t->stack_size; diff --git a/system/lib/pthread/pthread_create.c b/system/lib/pthread/pthread_create.c index 570306a907a42..af6a360bf8844 100644 --- a/system/lib/pthread/pthread_create.c +++ b/system/lib/pthread/pthread_create.c @@ -172,7 +172,6 @@ int __pthread_create(pthread_t* restrict res, offset += sizeof(struct pthread); new->map_base = block; - new->map_size = size; // The pthread struct has a field that points to itself - this is used as a // magic ID to detect whether the pthread_t structure is 'alive'. diff --git a/system/lib/pthread/pthread_self_stub.c b/system/lib/pthread/pthread_self_stub.c index 8a484c7cb82ac..c7164916dc297 100644 --- a/system/lib/pthread/pthread_self_stub.c +++ b/system/lib/pthread/pthread_self_stub.c @@ -33,5 +33,4 @@ static void init_pthread_self(void) { __main_pthread.tid = getpid(); __main_pthread.stack = &__stack_high; __main_pthread.stack_size = ((size_t)&__stack_high) - ((size_t)&__stack_low); - __main_pthread.guard_size = __default_guardsize; } diff --git a/test/codesize/test_codesize_cxx_ctors1.json b/test/codesize/test_codesize_cxx_ctors1.json index dff56a4063fb0..b07e300812cc2 100644 --- a/test/codesize/test_codesize_cxx_ctors1.json +++ b/test/codesize/test_codesize_cxx_ctors1.json @@ -1,10 +1,10 @@ { "a.out.js": 19194, "a.out.js.gz": 7969, - "a.out.nodebug.wasm": 132638, - "a.out.nodebug.wasm.gz": 49927, - "total": 151832, - "total_gz": 57896, + "a.out.nodebug.wasm": 132618, + "a.out.nodebug.wasm.gz": 49913, + "total": 151812, + "total_gz": 57882, "sent": [ "__cxa_throw", "_abort_js", diff --git a/test/codesize/test_codesize_cxx_ctors2.json b/test/codesize/test_codesize_cxx_ctors2.json index fb2e77ec4ae92..eabeafa1948ab 100644 --- a/test/codesize/test_codesize_cxx_ctors2.json +++ b/test/codesize/test_codesize_cxx_ctors2.json @@ -1,10 +1,10 @@ { "a.out.js": 19171, "a.out.js.gz": 7957, - "a.out.nodebug.wasm": 132064, - "a.out.nodebug.wasm.gz": 49586, - "total": 151235, - "total_gz": 57543, + "a.out.nodebug.wasm": 132055, + "a.out.nodebug.wasm.gz": 49582, + "total": 151226, + "total_gz": 57539, "sent": [ "__cxa_throw", "_abort_js", diff --git a/test/codesize/test_codesize_cxx_except.json b/test/codesize/test_codesize_cxx_except.json index c29dbd2b26b46..1d07f32fc917c 100644 --- a/test/codesize/test_codesize_cxx_except.json +++ b/test/codesize/test_codesize_cxx_except.json @@ -1,10 +1,10 @@ { "a.out.js": 23174, "a.out.js.gz": 8960, - "a.out.nodebug.wasm": 172516, - "a.out.nodebug.wasm.gz": 57438, - "total": 195690, - "total_gz": 66398, + "a.out.nodebug.wasm": 172498, + "a.out.nodebug.wasm.gz": 57424, + "total": 195672, + "total_gz": 66384, "sent": [ "__cxa_begin_catch", "__cxa_end_catch", diff --git a/test/codesize/test_codesize_cxx_except_wasm.json b/test/codesize/test_codesize_cxx_except_wasm.json index cb737cf73678b..458e0acb64d53 100644 --- a/test/codesize/test_codesize_cxx_except_wasm.json +++ b/test/codesize/test_codesize_cxx_except_wasm.json @@ -1,10 +1,10 @@ { "a.out.js": 19026, "a.out.js.gz": 7904, - "a.out.nodebug.wasm": 147922, - "a.out.nodebug.wasm.gz": 55312, - "total": 166948, - "total_gz": 63216, + "a.out.nodebug.wasm": 147904, + "a.out.nodebug.wasm.gz": 55305, + "total": 166930, + "total_gz": 63209, "sent": [ "_abort_js", "_tzset_js", diff --git a/test/codesize/test_codesize_cxx_except_wasm_legacy.json b/test/codesize/test_codesize_cxx_except_wasm_legacy.json index 71fbc6b06eb6d..96ae36b15c51f 100644 --- a/test/codesize/test_codesize_cxx_except_wasm_legacy.json +++ b/test/codesize/test_codesize_cxx_except_wasm_legacy.json @@ -1,10 +1,10 @@ { "a.out.js": 19100, "a.out.js.gz": 7929, - "a.out.nodebug.wasm": 145729, - "a.out.nodebug.wasm.gz": 54945, - "total": 164829, - "total_gz": 62874, + "a.out.nodebug.wasm": 145711, + "a.out.nodebug.wasm.gz": 54939, + "total": 164811, + "total_gz": 62868, "sent": [ "_abort_js", "_tzset_js", diff --git a/test/codesize/test_codesize_cxx_lto.json b/test/codesize/test_codesize_cxx_lto.json index f92589e8aecc0..362000fe57b43 100644 --- a/test/codesize/test_codesize_cxx_lto.json +++ b/test/codesize/test_codesize_cxx_lto.json @@ -1,10 +1,10 @@ { "a.out.js": 18563, "a.out.js.gz": 7666, - "a.out.nodebug.wasm": 101956, - "a.out.nodebug.wasm.gz": 39461, - "total": 120519, - "total_gz": 47127, + "a.out.nodebug.wasm": 101945, + "a.out.nodebug.wasm.gz": 39456, + "total": 120508, + "total_gz": 47122, "sent": [ "a (emscripten_resize_heap)", "b (_setitimer_js)", diff --git a/test/codesize/test_codesize_cxx_mangle.json b/test/codesize/test_codesize_cxx_mangle.json index f2ddec2dbd75d..4822d58e3fb3b 100644 --- a/test/codesize/test_codesize_cxx_mangle.json +++ b/test/codesize/test_codesize_cxx_mangle.json @@ -1,10 +1,10 @@ { "a.out.js": 23224, "a.out.js.gz": 8983, - "a.out.nodebug.wasm": 238957, - "a.out.nodebug.wasm.gz": 79847, - "total": 262181, - "total_gz": 88830, + "a.out.nodebug.wasm": 238939, + "a.out.nodebug.wasm.gz": 79836, + "total": 262163, + "total_gz": 88819, "sent": [ "__cxa_begin_catch", "__cxa_end_catch", diff --git a/test/codesize/test_codesize_cxx_noexcept.json b/test/codesize/test_codesize_cxx_noexcept.json index 4881c4a93cac7..9874d66ce6ef2 100644 --- a/test/codesize/test_codesize_cxx_noexcept.json +++ b/test/codesize/test_codesize_cxx_noexcept.json @@ -1,10 +1,10 @@ { "a.out.js": 19194, "a.out.js.gz": 7969, - "a.out.nodebug.wasm": 134661, - "a.out.nodebug.wasm.gz": 50777, - "total": 153855, - "total_gz": 58746, + "a.out.nodebug.wasm": 134642, + "a.out.nodebug.wasm.gz": 50759, + "total": 153836, + "total_gz": 58728, "sent": [ "__cxa_throw", "_abort_js", diff --git a/test/codesize/test_codesize_cxx_wasmfs.json b/test/codesize/test_codesize_cxx_wasmfs.json index 9d83f3e7f89b1..efac9bbadbbc2 100644 --- a/test/codesize/test_codesize_cxx_wasmfs.json +++ b/test/codesize/test_codesize_cxx_wasmfs.json @@ -1,10 +1,10 @@ { "a.out.js": 7023, "a.out.js.gz": 3310, - "a.out.nodebug.wasm": 172714, - "a.out.nodebug.wasm.gz": 63316, - "total": 179737, - "total_gz": 66626, + "a.out.nodebug.wasm": 172696, + "a.out.nodebug.wasm.gz": 63298, + "total": 179719, + "total_gz": 66608, "sent": [ "__cxa_throw", "_abort_js", diff --git a/test/codesize/test_codesize_hello_dylink.json b/test/codesize/test_codesize_hello_dylink.json index efb0f7d1c501f..1b837fd95ede6 100644 --- a/test/codesize/test_codesize_hello_dylink.json +++ b/test/codesize/test_codesize_hello_dylink.json @@ -1,10 +1,10 @@ { "a.out.js": 26185, "a.out.js.gz": 11171, - "a.out.nodebug.wasm": 17668, - "a.out.nodebug.wasm.gz": 8921, - "total": 43853, - "total_gz": 20092, + "a.out.nodebug.wasm": 17652, + "a.out.nodebug.wasm.gz": 8911, + "total": 43837, + "total_gz": 20082, "sent": [ "__syscall_stat64", "emscripten_resize_heap", diff --git a/test/codesize/test_codesize_hello_dylink_all.json b/test/codesize/test_codesize_hello_dylink_all.json index cc1a38764c569..42f86c2aad540 100644 --- a/test/codesize/test_codesize_hello_dylink_all.json +++ b/test/codesize/test_codesize_hello_dylink_all.json @@ -1,7 +1,7 @@ { "a.out.js": 244300, - "a.out.nodebug.wasm": 577506, - "total": 821806, + "a.out.nodebug.wasm": 577410, + "total": 821710, "sent": [ "IMG_Init", "IMG_Load", diff --git a/test/codesize/test_codesize_minimal_pthreads.json b/test/codesize/test_codesize_minimal_pthreads.json index 75383a2ac05a7..3531c43fa4b63 100644 --- a/test/codesize/test_codesize_minimal_pthreads.json +++ b/test/codesize/test_codesize_minimal_pthreads.json @@ -1,10 +1,10 @@ { - "a.out.js": 7363, - "a.out.js.gz": 3604, - "a.out.nodebug.wasm": 19046, - "a.out.nodebug.wasm.gz": 8822, - "total": 26409, - "total_gz": 12426, + "a.out.js": 7362, + "a.out.js.gz": 3603, + "a.out.nodebug.wasm": 19033, + "a.out.nodebug.wasm.gz": 8807, + "total": 26395, + "total_gz": 12410, "sent": [ "a (memory)", "b (exit)", diff --git a/test/codesize/test_codesize_minimal_pthreads_memgrowth.json b/test/codesize/test_codesize_minimal_pthreads_memgrowth.json index 4d941efd39106..a042be5a8f650 100644 --- a/test/codesize/test_codesize_minimal_pthreads_memgrowth.json +++ b/test/codesize/test_codesize_minimal_pthreads_memgrowth.json @@ -1,10 +1,10 @@ { - "a.out.js": 7765, - "a.out.js.gz": 3810, - "a.out.nodebug.wasm": 19047, - "a.out.nodebug.wasm.gz": 8823, - "total": 26812, - "total_gz": 12633, + "a.out.js": 7764, + "a.out.js.gz": 3808, + "a.out.nodebug.wasm": 19034, + "a.out.nodebug.wasm.gz": 8808, + "total": 26798, + "total_gz": 12616, "sent": [ "a (memory)", "b (exit)",