edith_codecs
Clone
https://git.tahinli.com/Tahinli/edith_codecs.git
0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits
| 1 | |
| 2 | //! A measured bound on the crate's BY-VALUE types, and a gate that bites. |
| 3 | //! |
| 4 | //! # The class |
| 5 | //! |
| 6 | //! A large struct returned BY VALUE from a public constructor is a stack |
| 7 | //! cost, not a heap cost, and the cost is paid by the CALLER. The |
| 8 | //! constructors nest, and in an unoptimized build each level materialises |
| 9 | //! its whole `Result<T>` return slot before the inner call returns. So one |
| 10 | //! value's `size_of` is the MULTIPLIER, and the chain length is the other |
| 11 | //! factor. |
| 12 | //! |
| 13 | //! That is not hypothetical. `Decoder` was **10,944** bytes by value -- |
| 14 | //! five INLINE `FrameContext` slots (2,039 bytes each: the `frame_ctxs` |
| 15 | //! array of four, plus the `ctx` in force) -- and 10,944 is **1.34x** this |
| 16 | //! crate's 8 KiB by-value budget. Boxing both fields |
| 17 | //! (`frame_ctxs: Box<[FrameContext; 4]>` and `ctx: Box<FrameContext>`) took |
| 18 | //! it to **768**, and [`the_frame_contexts_stay_behind_a_box`] is the pin |
| 19 | //! that keeps it there. |
| 20 | //! |
| 21 | //! # Why this module exists |
| 22 | //! |
| 23 | //! One boxed field is one measurement. This module is the sweep: |
| 24 | //! |
| 25 | //! 1. [`BY_VALUE_TYPES`] is the inventory -- every type in this crate a |
| 26 | //! public `fn` can return by value, with a measured `size_of` recorded |
| 27 | //! as of this commit. [`the_measured_inventory_is_accurate`] fails if a |
| 28 | //! type's size moves, so the table cannot rot into stale small numbers. |
| 29 | //! 2. [`STACK_BUDGET`] is the bound, and |
| 30 | //! [`every_by_value_type_fits_the_stack_budget`] asserts it BY NAME for |
| 31 | //! every public row, so a regression reds naming the type. |
| 32 | //! 3. [`the_frame_contexts_stay_behind_a_box`] pins the boxed field, with |
| 33 | //! a negative control so the pin cannot be matching a comment. |
| 34 | //! 4. [`the_deepest_by_value_constructors_fit_on_a_tight_stack`] is the |
| 35 | //! capability arm: it really constructs `Decoder` on a deliberately |
| 36 | //! tight thread and asserts real post-construction state, pinning the |
| 37 | //! measured live stack of the by-value return path. At 256 KiB it does |
| 38 | //! NOT red the unboxing mutation -- a 10,944-byte struct is not that |
| 39 | //! expensive; the budget assert and the box pin are what red, BY NAME. |
| 40 | //! The arm's own docs carry the measured threshold table. |
| 41 | //! 5. [`every_by_value_public_return_is_in_the_inventory`] re-derives the |
| 42 | //! set of by-value public returns from the crate source. |
| 43 | //! [`the_inventory_scan_is_not_vacuous`] is its control. |
| 44 | //! |
| 45 | //! # The bound, and why this number |
| 46 | //! |
| 47 | //! [`STACK_BUDGET`] = 8192 bytes (8 KiB) -- the SAME number as ec-av1's, |
| 48 | //! deliberately, so one bound covers the by-value class across the codec |
| 49 | //! crates rather than each crate calibrating its own. |
| 50 | //! |
| 51 | //! **The quantity being bounded.** Not `size_of` in the abstract, but |
| 52 | //! *what a caller can be made to owe*: the size of one by-value return, |
| 53 | //! multiplied by how many public levels hold a copy at once. Three inputs: |
| 54 | //! 1. **The smallest stack a caller can hand this crate is 2 MiB.** That |
| 55 | //! is libtest's default test thread and what `std::thread::Builder` |
| 56 | //! hands a caller who does not ask for a size. A library consumer gets |
| 57 | //! no `.cargo/config.toml` -- and this repo's sets |
| 58 | //! `RUST_MIN_STACK = 67108864`, which HIDES the problem from this |
| 59 | //! workspace's own runs. The bound is therefore derived against the |
| 60 | //! 2 MiB an unconfigured caller really has. |
| 61 | //! 2. **The deepest PUBLIC by-value chain is 3** |
| 62 | //! ([`DEEPEST_PUBLIC_CHAIN`]): `stream::decode_stream` (stream.rs:74) |
| 63 | //! -> `stream::decode_stream_with` (stream.rs:88) -> |
| 64 | //! `Decoder::decode` (decode.rs:238), after which the remaining frames |
| 65 | //! (`decode_one`, `decode_keyframe`, `decode_inter`) are private. The |
| 66 | //! test asserts `DEEPEST_PUBLIC_CHAIN * worst * 8 <= 2 MiB` -- the |
| 67 | //! by-value return slots may claim at most an EIGHTH of a caller's |
| 68 | //! stack, leaving the rest for the decode walk, which is the thing that |
| 69 | //! actually needs it. |
| 70 | //! 3. **The budget applies only to types a `pub fn` returns.** The |
| 71 | //! crate-private 2,039-byte `FrameContext` is measured and listed but |
| 72 | //! NOT bounded, because no caller can reach it by value -- every field |
| 73 | //! holding one is a `Box`, pinned by |
| 74 | //! [`the_frame_contexts_stay_behind_a_box`]. Bounding it would |
| 75 | //! calibrate the number to something unreachable. |
| 76 | //! |
| 77 | //! **Where the number sits.** At 8 KiB with the worst public return at |
| 78 | //! 768 bytes ([`Decoder`]), the worst a caller owes on return slots |
| 79 | //! alone is `3 x 768 = 2,304` bytes -- 0.11% of a 2 MiB thread, and the |
| 80 | //! budget itself is 10.7x the largest live by-value type. Both directions |
| 81 | //! have room, which is the point: a bound the largest type sits just |
| 82 | //! under is a bound the next lane will quietly raise. |
| 83 | //! |
| 84 | //! **What it catches.** The pre-fix `Decoder` (10,944 B) is **1.34x over** |
| 85 | //! -- the SMALLEST over-budget case of the three crates this sweep has |
| 86 | //! measured (ec-av1's pre-fix `Av1Encoder` was 17.1x over, ec-ac3's |
| 87 | //! pre-fix `Ac3Decoder` 2.43x). It still reds, and by a factor rather than |
| 88 | //! a hair, which is all a bound has to do. |
| 89 | //! |
| 90 | //! # This crate's inventory is SMALL, and that is a measurement |
| 91 | //! |
| 92 | //! ec-av1's floor for bound-checked public rows is 10. **ec-vp9 genuinely |
| 93 | //! has 3** ([`Decoder`], [`Picture`], [`BoolDecoder`]) and the floor is |
| 94 | //! lowered to 3 rather than padded with types this crate does not have. |
| 95 | //! The reason is structural, not accidental: every other module here |
| 96 | //! (`inter`, `mc`, `modes`, `tokens`, `transform`, `loopfilter`, `intra`, |
| 97 | //! `header`, `tables`) exposes ZERO `pub fn` -- its API is |
| 98 | //! `pub(crate)`, reached through `Decoder`. There are 13 `pub fn` lines |
| 99 | //! in the whole crate, 11 of which return a primitive, a `Self`, or |
| 100 | //! `()`. |
| 101 | //! |
| 102 | //! The sweep is still worth having at that size, and the reason is |
| 103 | //! direction of travel: the scan found exactly ONE by-value public return |
| 104 | //! name (`Picture`, from `Decoder::decode`), because `Decoder::new` |
| 105 | //! returns `Self` and `BoolDecoder::new` returns `Result<Self>`, both of |
| 106 | //! which the matcher exempts. A tiny inventory is a floor to grow into, |
| 107 | //! and [`every_by_value_public_return_is_in_the_inventory`] is what makes |
| 108 | //! a new 40 KB by-value return impossible to add unnoticed -- which is the |
| 109 | //! only direction that can actually bite here. |
| 110 | //! |
| 111 | //! # What it does not claim |
| 112 | //! |
| 113 | //! `size_of` is layout, not stack depth. The residual is why |
| 114 | //! [`the_deepest_by_value_constructors_fit_on_a_tight_stack`] constructs |
| 115 | //! for real instead of trusting the arithmetic. And note |
| 116 | //! [`the_frame_contexts_stay_behind_a_box`] pins ONE field: `Decoder::ctx` |
| 117 | //! (decode.rs:129) is still an INLINE `FrameContext`, and at 2,039 bytes |
| 118 | //! it is 72.8% of the 2,800 a caller pays today. Boxing that too is the |
| 119 | //! obvious next move in this class; this module only records the |
| 120 | //! measurement. |
| 121 | |
| 122 | use crateBoolDecoder; |
| 123 | use crate; |
| 124 | |
| 125 | /// The per-type ceiling for a type a CALLER can be made to hold by value, in |
| 126 | /// bytes. See the module docs for the derivation; it is two-sided, and both |
| 127 | /// sides are measured. |
| 128 | pub const STACK_BUDGET: usize = 8192; |
| 129 | |
| 130 | /// The deepest PUBLIC by-value chain in this crate, counted from the source: |
| 131 | /// `stream::decode_stream` (stream.rs:74) -> `stream::decode_stream_with` |
| 132 | /// (stream.rs:88) -> `Decoder::decode` (decode.rs:238) is three public |
| 133 | /// levels, after which `decode_one` / `decode_keyframe` / `decode_inter` |
| 134 | /// are private. The budget is checked against three. |
| 135 | /// |
| 136 | /// The by-value return slots IN that chain are all small |
| 137 | /// (`Result<Vec<Picture>>`, `Result<()>`, `Result<Option<Picture>>`); the |
| 138 | /// 2,800-byte `Decoder` is a by-value LOCAL in `decode_stream_with` that |
| 139 | /// stays live across the inner `decode` calls. Multiplying the chain by the |
| 140 | /// crate's worst public return is the conservative direction, and is what |
| 141 | /// [`every_by_value_type_fits_the_stack_budget`] asserts. |
| 142 | |
| 143 | const DEEPEST_PUBLIC_CHAIN: usize = 3; |
| 144 | |
| 145 | /// Every type in this crate a public `fn` can return BY VALUE, with the |
| 146 | /// `size_of` measured at the commit that wrote this table. `None` means "not |
| 147 | /// yet measured", which [`the_measured_inventory_is_accurate`] rejects. |
| 148 | /// |
| 149 | /// The third column names the return path, so a reader can see why a type is |
| 150 | /// here rather than a coincidence of the type system. |
| 151 | pub const BY_VALUE_TYPES: & = & |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | ; |
| 203 | |
| 204 | /// The live `size_of` for every inventoried type, keyed BY NAME. |
| 205 | /// |
| 206 | /// Keyed rather than positional on purpose: a row inserted into |
| 207 | /// [`BY_VALUE_TYPES`] without a matching insert here would zip cleanly and |
| 208 | /// assert the wrong type's size against the wrong recorded one. A name key |
| 209 | /// makes that mistake loud instead. |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | /// The stack [`the_deepest_by_value_constructors_fit_on_a_tight_stack`] |
| 225 | /// constructs on. |
| 226 | /// |
| 227 | /// 256 KiB: an eighth of what an unconfigured caller hands this crate, and |
| 228 | /// far above what a 2,800-byte by-value `Decoder` needs in a debug build |
| 229 | /// (the threshold is swept with `EC_VP9_TIGHT_STACK_BYTES`). |
| 230 | const TIGHT_STACK: usize = 256 * 1024; |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | |
| 276 | |
| 277 | |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | |
| 289 | |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | |
| 308 | |
| 309 | |
| 310 | |
| 311 | |
| 312 | |
| 313 | |
| 314 | |
| 315 | |
| 316 | |
| 317 | |
| 318 | |
| 319 | |
| 320 | |
| 321 | |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | |
| 327 | |
| 328 | |
| 329 | |
| 330 | |
| 331 | |
| 332 | |
| 333 | |
| 334 | |
| 335 | |
| 336 | |
| 337 | |
| 338 | |
| 339 | |
| 340 | |
| 341 | |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | |
| 350 | |
| 351 | |
| 352 | |
| 353 | |
| 354 | |
| 355 | |
| 356 | |
| 357 | |
| 358 | |
| 359 | |
| 360 | |
| 361 | |
| 362 | |
| 363 | |
| 364 | |
| 365 | |
| 366 | |
| 367 | |
| 368 | |
| 369 | |
| 370 | |
| 371 | |
| 372 | |
| 373 | |
| 374 | |
| 375 | |
| 376 | |
| 377 | |
| 378 | |
| 379 | |
| 380 | |
| 381 | |
| 382 | |
| 383 | |
| 384 | |
| 385 | |
| 386 | |
| 387 | |
| 388 | |
| 389 | |
| 390 | |
| 391 | |
| 392 | |
| 393 | |
| 394 | |
| 395 | |
| 396 | |
| 397 | |
| 398 | |
| 399 | |
| 400 | |
| 401 | |
| 402 | |
| 403 | |
| 404 | |
| 405 | |
| 406 | |
| 407 | |
| 408 | |
| 409 | |
| 410 | |
| 411 | |
| 412 | |
| 413 | |
| 414 | |
| 415 | |
| 416 | |
| 417 | |
| 418 | |
| 419 | |
| 420 | |
| 421 | |
| 422 | |
| 423 | |
| 424 | |
| 425 | |
| 426 | |
| 427 | |
| 428 | |
| 429 | |
| 430 | |
| 431 | |
| 432 | |
| 433 | |
| 434 | |
| 435 | |
| 436 | |
| 437 | |
| 438 | |
| 439 | |
| 440 | |
| 441 | |
| 442 | |
| 443 | |
| 444 | |
| 445 | |
| 446 | |
| 447 | |
| 448 | |
| 449 | |
| 450 | |
| 451 | |
| 452 | |
| 453 | |
| 454 | |
| 455 | |
| 456 | |
| 457 | |
| 458 | |
| 459 | |
| 460 | |
| 461 | |
| 462 | |
| 463 | |
| 464 | |
| 465 | |
| 466 | |
| 467 | |
| 468 | |
| 469 | |
| 470 | |
| 471 | |
| 472 | |
| 473 | |
| 474 | |
| 475 | |
| 476 | |
| 477 | |
| 478 | |
| 479 | |
| 480 | |
| 481 | |
| 482 | |
| 483 | |
| 484 | |
| 485 | |
| 486 | |
| 487 | |
| 488 | |
| 489 | |
| 490 | |
| 491 | |
| 492 | |
| 493 | |
| 494 | |
| 495 | |
| 496 | |
| 497 | |
| 498 | |
| 499 | |
| 500 | |
| 501 | |
| 502 | |
| 503 | |
| 504 | |
| 505 | |
| 506 | |
| 507 | |
| 508 | |
| 509 | |
| 510 | |
| 511 | |
| 512 | |
| 513 | |
| 514 | |
| 515 | |
| 516 | |
| 517 | |
| 518 | |
| 519 | |
| 520 | |
| 521 | |
| 522 | |
| 523 | |
| 524 | |
| 525 | |
| 526 | |
| 527 | |
| 528 | |
| 529 | |
| 530 | |
| 531 | |
| 532 | |
| 533 | |
| 534 | |
| 535 | |
| 536 | |
| 537 | |
| 538 | |
| 539 | |
| 540 | |
| 541 | |
| 542 | |
| 543 | |
| 544 | |
| 545 | |
| 546 | |
| 547 | |
| 548 | |
| 549 | |
| 550 | |
| 551 | |
| 552 | |
| 553 | |
| 554 | |
| 555 | |
| 556 | |
| 557 | |
| 558 | |
| 559 | |
| 560 | |
| 561 | |
| 562 | |
| 563 | |
| 564 | |
| 565 | |
| 566 | |
| 567 | |
| 568 | |
| 569 | |
| 570 | |
| 571 | |
| 572 | |
| 573 | |
| 574 | |
| 575 | |
| 576 | |
| 577 | |
| 578 | |
| 579 | |
| 580 | |
| 581 | |
| 582 | |
| 583 | |
| 584 | |
| 585 | |
| 586 | |
| 587 | |
| 588 | |
| 589 | |
| 590 | |
| 591 | |
| 592 | |
| 593 | |
| 594 | |
| 595 | |
| 596 | |
| 597 | |
| 598 | |
| 599 | |
| 600 | |
| 601 | |
| 602 | |
| 603 | |
| 604 | |
| 605 | |
| 606 | |
| 607 | |
| 608 | |
| 609 | |
| 610 | |
| 611 | |
| 612 | |
| 613 | |
| 614 | |
| 615 | |
| 616 | |
| 617 | |
| 618 | |
| 619 | |
| 620 | |
| 621 | |
| 622 | |
| 623 | |
| 624 | |
| 625 | |
| 626 | |
| 627 | |
| 628 | |
| 629 | |
| 630 | |
| 631 | |
| 632 | |
| 633 | |
| 634 | |
| 635 | |
| 636 | |
| 637 | |
| 638 | |
| 639 | |
| 640 | |
| 641 | |
| 642 | |
| 643 | |
| 644 | |
| 645 | |
| 646 | |
| 647 | |
| 648 | |
| 649 | |
| 650 | |
| 651 | |
| 652 | |
| 653 | |
| 654 | |
| 655 | |
| 656 | |
| 657 | |
| 658 | |
| 659 | |
| 660 | |
| 661 | |
| 662 | |
| 663 | |
| 664 | |
| 665 | |
| 666 | |
| 667 | |
| 668 | |
| 669 | |
| 670 | |
| 671 | |
| 672 | |
| 673 | |
| 674 | |
| 675 | |
| 676 | |
| 677 | |
| 678 | |
| 679 | |
| 680 | |
| 681 | |
| 682 | |
| 683 | |
| 684 | |
| 685 | |
| 686 | |
| 687 | |
| 688 | |
| 689 | |
| 690 | |
| 691 | |
| 692 | |
| 693 | |
| 694 | |
| 695 | |
| 696 | |
| 697 | |
| 698 | |
| 699 | |
| 700 | |
| 701 | |
| 702 | |
| 703 | |
| 704 | |
| 705 | |
| 706 | |
| 707 | |
| 708 | |
| 709 | |
| 710 | |
| 711 | |
| 712 | |
| 713 | |
| 714 | |
| 715 | |
| 716 | |
| 717 | |
| 718 | |
| 719 | |
| 720 | |
| 721 | |
| 722 | |
| 723 | |
| 724 | |
| 725 | |
| 726 | |
| 727 | |
| 728 | |
| 729 | |
| 730 | |
| 731 | |
| 732 | |
| 733 | |
| 734 | |
| 735 | |
| 736 | |
| 737 | |
| 738 | |
| 739 | |
| 740 | |
| 741 | |
| 742 | |
| 743 | |
| 744 | |
| 745 | |
| 746 | |
| 747 | |
| 748 |