Commit e6c3217
committed
heap: 8 power-of-two classes, XL retention, footprint shrink policy
CLASSES
17 binned classes collapsed to 8 powers of two (16, 32, 64, 128,
256, 512, 1024, 2048). Every class divides HEAP_PAGE_SIZE
exactly. heap_class_idx_for becomes an 8-step ladder. The L tier
(768 / 1024 / 1536 / 2048) loses its non-power-of-two bins; the
alignment-waste regression they kept covering with intra-page
shims is now zero.
XL RETENTION
Replaced the xl[] open-addressed hash with two parallel flat
arrays:
xl_in_use[] -- live XL descriptors,
xl_freed[] -- retained mappings (freed but not yet returned).
Alloc looks for a matching `os_pages` entry in xl_freed (LIFO
scan) and only mmaps on miss. Free swap-removes from xl_in_use
and swap-pushes onto xl_freed. Linear scan on free is O(N_live);
for typical XL counts (< 100) it is one contiguous read per
probe, faster than the Fibonacci-hash lookup it replaced.
BM_AllocFreePair 4-64 KiB drops from ~1185 ns to ~19 ns
(validate-full) and ~11 ns (validate-fast). The plateau is now
retention-pool reuse, not mmap/munmap syscall pairs.
FOOTPRINT SHRINK POLICY
Tracks retention_bytes (sum of recycle + xl_freed mappings).
When footprint_bytes >= HEAP_FOOTPRINT_SHRINK_THRESHOLD (1 MiB)
AND retention_bytes > footprint_bytes / 2, the deallocate path
unmaps xl_freed entries (then recycle entries) until footprint
drops to 75% of its old value. Fast check is FORCE_INLINE'd at
the call site so the common case (no shrink needed) is two loads
+ two compares.
CACHE-LINE ALIGNMENT
`_Alignas(64)` on HeapPage's first member (portable C11, gcc /
clang / clang-cl / MSVC) so each pages[] hash bucket fits on one
cache line. Without this the natural 56-byte struct leaves bucket
tails bleeding into the next line, doubling probe cost on the
classed free/grow hot paths.
KEEP-ONE-WARM GUARD
The pre-rewrite reclaim path had a class_count[cls] > 1 check
that kept the last warm page parked. That check was dropped in
the initial commit and BM_AllocFreePair regressed by 12 ns
because every iteration paid a recycle-pop + HeapPage descriptor
re-stamp (two 32-byte AVX moves). Reinstated using just the warm
list -- if this page is the warm-list head AND has no next link,
reclaiming would leave the class with no warm page. Costs two
loads and a compare; no new state.
DROPPED
- class_count[HEAP_NUM_CLASSES]
- HeapPage._pad0 / _pad1[12]
- heap_xl_hash_* helpers (Fibonacci hash + back-shift delete +
resize, all keyed on the user pointer)
CONVENTION
Added two rules to CODING-CONVENTIONS.md macro hygiene:
- Don't pad structs by hand; trust the compiler / ABI / standard.
__attribute__((aligned)) is permitted only with a documented
hardware or wire-format constraint forcing the choice
(HeapPage's cache-line alignment is the canonical example).
- Over-engineering is not entertained; simplest design wins.
SIDE-CHANNEL CLEANUP (folded reviews)
- Pdb: 3-field hand-rolled name_pool descriptor + Str-internals
reach collapsed into one `Str name_pool;` field.
- Allocator.Heap.c: stale "sub-bin" terminology replaced with
"size class".
- Benchmark/Source/Fragmentation.cpp: comment reflected the new
class set ("six of the eight power-of-two classes").
- README + Heap.h docs: every reference to the xl[] hash or the
17-class S/M/L taxonomy rewritten.
BENCHMARK NUMBERS (BM_AllocFreePair, validate-full / validate-fast):
| size | pre-rewrite | post-rewrite |
|-------|-------------|--------------|
| 16 B | 15.0 / 14.2 | 14.8 / 14.3 |
| 256 B | 15.2 / 14.9 | 15.0 / 14.5 |
| 4 KiB | 1185 / 1155 | 19.1 / 10.6 |
| 64 KiB| 1193 / 1164 | 19.2 / 10.6 |
Small classes recover to baseline within noise; XL 4-64 KiB at
~63x (validate-full) / ~110x (validate-fast) faster.
TESTING
- Linux gcc 15.2 default build: 104/104 OK.
- Linux gcc 15.2 -Db_sanitize=address,undefined: 104/104 OK.
- macOS arm64 (Apple clang via SSH test box): 99/99 OK.1 parent aab155c commit e6c3217
10 files changed
Lines changed: 453 additions & 429 deletions
File tree
- Benchmark
- Source
- Include/Misra
- Parsers
- Std/Allocator
- Source/Misra
- Parsers
- Std/Allocator
- Tests
- Json
- Std
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
13 | | - | |
14 | | - | |
15 | | - | |
16 | | - | |
17 | | - | |
18 | | - | |
19 | | - | |
20 | | - | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
| |||
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
30 | | - | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | | - | |
35 | | - | |
36 | | - | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
37 | 37 | | |
38 | 38 | | |
39 | 39 | | |
| |||
43 | 43 | | |
44 | 44 | | |
45 | 45 | | |
46 | | - | |
47 | | - | |
48 | | - | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
49 | 49 | | |
50 | 50 | | |
51 | 51 | | |
| |||
55 | 55 | | |
56 | 56 | | |
57 | 57 | | |
58 | | - | |
59 | | - | |
60 | | - | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
61 | 61 | | |
62 | 62 | | |
63 | 63 | | |
| |||
67 | 67 | | |
68 | 68 | | |
69 | 69 | | |
70 | | - | |
| 70 | + | |
71 | 71 | | |
72 | 72 | | |
73 | 73 | | |
| |||
77 | 77 | | |
78 | 78 | | |
79 | 79 | | |
80 | | - | |
| 80 | + | |
81 | 81 | | |
82 | 82 | | |
83 | 83 | | |
| |||
88 | 88 | | |
89 | 89 | | |
90 | 90 | | |
91 | | - | |
92 | | - | |
| 91 | + | |
| 92 | + | |
93 | 93 | | |
94 | 94 | | |
95 | 95 | | |
| |||
99 | 99 | | |
100 | 100 | | |
101 | 101 | | |
102 | | - | |
103 | | - | |
104 | | - | |
105 | | - | |
106 | | - | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
107 | 107 | | |
108 | 108 | | |
109 | 109 | | |
| |||
143 | 143 | | |
144 | 144 | | |
145 | 145 | | |
146 | | - | |
147 | | - | |
| 146 | + | |
| 147 | + | |
148 | 148 | | |
149 | 149 | | |
150 | 150 | | |
151 | | - | |
| 151 | + | |
152 | 152 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
156 | 156 | | |
157 | 157 | | |
158 | 158 | | |
159 | | - | |
160 | | - | |
161 | | - | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
162 | 163 | | |
163 | 164 | | |
164 | 165 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
565 | 565 | | |
566 | 566 | | |
567 | 567 | | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
568 | 585 | | |
569 | 586 | | |
570 | 587 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
| 30 | + | |
30 | 31 | | |
31 | 32 | | |
32 | 33 | | |
| |||
103 | 104 | | |
104 | 105 | | |
105 | 106 | | |
106 | | - | |
107 | | - | |
108 | | - | |
| 107 | + | |
109 | 108 | | |
110 | 109 | | |
111 | 110 | | |
| |||
0 commit comments