Skip to content

feat!: share ReaderRequirements (rreq) through Payload and MetadataList, read once - #784

Open
D0ubleD0uble wants to merge 4 commits into
mainfrom
feat/rreq-shared
Open

D0ubleD0uble wants to merge 4 commits into
mainfrom
feat/rreq-shared

Conversation

@D0ubleD0uble

Copy link
Copy Markdown
Owner

Closes #783

What changes

  • ReaderRequirements::fully_understand, decode_completely, StandardFeature::mask and VendorFeature::mask are a Payload. standard_features and vendor_features are a MetadataList. This is the design feat!: share reported box payloads through an opaque Payload type, copied once #769 and feat!: make Jp2Metadata.color_specifications a MetadataList #780 used for the other boxes.
  • rreq::read (it replaces rreq::parse) first checks the box without allocating. In the measure pass of Copier::twice it builds nothing. It walks the box again only when ML ≥ 16, so the masks that go in a shared chunk get measured. In the emit pass it builds the box once, through Copier and list::Extender.
  • Passthrough carries a rreq_seen flag instead of a clone of the kept rreq. open_boxes takes the flag, not a second copy. The header gets kept.rreq.clone(), which shares the masks and the lists.
  • Docs: wherever "3.2 bytes for each byte of JP2 metadata boxes" is cited, it now adds "and about 4.6 MiB more for the first rreq". See the note on the bound below. The places are SECURITY.md (the three bound sentences, the incremental list and step 1), README.md, docs/resource-bounds.md (three citations, the JP2 box records section and a new measurement table), docs/incremental-decoding.md, the IncrementalDecoder::header rustdoc, and the decode/DecodeOptions::sample_limit rustdoc in src/lib.rs. The CHANGELOG "Migrating from 0.5" covers the rreq fields, and a Changed entry gives the numbers.

Acceptance

