Commit 7771c87
fix: size the work-stealing deque to its capacity and index it circularly (#393)
The deque (used by the FIFO global request scheduler) declared its backing
store as a fixed inline array `type wrk[DEQUE_MAX_SZ]` (1<<23 entries —
~64 MB for struct sandbox*), and the `size` passed at init was only a soft
occupancy cap. Worse, top/bottom were used as raw, monotonically increasing
array indices with no modulo wrap; since the global scheduler only pushes
(bottom++) and steals (top++), the index marches forward forever and runs
off the end of the array after 1<<23 total requests.
Allocate the backing buffer on the heap at init, sized to the requested
capacity (rounded up to a power of two), and index it circularly via a mask.
The occupancy cap (size - 1) keeps the producer from lapping the consumers,
so masked indices of live elements never collide. This makes memory scale
with the configured capacity (4096 entries instead of 1<<23 -> ~64 MB less
virtual reservation) and removes the eventual out-of-bounds. The atomics and
ordering are unchanged; only the index reads/writes gained `& mask`.
Also add deque_free, have deque_init report allocation failure (checked by
the caller), and make deque.h self-contained (stdbool/stdlib/errno/assert).
Growing the buffer when full is intentionally left out: stealers consume it
lock-free concurrently with the producer, so reallocating underneath an
in-flight steal would be a use-after-free. The deque returns -ENOSPC when
full and the caller applies backpressure.
Verified: builds; FIFO serves resize requests correctly (20/20 byte-
identical); a unit test drives 1,000,007 push/steal pairs through a size-8
ring (indices wrap ~125,000x, live window straddles the boundary 750,000
times) preserving strict FIFO order; virtual footprint drops ~64 MB.
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>1 parent 31c5780 commit 7771c87
2 files changed
Lines changed: 57 additions & 20 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
10 | 16 | | |
11 | 17 | | |
12 | 18 | | |
| |||
18 | 24 | | |
19 | 25 | | |
20 | 26 | | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
21 | 39 | | |
22 | 40 | | |
23 | | - | |
| 41 | + | |
24 | 42 | | |
25 | 43 | | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
26 | 52 | | |
27 | 53 | | |
28 | | - | |
29 | | - | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
30 | 57 | | |
31 | 58 | | |
32 | 59 | | |
33 | 60 | | |
34 | 61 | | |
35 | | - | |
| 62 | + | |
| 63 | + | |
36 | 64 | | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | | - | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | | - | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
46 | 71 | | |
47 | | - | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
48 | 84 | | |
49 | 85 | | |
50 | 86 | | |
| |||
55 | 91 | | |
56 | 92 | | |
57 | 93 | | |
58 | | - | |
| 94 | + | |
59 | 95 | | |
60 | 96 | | |
61 | | - | |
| 97 | + | |
62 | 98 | | |
63 | 99 | | |
64 | 100 | | |
| |||
82 | 118 | | |
83 | 119 | | |
84 | 120 | | |
85 | | - | |
| 121 | + | |
86 | 122 | | |
87 | 123 | | |
88 | 124 | | |
| |||
110 | 146 | | |
111 | 147 | | |
112 | 148 | | |
113 | | - | |
| 149 | + | |
114 | 150 | | |
115 | 151 | | |
116 | 152 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
53 | 53 | | |
54 | 54 | | |
55 | 55 | | |
56 | | - | |
57 | | - | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
58 | 59 | | |
59 | 60 | | |
60 | 61 | | |
| |||
0 commit comments