Reserve context message storage once
Reserve the known final context message count before copying. The PR is 82 added / 6 removed lines across three files: the production reservation, test wiring, and one shrinking allocation/capacity property.
Lower bound: exactly one vector allocation for a nonempty concatenation, zero for empty, capacity equal to the required message count, plus unavoidable owned payload clones. Every test message has distinct nonempty text (first-i, second-i, or the inserted user), so equality with the independent original branch detects ordering and wrong-slice copies. The property accounts for one required string allocation per message separately. Each generated case also includes empty output and an order-sensitive growing case, even after shrinking. Independent negative controls reject the original clone-and-grow allocation count and a vector reserved at twice the required capacity. All overflow checks, metered charges and limits are preserved; the metered body is unchanged.
Before/after release measurements retained from the reviewed production change (no new performance claim):
| 65 messages, 1 KiB payload each | Before | After |
|---|---|---|
| Lane median, no subscriber | 3.933 µs | 3.273 µs |
| Independent review median | 4.256 µs | 3.616 µs |
| Allocations | 68 | 66 |
| Allocated bytes | 132,864 | 85,800 |
| Peak live bytes | 104,448 | 85,800 |
The absolute saving is small; no end-to-end improvement is claimed. Prior tracing measurements were 3.933 → 3.262 µs; accounting uses the unchanged metered body.
Validation:
- One bs.ps1 batch: compaction helpers (including the 128-case shrinking property) 3 passed in 0.09 s; existing compaction/proof tests 3 passed in 0.01 s; pipeline tests 4 passed in 0.01 s.
- Independently deleted each actual assertion and rebuilt the changed test: deleting the allocation assertion failed at the old-growth negative control; deleting the capacity assertion failed at the twice-capacity negative control. Each shrank to first=0, second=0, insert=false and ran in <0.01 s. Normal source restored afterwards.
- Windows lb.ps1 cargo check -p acyclic-harness --features filesystem --lib --tests --locked passed in 21.94 s, excluding slot wait.
- Linux normal/allocation-mutant/capacity-mutant compilation: 91.78 / 44.96 / 51.86 s, excluded from suite times. rustfmt and git diff --check passed.
Decisions: keep one shrinking allocation/capacity property and the independent original implementation, with no permanent timing scaffold. Payloads carry slice and index identity while their unavoidable allocation costs are counted explicitly.