Criterion Evidence
Public types change; a repeat header() shares the rreq (pointer test that fails on main) tests/incremental_header_payloads.rs::header_shares_the_rreq_across_calls. It covers ML 1 and 16, before and after the codestream, and 70 features (past a block). Entry pointers, and for ML 16 the mask pointers, are equal across a repeat call and a call on more bytes, and the FUAM/DCM masks are adjacent in one chunk. Fails on main (line 438, the entry pointers differ).
Read once, no parse in the measure pass, single copy tests/rreq_alloc.rs (counting allocator), on ML 0, NSF = NVF = 65535. One-shot: heap held < 4.2, peak < 4.4, allocated in all < 8.6 bytes a byte. Incremental: a second header() allocates < 4 KiB and shares the entries. Fails on main (peak 8.00, allocated 24.0).
Mutation checks Each one was applied, asserted, run and restored from a backup. M1: the measure pass builds the box → rreq_alloc fails (allocated). M2: the header deep-copies the lists → header_shares_the_rreq_across_calls fails. M3: the measure pass skips the shared masks → the same test fails (payloads.rs "a payload not measured"). M4: the open last box reads a second rreq → a_last_rreq_of_length_0_counts_only_as_the_first fails. M5: masks copied with copy_of, not into the chunk → the adjacency check fails.
Output, Debug and oracle snapshots unchanged All 89 .jp2/.jph fixtures under tests/fixtures, in default, ColorOutput::Srgb and strict(false): format!("{:?}") of decode_with (samples included) and of read_header_with, from a release build on main 2b85059 and on this branch. 267 files, cmp: 0 differ. 6 of the outputs carry an rreq. The oracle snapshot tests pass.
CHANGELOG, auto_traits, rustdoc, docs Migrating entries updated (#783). tests/auto_traits.rs checks MetadataList<StandardFeature> and MetadataList<VendorFeature>. New a_feature_entry_is_the_size_the_docs_give checks the 32/40-byte entries (24/36 on 32-bit). It also runs in the wasm32 lib tests.
Gate, doc, wasm32, fuzz See below.

Measurements

One rreq before the jp2c of tests/fixtures/jp2_sycc_422_even.jp2, by the method in docs/resource-bounds.md: a release program that reads the file and then calls decode, or feeds it all and calls header once and then 10 more times. Heap is from a counting allocator, in bytes for each byte of the box. Resident peak is /usr/bin/time -v over a no-feature rreq baseline, input included, in MiB. Load was 3 to 4.

rreq (box) heap peak before → after heap held before → after resident peak before → after later header() call before → after
ML 0 (1.18 MB) 8.00 → 4.08 4.00 → 4.07 13.3 → 5.7 4.9 ms, +4.0 B/B → 2.3 µs, 1160 B
ML 1 (1.31 MB) 7.40 → 3.67 3.70 → 3.66 18.2 → 5.7 25.8 ms, +3.7 B/B → 2.4 µs, 1160 B
ML 8 (2.23 MB) 5.18 → 2.16 2.59 → 2.15 19.0 → 6.7 28.9 ms, +2.6 B/B → 2.3 µs, 1160 B
ML 0, NVF 0 (131 KB) 32.0 → 16.4 16.0 → 16.3 6.0 → 2.1 1.0 ms → 2.2 µs

The incremental first call matches the one-shot figures. Resident held after decode, over the baseline, was 11 to 16 MiB before and is 2.7 to 3.7 MiB now.

The bound: please judge

The heap held at ML 0 is still about 4.07 bytes for each byte of the box, not 3.2. The entries themselves set that: a StandardFeature is 32 bytes for 2 bytes of box, and a VendorFeature is 40 for 16. A box of standard features alone holds about 16 bytes for each byte of it. Only the first rreq is read, though, and it has at most 65535 features of each kind. So its entries are capped at about 4.6 MiB whatever its size, and any mask over 15 bytes counts as payload bytes under 3.2. The docs now say "up to about 3.2 bytes for each byte of JP2 metadata boxes and about 4.6 MiB more for the first rreq" wherever the 3.2 figure is cited. That is the issue's "narrower honest bound" option. Shrinking the entries below 32/40 bytes would need a different Payload or a different public shape, which is outside this issue.

Gate (after rustup update stable, rustc 1.100.1)

Run on ed16d50. The commit after it changes only a SECURITY.md wording.

  • cargo fmt --all -- --check: ok
  • cargo clippy --all-targets -- -D warnings: ok
  • cargo clippy --lib -- -D warnings: ok
  • cargo test: 2086 passed, 0 failed, 20 ignored
  • cargo deny check: advisories, bans, licenses, sources ok
  • RUSTDOCFLAGS=-D warnings cargo doc --no-deps: ok
  • cargo test --target wasm32-wasip1 --lib (wasmtime runner as in CI): 854 passed, 6 ignored
  • Fuzz, 60 s each, nice, one at a time: jp2_boxes 821,384 runs, header_split 116,532 runs, no crash

What to look at hardest

  • src/jp2/rreq.rs: the measure and emit passes must call copier.copy in the same order for the same boxes. fields checks the box before either pass copies anything, so a malformed box copies nothing in both.
  • metadata() in src/jp2/mod.rs: kept.rreq is set only on the first call that meets the box, and the header takes kept.rreq.clone().flatten().or(open.rreq). A malformed first rreq (Some(None)) still hides a later open one.

🤖 Generated with Claude Code

https://claude.ai/code/session_01EhjiZ6QTR3gTC3QaD9m2QV

D0ubleD0uble and others added 4 commits November 29, 2026 19:54
…st, read once

The rreq masks become Payload and its feature lists MetadataList. The box
is read once, in the pass that keeps what it builds; the measure pass
only checks it, and walks it again only for masks long enough to share a
chunk. The header shares the kept copy instead of a second one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EhjiZ6QTR3gTC3QaD9m2QV
…re a chunk

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EhjiZ6QTR3gTC3QaD9m2QV
The rreq entries are 32 and 40 bytes for features of 2 and 16 bytes, so
the box can hold more than 3.2 bytes for each byte of it. Only the first
is read and it has at most 65535 features of each kind, so the bounds now
add about 4.6 MiB for it wherever the 3.2 figure is cited.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EhjiZ6QTR3gTC3QaD9m2QV

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat!: share ReaderRequirements (rreq) through Payload and MetadataList, read once

1 participant