fix: heap-allocate thread-local random buffer to reduce thread creation overhead#20488
Open
johnathan79717 wants to merge 1 commit intomerge-train/barretenbergfrom
Open
fix: heap-allocate thread-local random buffer to reduce thread creation overhead#20488johnathan79717 wants to merge 1 commit intomerge-train/barretenbergfrom
johnathan79717 wants to merge 1 commit intomerge-train/barretenbergfrom
Conversation
…on overhead The 1 MiB `thread_local` random buffer in `engine.cpp` was stored inline in the TLS segment (.tbss), causing every `pthread_create` to allocate and zero-initialize 1 MiB per thread. With 31 worker threads this added ~18 ms of overhead to thread pool creation — making UltraHonk verification slower at 32 cores than at 8 cores. Moving the buffer to heap allocation on first use reduces the TLS footprint from ~1 MiB to 16 bytes per thread, dropping thread pool creation from 18 ms to ~1.6 ms for 31 threads. Benchmark results (parity_base circuit, noir-rollup verifier target): Cores | Before | After 8 | 63 ms | 59 ms 32 | 72 ms | 51 ms 64 | 87 ms | 53 ms Resolves AztecProtocol/barretenberg#1624
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
thread_localrandom buffer inengine.cppfrom inline TLS (.tbss) to heap allocation on first useRoot cause
The
RandomBufferWrapperstruct had auint8_t buffer[1 << 20]inline array declaredthread_local. This placed 1 MiB in the ELF.tbsssection, meaning everypthread_createhad to allocate and zero-initialize 1 MiB of TLS. With 31 worker threads, this added ~18 ms to thread pool creation — which dominated verification time at high core counts.Benchmark results (parity_base circuit)
With IPA (noir-rollup):
Without IPA (noir-recursive-no-zk):
Remaining overhead at high core counts
After this fix there is still a small overhead at 64 cores vs 32 cores for IPA, and at 32/64 cores vs 8 cores for non-IPA. Two sources:
Thread creation still costs ~1.6 ms for 31 threads / ~3 ms for 63 threads — the baseline
pthread_create+ remaining TLS (~3 KiB) + stack mapping cost, paid once on firstparallel_forcall.notify_all()wakes all sleeping workers regardless of how much work there is. For the non-IPA 66-point MSM, all 31 (or 63) threads wake up, acquire the mutex, find no work left, and go back to sleep. This mutex contention scales linearly with thread count.Follow-up improvements that could close this gap:
start_tasks(), so a 66-point MSM never spawns 31 threadsnotify_one: wake only as many workers as there are iterations, avoiding spurious wakeupsbatch_multi_scalar_mulbased on total scalar countTest plan
.tbsssection shrinks from ~1 MiB to ~3 KiB viareadelf -S bin/bbResolves AztecProtocol/barretenberg#1624