Skip to content

Commit 4b0a1cc

Browse files
authored
fix: improve atomic ordering in ThreadPool and NAPI init (#310)
## Motivation `ThreadPool` worker hot loops use `.acquire` on `err_flag.load()` where `.monotonic` suffices — the flag is a pure early-exit signal with no data dependency on the setter's other writes. BLS verification is CPU-intensive, so relaxing this in the inner loop avoids unnecessary memory-fence cost. Matches the pattern already used in `src/state_transition/cache/pubkey_cache.zig`. The earlier NAPI init-mutex changes from this branch have been dropped after merging main, because main moved to zapi-managed lifecycle (`js.exportModule` with `init`/`cleanup` hooks). The concurrent-register race they were guarding against has been filed against zapi upstream: ChainSafe/zapi#31. ## Description `src/bls/ThreadPool.zig`: - `err_flag.load(.acquire)` → `.monotonic` in `VerifyMultiWorkItem.exec` and `AggVerifyWorkItem.exec` worker loops. - Setter side (`err_flag.store(true, .release)` on pairing failure) is unchanged — release semantics on the producer side carry no obligation on the consumer to also be `.acquire` when the consumer doesn't depend on the producer's other writes.
1 parent bdf5b67 commit 4b0a1cc

1 file changed

Lines changed: 2 additions & 3 deletions

File tree

src/bls/ThreadPool.zig

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,6 @@ const VerifyMultiWorkItem = struct {
203203
const self: *VerifyMultiWorkItem = @fieldParentPtr("base", base_item);
204204
const job = self.job;
205205

206-
// Each worker gets its own pairing buffer on the stack
207206
var buf: PairingBuf = .{};
208207
var pairing = Pairing.init(&buf.data, true, job.dst);
209208

@@ -213,7 +212,7 @@ const VerifyMultiWorkItem = struct {
213212
while (true) {
214213
const i = job.counter.fetchAdd(1, .monotonic);
215214
if (i >= n_elems) break;
216-
if (job.err_flag.load(.acquire)) break;
215+
if (job.err_flag.load(.monotonic)) break;
217216

218217
did_work = true;
219218

@@ -347,7 +346,7 @@ const AggVerifyWorkItem = struct {
347346
while (true) {
348347
const i = job.counter.fetchAdd(1, .monotonic);
349348
if (i >= job.n_elems) break;
350-
if (job.err_flag.load(.acquire)) break;
349+
if (job.err_flag.load(.monotonic)) break;
351350

352351
did_work = true;
353352

0 commit comments

Comments
 (0)