edith_codecs
Clone
https://git.tahinli.com/Tahinli/edith_codecs.git
0 watching·0 forks·Rust·370.3 MiB·9b67700·Tahinli·3 days ago·4021 commits
| 1 | # lane-stackbox — the by-value stack class in the sibling crates |
| 2 | |
| 3 | **Branch:** `lane-stackbox` (worktree `/home/user/.cache/wt/stackbox`, base `main` d202b8b4) |
| 4 | **Scope:** `ec-opus`, `ec-ac3`, `ec-vp9`. **`ec-av1` untouched** (another lane owns it). |
| 5 | |
| 6 | ## The class |
| 7 | |
| 8 | A large struct returned **by value** from a public constructor is a stack cost, |
| 9 | not a heap cost, and the cost is paid by the **caller**. Constructors nest, so |
| 10 | one value's `size_of` is the *multiplier*, not the cost. |
| 11 | |
| 12 | `crates/ec-av1/src/stack_budget.rs` (merged d202b8b4) fixed and gated this for |
| 13 | `ec-av1`. This lane sweeps the three sibling codecs. **Layout only** — no codec |
| 14 | behaviour, no defaults, no bitstream output changes. |
| 15 | |
| 16 | ## 1. The measured inventory |
| 17 | |
| 18 | Every number is `std::mem::size_of`, measured with a throwaway probe test in |
| 19 | each crate, then re-measured permanently by each crate's new |
| 20 | `the_measured_inventory_is_accurate` gate (the probe was deleted; the gate is |
| 21 | the instrument). One command reproduces the whole table per crate: |
| 22 | |
| 23 | ``` |
| 24 | export CARGO_TARGET_DIR=/home/user/.cache/tgt/stackbox EC_NOMEMGUARD=1 |
| 25 | cd /home/user/.cache/wt/stackbox |
| 26 | cargo test -p <ec-opus|ec-ac3|ec-vp9> --lib -- stack_budget -- --nocapture |
| 27 | ``` |
| 28 | |
| 29 | ### Headline: before → after |
| 30 | |
| 31 | | crate | type | before | after | shrink | boxed fields | |
| 32 | |---|---|---|---|---|---| |
| 33 | | `ec-opus` | `Encoder` | **87,024** | **4,072** | 21.4x | 6× `Option<Box<SilkEncoder\|SilkStereoEncoder>>`, `silk_buf: Box<[u8; N]>` | |
| 34 | | `ec-opus` | `Decoder` | 7,576 | **1,280** | 5.9x | `silk: Box<SilkDecoder>` | |
| 35 | | `ec-ac3` | `Ac3Decoder` | **20,024** | **3,160** | 6.3x | `Core::{exps, bap, coeffs, delay}` | |
| 36 | | `ec-vp9` | `Decoder` | **10,944** | **768** | 14.3x | `frame_ctxs: Box<[FrameContext; 4]>`, `ctx: Box<FrameContext>` | |
| 37 | |
| 38 | The sibling-lane pre-fix figures (87,024 / 20,024 / 10,944) **reproduced |
| 39 | exactly**. All four types are now under the 8,192 B budget, the worst at 49.7%. |
| 40 | |
| 41 | **Correction to the charter's brief:** `silk_buf` is |
| 42 | `1+1+2*2+3*1275 = 3,831` bytes, not 4,088 as the contract stated. `Encoder`'s |
| 43 | pre-fix field breakdown: 59,232 (3× `SilkStereoEncoder` @ 19,744) + 19,944 |
| 44 | (3× `SilkEncoder` @ 6,648) + 3,831 (`silk_buf`) + 2,168 (`TonalityAnalysis`) |
| 45 | + 1,592 (`CeltEncoder`) + ~297 scalars/`Vec`s = 87,024. |
| 46 | |
| 47 | ### Full inventory (printed by the gates, post-fix) |
| 48 | |
| 49 | `ec-opus` — `SilkStereoEncoder` 19744 (crate-private), `SilkEncoder` 6648 |
| 50 | (crate-private), `SilkDecoder` 6304, **`Encoder` 4072**, `TonalityAnalysis` |
| 51 | 2168 (crate-private), `CeltEncoder` 1592 (crate-private), **`Decoder` 1280**, |
| 52 | `CeltDecoder` 1168, `CeltFrameDiag` 424, `SilkDecIndices` 104, |
| 53 | `MultistreamDecoder` 96, `MultistreamEncoder` 88, `RangeEncoder` 80, |
| 54 | `AnalysisInfo` 60, `RangeDecoder` 56, `SilkFrameDiag` 56, `EncSnapshot` 48, |
| 55 | `Packet` 40, `Toc` 3, `Application` 1, `Mode` 1, `Bandwidth` 1. |
| 56 | |
| 57 | `ec-ac3` — **`Ac3Decoder` 3160 (38.6%)**, `Core` 2864 (crate-private), |
| 58 | `Ac3Encoder` 2784 (34.0%), `Imdct` 472, `AudioFrame` 96, `Eac3Bsi` 96, |
| 59 | `EncodeStats` 72, `Bsi` 56, `Mantissas` 56, `Allocation` 56, `FrameInfo` 48, |
| 60 | `DeltaBa` 32, `SyncInfo` 24, `Channel` 12, `EncoderConfig` 12, `Options` 8, |
| 61 | `BitAllocParams` 5, and six 1-byte enums. |
| 62 | |
| 63 | `ec-vp9` — `FrameContext` 2039 (crate-private), **`Decoder` 768 (9.4%)**, |
| 64 | `Picture` 96, `BoolDecoder` 40. |
| 65 | |
| 66 | ## 2. What dominated each type, and what was boxed |
| 67 | |
| 68 | Rule applied: box the field(s) that **dominate**. A field was not boxed to |
| 69 | make a number look better. |
| 70 | |
| 71 | **`ec-opus::Encoder`.** The six SILK slots are 79,176 B = **91%** of the |
| 72 | struct; `silk_buf` is a further 3,831. Boxed as six `Option<Box<..>>` plus |
| 73 | `Box<[u8; MAX_SILK_PACKET_BYTES]>`. `analysis` (2,168) and `celt` (1,592) were |
| 74 | **left inline** — 3,760 combined is already under budget, and a `Box` per |
| 75 | field buys no budget while costing a per-frame indirection on a hot path. |
| 76 | The boxing is a natural fit: only one of the six slots is ever alive at a time |
| 77 | (the mode picks one), so the heap pays for one encoder and the stack for six |
| 78 | pointers. The `Option` already existed for exactly that laziness. |
| 79 | |
| 80 | **`ec-opus::Decoder`.** `silk: SilkDecoder` is 6,304 = **83%** of the struct. |
| 81 | Boxed. This type was *under* budget at 7,576 but at 92.5% of it, and `ec-av1`'s |
| 82 | own docs name "a bound the largest type sits just under is a bound the next |
| 83 | lane will quietly raise" as the failure mode. |
| 84 | |
| 85 | **`ec-ac3::Ac3Decoder`.** 296 B of header plus `Core` at 19,728. Inside `Core`, |
| 86 | four inline arrays are 16,896 = **86%**: `coeffs` 7,168, `delay` 6,144, |
| 87 | `exps` 1,792, `bap` 1,792. All four boxed as `Box<[[T; COEFFS]; N]>`. |
| 88 | Indexing, `&mut self.f[ch]` and `bitalloc::compute(.., &self.exps[ch], &mut |
| 89 | self.bap[ch])` all read through the box — **no call site needed rewriting**. |
| 90 | |
| 91 | **`ec-vp9::Decoder`.** `frame_ctxs: [FrameContext; 4]` is 8,156 = 75%. Boxing |
| 92 | it alone left **2,800 — under budget, but 2,039 of that (72.8%) was the single |
| 93 | inline `ctx` field**, i.e. one field carrying nearly all of the remainder. |
| 94 | `ctx` was boxed too, giving 768. The three whole-array assignment sites needed |
| 95 | `Box::new`; the three `frame_ctxs[..] = self.ctx.clone()` copy-out sites |
| 96 | became `.as_ref().clone()`. |
| 97 | |
| 98 | ## 3. API compatibility — checked BEFORE any edit |
| 99 | |
| 100 | **No public field type changed in any of the three crates.** Every field boxed |
| 101 | is private or `pub(crate)`: `ec-opus::Encoder` and `::Decoder` fields are all |
| 102 | private; `ec-ac3::decode::Core` is `pub(crate)` and `Core` itself is |
| 103 | crate-private; `ec-vp9::Decoder` fields are all private. |
| 104 | |
| 105 | Public method signatures are untouched — `Encoder::new`, `Decoder::new`, |
| 106 | `Ac3Decoder::new`, `Ac3Decoder::with_options`, `vp9::Decoder::new` keep exact |
| 107 | parameter and return types. A consumer that constructs an encoder and calls |
| 108 | `encode` compiles and runs unchanged. The only observable difference is a few |
| 109 | heap allocations at construction that were previously zero; these are not on |
| 110 | the per-frame path and are not part of any contract here. |
| 111 | |
| 112 | `ec-vp9::Picture`'s `pub` fields were **not** touched — 96 B of `Vec` handles. |
| 113 | |
| 114 | ## 4. No output change — before/after, with counts |
| 115 | |
| 116 | Baselines taken on the **unmodified** tree at d202b8b4, before any edit. |
| 117 | |
| 118 | | crate | gate | before | after | |
| 119 | |---|---|---|---| |
| 120 | | `ec-opus` | `--lib` | 36 passed, 0 failed | **42** passed, 0 failed (+6 gate) | |
| 121 | | `ec-opus` | `--test conformance` | 28 passed, 0 failed, 16 ignored | **28 passed, 0 failed, 16 ignored** | |
| 122 | | `ec-ac3` | `--lib` | 36 passed, 0 failed | **43** passed, 0 failed (+7 gate) | |
| 123 | | `ec-ac3` | `--test decode_matrix` | 8 passed, 0 failed | **8 passed**, 0 failed | |
| 124 | | `ec-ac3` | `--test encode_matrix` | 6 passed, 0 failed | **6 passed**, 0 failed | |
| 125 | | `ec-vp9` | `--lib` | 10 passed, 0 failed | **16** passed, 0 failed (+6 gate) | |
| 126 | | `ec-vp9` | `keyframe_exact` | 3 passed | **3 passed** | |
| 127 | | `ec-vp9` | `inter_pixels_exact` | 3 passed | **3 passed** | |
| 128 | | `ec-vp9` | `hbd_exact` | 1 passed | **1 passed** | |
| 129 | | `ec-vp9` | `subsampling_exact` | 3 passed | **3 passed** | |
| 130 | | `ec-vp9` | `odd_dimensions_exact` | 2 passed | **2 passed** | |
| 131 | | `ec-vp9` | `scaledref_exact` | 3 passed | **3 passed** | |
| 132 | | `ec-vp9` | `intraonly_exact` | 1 passed | **1 passed** | |
| 133 | |
| 134 | **Every bit-exactness count is identical before and after.** The only delta in |
| 135 | any line is the `+N` gate tests this lane added to `--lib`. |
| 136 | |
| 137 | `ec-opus --test conformance` is the crate's own bit-exactness gate: it holds |
| 138 | the closed loop range-state-exact against this crate's RFC-vector-verified |
| 139 | decoder. 16 tests are `#[ignore]`d on unmodified `main` and stay ignored. |
| 140 | |
| 141 | **A pre-existing red worth recording:** on unmodified `main` in a bare |
| 142 | worktree, `ec-ac3 encode_matrix::real_fixtures_round_trip…` and |
| 143 | `vp9 keyframe_exact::lossless_64_matches_ffmpeg` both FAIL with a |
| 144 | missing-fixture error (`No such file or directory`), not a decode mismatch. |
| 145 | The worktree ships no `fixtures/` payload; after copying it from the primary |
| 146 | checkout both are green. Noted so a reader does not mistake the pre-edit red |
| 147 | for a regression. |
| 148 | |
| 149 | ## 5. The gates |
| 150 | |
| 151 | Three new `stack_budget.rs` modules, each `#![cfg(test)]`, each modelled on |
| 152 | `crates/ec-av1/src/stack_budget.rs` and each with `STACK_BUDGET = 8192` |
| 153 | (the same number, derived the same way: an unconfigured caller hands a crate at |
| 154 | least 2 MiB; deepest public by-value chain × worst public return × 8 must fit). |
| 155 | |
| 156 | Per crate, seven tests: |
| 157 | |
| 158 | 1. `the_measured_inventory_is_accurate` — the `BY_VALUE_TYPES` table vs live |
| 159 | `size_of`, keyed **by name** (positional zip would assert the wrong type's |
| 160 | size when two types share one). Prints the sorted table. |
| 161 | 2. `every_by_value_type_fits_the_stack_budget` — the bound asserted **by name** |
| 162 | per row, plus the chain×worst×8 derivation re-asserted so neither side rots. |
| 163 | 3. a **box pin** — a source scan over the real field declarations with a |
| 164 | **negative control** that the unboxed spelling is absent (otherwise the |
| 165 | scan could be matching a doc comment). |
| 166 | - opus: `the_crate_private_silk_encoders_stay_behind_a_box` |
| 167 | - ac3: `the_crate_private_core_stays_behind_a_box` |
| 168 | - vp9: `the_frame_contexts_stay_behind_a_box` |
| 169 | 4. **capability arm** — really constructs the heavy types on a deliberately |
| 170 | tight thread, so the old layout **aborts the process** instead of costing |
| 171 | invisible margin. Overridable via `EC_{OPUS,AC3,VP9}_TIGHT_STACK_BYTES`. |
| 172 | 5. `every_by_value_public_return_is_in_the_inventory` — source scan |
| 173 | re-deriving by-value `pub fn` returns from the crate, so a lane adding a |
| 174 | 40 KB struct to a `pub fn` reds until it is measured and listed. |
| 175 | 6. `the_inventory_scan_is_not_vacuous` — the **synthetic control** on the |
| 176 | matcher (5 positive + 8 thin spellings), sharing the matcher with the sweep. |
| 177 | 7. `every_by_value_module_is_scanned` — the scanned file list is **derived** |
| 178 | from `lib.rs`, so a new module reds. |
| 179 | |
| 180 | Measured tight-stack thresholds (one process per point): |
| 181 | |
| 182 | | crate | gate | deepest measured need | margin | |
| 183 | |---|---|---|---| |
| 184 | | `ec-opus` | 256 KiB | pre-fix layout *completed* at 544 KiB, overflowed at 528 KiB | current layout ok at 48 KiB, overflow at 32 KiB → 5.3x | |
| 185 | | `ec-ac3` | 1 MiB | ~80 KiB (ok 81,920 / overflow 77,824) | 12.8x | |
| 186 | | `ec-vp9` | 256 KiB | constructs `Decoder::new` fine | — | |
| 187 | |
| 188 | The `ec-opus` gate is deliberately at **256 KiB, not 1 MiB**: the pre-fix |
| 189 | layout *passes* a 1 MiB gate, so a 1 MiB gate would have been a formality. At |
| 190 | 256 KiB the pre-fix layout aborts the process and the current one has 5.3x |
| 191 | margin. |
| 192 | |
| 193 | ## 6. Mutation proofs — the gate bites |
| 194 | |
| 195 | Each crate: unbox one field, show the gate red **by name**, restore |
| 196 | **byte-exact** (sha256 verified), re-run green. |
| 197 | |
| 198 | **`ec-opus`** — unboxed `silk_buf` back to an inline array. **2 tests red, both |
| 199 | naming it:** |
| 200 | |
| 201 | ``` |
| 202 | the_crate_private_silk_encoders_stay_behind_a_box ... FAILED |
| 203 | encoder.rs::Encoder::silk_buf is no longer declared |
| 204 | `silk_buf: Box<[u8; MAX_SILK_PACKET_BYTES]>`. An inline SILK encoder or |
| 205 | scratch buffer there puts Encoder::silk_buf's bytes back on every caller's |
| 206 | stack -- that is how Encoder was 87024 bytes ... |
| 207 | the_measured_inventory_is_accurate ... FAILED |
| 208 | Encoder is 7896 bytes, the inventory records 4072 |
| 209 | ``` |
| 210 | |
| 211 | **`ec-ac3`** — unboxed `coeffs`. **3 tests red, each named** (inventory 10,320 |
| 212 | vs 3,160; budget 1.26x over; box pin). The negative control was separately |
| 213 | proven non-vacuous: with `coeffs` inline but both box spellings injected **as |
| 214 | comments**, all positive pins pass and only the negative control fires — exactly |
| 215 | the comment-masking failure it exists for. |
| 216 | |
| 217 | **`ec-vp9`** — I ran this one myself, after adding the `ctx` pin. Unboxed |
| 218 | `ctx`. **2 tests red, both naming it:** |
| 219 | |
| 220 | ``` |
| 221 | the_frame_contexts_stay_behind_a_box ... FAILED |
| 222 | decode.rs::the ctx field is no longer declared `ctx: Box<FrameContext>`. |
| 223 | An inline `FrameContext` there puts 2039 bytes back into every caller's |
| 224 | stack -- five of them were 10195 bytes, which is how `Decoder` was 10944 |
| 225 | bytes and 1.34x over the by-value budget before the boxes. |
| 226 | the_measured_inventory_is_accurate ... FAILED |
| 227 | Decoder is 2800 bytes, the inventory records 768 |
| 228 | ``` |
| 229 | |
| 230 | All three restores verified by sha256 identical before and after, plus a green |
| 231 | re-run (`ec-opus` conformance re-run too: 28 passed, 0 failed). |
| 232 | |
| 233 | ## 7. Honest notes / what was NOT done |
| 234 | |
| 235 | - **`ec-av1` not touched.** No read-modify-write; only read as the precedent. |
| 236 | - **`.cargo/config.toml`'s `RUST_MIN_STACK` not lowered.** The point of the |
| 237 | lane is that a consumer who never sees that file is safe. |
| 238 | - **No codec behaviour, default, or bitstream output changed.** The diff is |
| 239 | field types, `Box::new` at construction, `&mut f` → `&mut f[..]` at four |
| 240 | `silk_buf` borrow sites, and three `.clone()` → `.as_ref().clone()` at vp9 |
| 241 | copy-out sites. |
| 242 | - **`ec-vp9`'s inventory is genuinely small** — only 3 public by-value types, |
| 243 | because 11 of its 13 `pub fn` lines return a primitive, `Self` or `()` and |
| 244 | every other module's API is `pub(crate)` behind `Decoder`. The gate's floor |
| 245 | is set to the real count (3) rather than padded to `ec-av1`'s 10; the assert |
| 246 | is an exact equality whose failure message states the real number. Padding it |
| 247 | would have required inventing types, so it was not padded. |
| 248 | - **`ec-vp9`'s matcher unwraps `Result`/`Option` in a loop**, not once: its one |
| 249 | by-value public return is `Result<Option<Picture>>`, which a single unwrap |
| 250 | reads as the non-type `"Option"`. A synthetic control case pins the |
| 251 | double-wrapped spelling. |
| 252 | - **Not done, and named:** the `ec-opus` sweep found `CeltEncoder` (1,592), |
| 253 | `TonalityAnalysis` (2,168) and `CeltDecoder` (1,168) still inline. All are |
| 254 | under budget, so boxing them is a future move, not this lane's. |
| 255 | - **Full-suite validation is the integrator's.** Only scoped per-crate |
| 256 | `--lib`, the named integration exactness tests, and the new gate module were |
| 257 | run here; concurrent lanes made a project-wide run unsafe mid-flight. |
| 258 | |
| 259 | ## 8. Files |
| 260 | |
| 261 | ``` |
| 262 | crates/ec-ac3/src/decode.rs | 25 +++++++++++----------- (boxing) |
| 263 | crates/ec-ac3/src/lib.rs | 1 + (mod decl) |
| 264 | crates/ec-opus/src/encoder.rs | 61 +++++++++++++++++-------- (boxing) |
| 265 | crates/ec-opus/src/lib.rs | 11 +++-- (boxing + mod decl) |
| 266 | crates/ec-vp9/src/decode.rs | 36 ++++++++++++++----------- (boxing) |
| 267 | crates/ec-vp9/src/lib.rs | 1 + (mod decl) |
| 268 | crates/ec-ac3/src/stack_budget.rs | new, 995 lines, 8 tests |
| 269 | crates/ec-opus/src/stack_budget.rs | new, 949 lines, 7 tests |
| 270 | crates/ec-vp9/src/stack_budget.rs | new, 725 lines, 7 tests |
| 271 | lanes/stackbox.report.md | this file |
| 272 | ``` |
| 273 | |
| 274 | No commit, no push, no merge, no rustfmt — per the lane's constraints. |