Skip to content

Commit 574f4f4

Browse files
authored
Don't memset/zero the stack region for new pthread. NFC (#19125)
We had reports of slowdown when #17090 landed. Seems I overlooked that we were previously not memset'ing the stack region. This change should restore the previous performance. Fixes: #18628
1 parent 3a4035e commit 574f4f4

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

system/lib/pthread/pthread_create.c

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -148,20 +148,22 @@ int __pthread_create(pthread_t* restrict res,
148148
//
149149
// 1. pthread struct (sizeof struct pthread)
150150
// 2. tls data (__builtin_wasm_tls_size())
151-
// 3. stack (_emscripten_default_pthread_stack_size())
152-
// 4. tsd pointers (__pthread_tsd_size)
151+
// 3. tsd pointers (__pthread_tsd_size)
152+
// 4. stack (_emscripten_default_pthread_stack_size())
153153
size_t size = sizeof(struct pthread);
154154
if (__builtin_wasm_tls_size()) {
155155
size += __builtin_wasm_tls_size() + __builtin_wasm_tls_align() - 1;
156156
}
157+
size += __pthread_tsd_size + TSD_ALIGN - 1;
158+
size_t zero_size = size;
157159
if (!attr._a_stackaddr) {
158160
size += attr._a_stacksize + STACK_ALIGN - 1;
159161
}
160-
size += __pthread_tsd_size + TSD_ALIGN - 1;
161162

162-
// Allocate all the data for the new thread and zero-initialize.
163+
// Allocate all the data for the new thread and zero-initialize all parts
164+
// except for the stack.
163165
unsigned char* block = emscripten_builtin_malloc(size);
164-
memset(block, 0, size);
166+
memset(block, 0, zero_size);
165167

166168
uintptr_t offset = (uintptr_t)block;
167169

@@ -195,7 +197,14 @@ int __pthread_create(pthread_t* restrict res,
195197
offset += __builtin_wasm_tls_size();
196198
}
197199

198-
// 3. stack data
200+
// 3. tsd slots
201+
if (__pthread_tsd_size) {
202+
offset = ROUND_UP(offset, TSD_ALIGN);
203+
new->tsd = (void*)offset;
204+
offset += __pthread_tsd_size;
205+
}
206+
207+
// 4. stack data
199208
// musl stores top of the stack in pthread_t->stack (i.e. the high
200209
// end from which it grows down).
201210
if (attr._a_stackaddr) {
@@ -205,13 +214,6 @@ int __pthread_create(pthread_t* restrict res,
205214
new->stack = (void*)offset;
206215
}
207216

208-
// 4. tsd slots
209-
if (__pthread_tsd_size) {
210-
offset = ROUND_UP(offset, TSD_ALIGN);
211-
new->tsd = (void*)offset;
212-
offset += __pthread_tsd_size;
213-
}
214-
215217
// Check that we didn't use more data than we allocated.
216218
assert(offset < (uintptr_t)block + size);
217219

0 commit comments

Comments
 (0)