Repository navigation
perf: record the header boxes before the codestream once (#789) - #790
Open
D0ubleD0uble wants to merge 9 commits into
Open
D0ubleD0uble wants to merge 9 commits into
D0ubleD0uble wants to merge 9 commits into
Conversation
An IncrementalDecoder walked the boxes before the codestream on every header() call and made a 24-byte record of each header box: the children of each jp2h, and the bpcc, colr, pclr, cmap and cdef boxes misplaced at the top level after it. The per-type accessors then collected payload Vecs. 100,000 misplaced colr boxes cost 10.5 MB allocated a call. The records now hold the box type and a range of the input, so they borrow nothing and the walks keep them (Walks::header) for one jp2c start, as they keep the lists. A later walk still checks the boxes but takes the kept records in place of new ones. The accessors return iterators, so no call collects a Vec. IccMemo::fit checks whether the profile is the one it holds, shared, before comparing the bytes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EhjiZ6QTR3gTC3QaD9m2QV
Keep the records only where the walks outlive the parse, shrunk to fit, and pin it: a unit test that the walks keep them once and later parses share them, and three shapes in the allocation guard (a jp2h of bpcc children, repeated jp2h boxes, misplaced top-level colr boxes) that a later call allocates under 1 KiB for, 787 KB to 1.3 MB on main. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EhjiZ6QTR3gTC3QaD9m2QV
…g lines The IncrementalDecoder::header rustdoc, docs/resource-bounds.md, docs/incremental-decoding.md, SECURITY.md and the HeaderCache docs now say that the first call keeps a 24-byte record for each header box in or after jp2h and later calls share it, with measured figures, and that the walks are 2480 bytes. Five prose lines over 80 columns are re-wrapped. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EhjiZ6QTR3gTC3QaD9m2QV
The walk places ihdr among the header boxes whether it records them or not, so the kept records need not carry it. A record past the input a header reads is refused as Error::Inconsistent, now pinned by a test. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EhjiZ6QTR3gTC3QaD9m2QV
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EhjiZ6QTR3gTC3QaD9m2QV
Shrinking the list to fit held both lists at once: with 100000 bpcc children the first incremental call peaked at 5.55 MB against 4.72 MB on main. The list is now kept as it grew, with room for at most twice as many records, and the first call's peak is main's. Also test that records kept for another jp2c start are made again where the walks hold no lists of the boxes before the codestream: the guard in parse_boxes_in was reached by no test. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EhjiZ6QTR3gTC3QaD9m2QV
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EhjiZ6QTR3gTC3QaD9m2QV
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
A list that grows by doubling either keeps up to twice the room it uses or, shrunk to fit, holds both copies at once on the first call. The records now go in the blocks MetadataList uses: a full block never moves or grows, so the first call peaks at the records (2.48 MB for 100000 bpcc children, 4.72 MB on main) and the decoder holds at most 3.2 bytes for each byte of the boxes, plus 128 bytes. The walks grow to 2504 bytes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EhjiZ6QTR3gTC3QaD9m2QV
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EhjiZ6QTR3gTC3QaD9m2QV
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #789
What changed
An
IncrementalDecodernow records the header boxes before the codestream once (everyjp2hchild, a repeatedjp2h, and misplaced top-levelcolr/bpcc/pclr/cmap/cdefafterjp2h) and shares them with every laterheader()call. Before, each call rebuilt the whole list.Decision: keep, don't document. The records can be kept without borrowing the input. A record is now an owned
Child { kind, start, end }, a range of the input (24 B, 12 B on 32-bit). The records sit injp2::Walks::headeras aMetadataList<Child>(the 64-record blocks the metadata lists use), under the same key as the other kept walks (thejp2cstart, with input that only grows). A later walk still walks and checks every header box, then adopts the kept records instead of pushing new ones.adoptrefuses records that end past the input withError::Inconsistent. One-shotparse_boxeskeeps none, soread_header/decodegain no extra allocation. The records are built in blocks of 64 (list::Extender): a full block never moves or grows, so no record is held twice and nothing is shrunk. The decoder holds 24 bytes a record, at most 64 more for each block of 64 (its 16-byte header and up to two 24-byte table slots), and 128 bytes more: at most 3.2 bytes for each byte of those boxes, plus 128 bytes.Also in this PR:
colr()/pclr()/cmap()/cdef()/res()accessors return iterators, so a call collects no Vec.IccMemo::fittakes aptr::eqfast path, so a shared profile is not compared byte by byte.Jp2Headerhas a manualDebug(it prints the same in both modes and does not print the input).size_of::<jp2::Walks>()goes from 2448 to 2504.MetadataListgains a crate-internalblocks()(the items as their slices), andExtendergainslen(),blocks()andClone.SECURITY.md:50/72/78,docs/incremental-decoding.md:420/474).No public API change. No error or refusal changes.
Acceptance → test
header()allocates nothing per header box (misplaced colr, repeated jp2h, many jp2h children)tests/header_boxes_alloc.rsevery_header_box_is_read_once_and_shared: n=10000 per shape, later call< 1 KiBand equal to the first. On main these shapes allocate 787,048 / 1,311,208 / 1,311,208 Bjp2::tests::the_walks_keep_the_header_box_records_once(5 records,Arcshared across later parses, ihdr position, Debug equal, the one-shot path keeps none)jp2cstart are made again, even with no kept box listsjp2::tests::header_box_records_kept_for_another_codestream_start_are_made_again(killsif kept.count_onlyat the re-walk guard)jp2::tests::header_box_records_past_a_block_are_read_back_in_order(130 records: two full blocks and a partial one, on the first walk and on the adopting walk)jp2::tests::header_box_records_past_the_input_are_refusedbpccone-shot< 480000 + 24 KiB(measured 498,593; main 789,401)jp2::srgb::memo_tests::the_memo_compares_no_bytes_of_the_profile_it_sharesheader()rustdoc,docs/resource-bounds.md(new #789 table),docs/incremental-decoding.md,SECURITY.md,HeaderCachenotes, CHANGELOGBefore / after (later
header()call)colrjp2hboxesjp2hwith 100kbpccjp2hwith 50kcolrjp2hwith 100kfree(Release build, median of 21 runs.)
First
header()call, and what the decoder holdsRelease build, one decoder per row. Time is the median of 7 first calls.
shrink_to_fitversionjp2hwith 100kbpcc: allocated / peakcolr: allocated / peakjp2h: allocated / peakFor the
bpccrow, the held records come to 24.74 bytes each, 2.75 bytes for each byte of the 9-byte boxes. Main holds nothing for the records, because it builds them again on every call. In thecolrrows the held figure also includes the metadata'scolrlist, which main holds too.Verification
cargo fmt --check, bothclippy -D warningsruns,cargo test(116 suites, 2098 passed),cargo deny check,RUSTDOCFLAGS=-D warnings cargo doc --no-deps.--lib862 passed,jp2_icc30,sample_limit18,limit_32bit2.jp2_boxes(798k runs on the block version),header_split(142k),incremental_decode(983k on the block version,-max_len=4096)..jp2/.jph) × default, Srgb and strict-off. Coversdecode_with,read_header_with, incrementalheader()twice andfinish(). FNV-128 over{:#?}, 5,865 hashes, identical. The first and secondheader()hashes match in every case.Extender::finishdrops the partial tailExtender::blocksdrops the pending tailMetadataList::blocksdrops the tail, or the last full blockExtender::lenleaves out the tailif kept.count_onlyptr::eqremovedWhat to look at hardest
parse_boxes_ininsrc/jp2/mod.rs. When records are kept, the early walk checks without pushing. Ifkeeps_forfails, it starts the walk again from scratch. Records are saved only once thejp2cstart is kept.Jp2Header::readinsrc/jp2/header.rs.ihdr_atand the child count are taken from the walk whether or not the walk records. That is whyRecordsno longer carriesihdr_at.adopttrusts that the walk under the same key saw the same boxes. The end check is a backstop against reading past the input, not a full equality check.🤖 Generated with Claude Code
https://claude.ai/code/session_01EhjiZ6QTR3gTC3QaD9m2QV