Commit e0c3592
authored
feat(avm): lock-free sharded TraceContainer (#24300)
## Summary
Port of AztecProtocol/aztec-packages-private#312 (by @fcarreiro) to
public `next`.
Replaces the AVM `TraceContainer`'s per-column `std::shared_mutex` +
`unordered_flat_map` storage with a lock-free, sharded design. Each
column is a fixed-size table of atomic pointers to dense, fixed-size row
shards; the hot `get`/`set` path is a single atomic load with no lock,
and shards are created at most once via compare-and-swap. The public API
is unchanged.
Motivation: under parallel tracegen (`parallel_for` over jobs), libc++'s
`std::shared_mutex::lock_shared()` is backed by a real mutex, so the
per-column lock serialized concurrent writers. Profiling showed lock
machinery dominating tracegen and poor scaling at 16 threads.
## Changes
- Column storage is now a fixed-size
`std::array<std::atomic<ColumnInterval*>, NUM_SHARDS>`, indexed by `row
/ INTERVAL_SIZE`, held inline and freed in the destructor.
- Each shard stores rows in a dense, value-initialized `std::array<FF,
INTERVAL_SIZE>` (zero == absent). Distinct rows are distinct elements,
so concurrent writers to different rows of a column never race or
serialize — including when two chunks meet at a shard boundary.
- Shard creation is lock-free via `compare_exchange_strong`. The
per-column lock is gone; the bulk `reserve_column` / `clear_column` /
`invert_column` paths are documented as not thread-safe (they run after
the parallel fill phase joins).
- In-shard offset uses `row % INTERVAL_SIZE` (correct for any
`INTERVAL_SIZE`; strength-reduces to the same instruction as a bitmask
for the power-of-two value).
- Documented the thread-safety contract: concurrent `get`/`set` is safe
as long as no two threads touch the same `(column, row)` with at least
one writer, and reads are barrier-separated from writes.
With `INTERVAL_SIZE = 2^11` and `MAX_AVM_TRACE_SIZE = 2^21`, `NUM_SHARDS
= 1024` and `INTERVAL_SIZE` divides the max trace size evenly.
## Performance & memory (from the source PR)
Measured with `HARDWARE_CONCURRENCY=16`, RelWithAssert `bb-avm`.
**Bulk test** (`avm_bulk.test.ts`), source branch vs `next`:
| | `next` (shared_mutex + hash-map) | this change (lock-free dense) |
delta |
|---|---:|---:|---:|
| Tracegen (all) | ~3.0 s | ~0.78 s | **≈ 4× faster** |
| Peak `bb-avm` process memory | ~4.1 GiB | ~3.2–3.4 GiB | **~0.8–0.9
GiB lower (~20%)** |
**Opcode spammer** (`avm_opcode_spam.test.ts`, proving skipped, 70
opcode cases), aggregate tracegen: ~878.6 s → ~230.2 s (**≈ 3.8×
faster**).
The lower memory comes from the trace being top-dense: a dense array
stores one `FF` per cell, versus the hash map's per-entry key + bucket +
load-factor overhead.
## Port notes
- The two changed files
(`barretenberg/cpp/src/barretenberg/vm2/tracegen/trace_container.{cpp,hpp}`)
were byte-identical between public `next` and the source PR's base, so
the change applies as-is (+186/−54, identical to the source PR).
- Verified locally: the changed translation unit compiles against `next`
(`cmake --preset default -DAVM=ON`, built
`vm2_objects/.../trace_container.cpp.o`).
Source: AztecProtocol/aztec-packages-private#312 · Part of Linear
AVM-200.
---
*Created by
[claudebox](https://claudebox.work/v2/sessions/7ad6d36a577e36fd) ·
group: `slackbot`*2 files changed
Lines changed: 186 additions & 54 deletions
Lines changed: 96 additions & 34 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
4 | | - | |
| 4 | + | |
5 | 5 | | |
| 6 | + | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
| |||
23 | 24 | | |
24 | 25 | | |
25 | 26 | | |
26 | | - | |
27 | | - | |
28 | | - | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
29 | 36 | | |
30 | 37 | | |
31 | 38 | | |
| |||
36 | 43 | | |
37 | 44 | | |
38 | 45 | | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
39 | 65 | | |
40 | 66 | | |
41 | 67 | | |
42 | | - | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
43 | 72 | | |
44 | | - | |
45 | | - | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
46 | 77 | | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
52 | 82 | | |
53 | 83 | | |
54 | 84 | | |
| |||
62 | 92 | | |
63 | 93 | | |
64 | 94 | | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
65 | 98 | | |
66 | | - | |
67 | | - | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
68 | 105 | | |
69 | 106 | | |
70 | 107 | | |
71 | 108 | | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
72 | 113 | | |
73 | | - | |
74 | | - | |
75 | | - | |
76 | | - | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | | - | |
81 | | - | |
82 | | - | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
83 | 128 | | |
84 | 129 | | |
85 | 130 | | |
| |||
103 | 148 | | |
104 | 149 | | |
105 | 150 | | |
106 | | - | |
107 | | - | |
108 | | - | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
109 | 163 | | |
110 | 164 | | |
111 | 165 | | |
| |||
120 | 174 | | |
121 | 175 | | |
122 | 176 | | |
123 | | - | |
124 | | - | |
125 | | - | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
126 | 188 | | |
127 | 189 | | |
128 | 190 | | |
129 | 191 | | |
130 | 192 | | |
131 | 193 | | |
| 194 | + | |
132 | 195 | | |
133 | | - | |
134 | | - | |
135 | | - | |
136 | | - | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
137 | 199 | | |
138 | 200 | | |
139 | 201 | | |
Lines changed: 90 additions & 20 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
5 | 6 | | |
| 7 | + | |
6 | 8 | | |
7 | 9 | | |
8 | | - | |
9 | 10 | | |
10 | | - | |
11 | 11 | | |
| 12 | + | |
12 | 13 | | |
13 | 14 | | |
| 15 | + | |
14 | 16 | | |
15 | 17 | | |
16 | 18 | | |
| |||
19 | 21 | | |
20 | 22 | | |
21 | 23 | | |
22 | | - | |
23 | | - | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
24 | 52 | | |
25 | 53 | | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
26 | 66 | | |
27 | 67 | | |
28 | 68 | | |
| |||
42 | 82 | | |
43 | 83 | | |
44 | 84 | | |
45 | | - | |
| 85 | + | |
46 | 86 | | |
47 | 87 | | |
48 | 88 | | |
| |||
53 | 93 | | |
54 | 94 | | |
55 | 95 | | |
56 | | - | |
| 96 | + | |
57 | 97 | | |
58 | 98 | | |
59 | | - | |
| 99 | + | |
60 | 100 | | |
61 | 101 | | |
62 | 102 | | |
63 | | - | |
64 | | - | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
65 | 128 | | |
66 | | - | |
67 | | - | |
68 | | - | |
69 | | - | |
70 | | - | |
71 | | - | |
72 | | - | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
73 | 141 | | |
74 | | - | |
75 | | - | |
76 | | - | |
77 | | - | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
78 | 145 | | |
79 | 146 | | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
80 | 150 | | |
81 | 151 | | |
82 | 152 | | |
| |||
0 commit comments