Skip to content

perf(mp4): preallocate flat subsample senc storage - #735

Merged
tobbee merged 1 commit into
Eyevinn:masterfrom
perfloop-oss:perfloop-pr-open-n5rfyekgvj
Dec 2, 2026
Merged

tobbee merged 1 commit into
Eyevinn:masterfrom
perfloop-oss:perfloop-pr-open-n5rfyekgvj

Conversation

@perfloop-agent

@perfloop-agent perfloop-agent commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Parsing subsample-encrypted fragments allocates a pattern slice for every sample. In BenchmarkDecodeCenc on prog_8s_enc_dashinit.mp4, allocations drop from 1,222 to 966 per op (21% fewer). This change reserves the sample tables and stores subsample patterns in one flat array.

cc @tobbee

Benchmark Base Patch Matched-run change
BenchmarkDecodeCenc 1,222 allocs/op 966 allocs/op -256 allocs/op (-257 to -256, n=12); -20.9% (-21.0% to -20.9%, n=12)

No end-to-end decrypt-time improvement is claimed; AES decryption is unchanged.

The first pass validates record boundaries and counts patterns without allocating sample storage. The second pass reads the fields directly from the payload. Each sample's pattern and IV slices have their capacities clipped to their lengths, so appending cannot overwrite the next sample. IVs continue to reference the input bytes.

Heuristic IV-size selection, saiz filtering and authoritative re-parsing keep their existing behavior. Rejected candidates allocate no sample arrays. Storage size still grows with the number of samples and patterns; the number of allocations does not.

Go tests covering senc parsing, encrypted fragment decoding and the decrypt CLI passed. Static analysis, formatting, spelling and changelog validation passed. The test-386 job is left to target CI.

The existing benchmark is in cmd/mp4ff-decrypt/main_test.go. Compiled benchmark runs from that directory used Go 1.27.4 on an Intel Xeon Platinum 8581C:

./decrypt.test -test.run='^$' -test.bench='^BenchmarkDecodeCenc$' -test.benchmem -test.count=1 -test.benchtime=200ms

A further reproduction command is:

go test ./cmd/mp4ff-decrypt -run '^$' -bench '^BenchmarkDecodeCenc$' -benchmem -count=8
Isolated parsing result

SencBox.ParseReadBox with an authoritative 8-byte IV size on the committed mp4/testdata/seg_cenc_no_seig.m4v fixture, measured with a harness not included in this PR:

Benchmark Base Patch Matched-run change
SencBox.ParseReadBox 19 allocs/op 3 allocs/op -16 allocs/op (-16 to -16, n=13); -84.2% (-84.2% to -84.2%, n=13)

Case and evidence


Made by Perfloop. Human sponsor: Tomás Senart.

@tobbee tobbee left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this PR! The change is correct and the gain is real. I checked it like this:

  • benchstat (n=8, Apple M4 Pro) of BenchmarkDecodeCenc: allocations 1,222 → 966 (−21%), B/op −1.4% and time unchanged (p=0.13), as the description says.
  • I compared the output of the old and new parser on about 137,000 parses of random senc boxes and on the senc boxes in mp4/testdata. The inputs included truncated records, trailing bytes, too large subsample counts, IV-size guessing with and without saiz, and re-parsing with a known IV size. The results were identical, including the non-nil empty slices of samples without subsamples.
  • The tests pass, also with -race.

Some changes would make it fit how this project works. AGENTS.md and CONTRIBUTING.md describe the conventions.

  1. Each commit should pass the tests on its own. b6c2aa4 adds TestSencParseAllocations before the change that makes it pass, so all its subtests fail on that commit, and git bisect would stop there. Pull requests are often rebase-merged, so the commits land on master as they are. Please fold the new tests into the perf commit, or keep only tests that already pass on the old code in the first commit, such as the append check in TestAddSamplesMixedSubSamples.
  2. Commit messages need a body. As on #734, the commit messages carry the history: what was slow and why, what changed, and the measurements. The PR description already has all of that.
  3. The changelog entry should describe the effect for users. "append-isolated sample slices" describes the implementation. As changelog.d/README.md says, an entry is one or two lines that say what changed for users and name the affected API. For example:
    - `SencBox.ParseReadBox` stores the subsample patterns of a box in one array, so decoding
      subsample-encrypted fragments makes about 20% fewer allocations
  4. TestSencParseAllocations can be smaller. It runs 3 sample counts × 3 IV sizes × 6 modes, 54 subtests. We test boundary values and a middle value rather than all combinations. The failure modes (shortRecord, truncated and trailing) do not depend on the sample count, so one size is enough for them, and two sample counts show that the number of allocations stays the same.
  5. Optional: cap the IV slices too, with data[:ivSize:ivSize]. Each IV still has the rest of the box data as capacity, so appending to IVs[0] overwrites the IV of the next sample in the input. That is not new in this PR, but it would give the IVs the protection that the PR gives the subsample slices, and a line in the append check could cover it.

Parsing a subsample-encrypted senc box allocated a pattern slice per sample and repeatedly grew the IV table. Heuristic attempts also allocated partial sample tables before rejecting a candidate.

Validate record boundaries and count patterns before allocating exact-size IV and sample tables and one flat pattern array. Decode directly from the payload and clip each sample pattern and IV slice capacity so appending cannot overwrite the next sample. Keep IV-size selection and authoritative re-parsing unchanged.

BenchmarkDecodeCenc on prog_8s_enc_dashinit.mp4 makes 966 allocations per operation instead of 1,222 (20.9% fewer). Isolated ParseReadBox on seg_cenc_no_seig.m4v makes 3 instead of 19. The allocation test covers representative sizes, IV sizes and parsing modes, and the mixed-sample test checks append isolation.

Signed-off-by: Perfloop Agent <[email protected]>
@perfloop
perfloop Bot force-pushed the perfloop-pr-open-n5rfyekgvj branch from 52ab677 to 6539632 Compare December 1, 2026 12:15
@perfloop-agent

Copy link
Copy Markdown
Contributor Author

The revision combines the optimization and tests into one passing perf commit, with a body explaining the motivation, implementation and allocation measurements. The changelog now names SencBox.ParseReadBox and the user-visible gain, and the allocation test is down to 11 representative cases.

IV capacities are clipped too, with the append-isolation check added to the mixed-sample test. Clipping left the measured allocation counts unchanged; the new check fails without it. The description keeps its existing structure and no decrypt-time improvement claim, with refreshed allocation results and the IV protection noted.

@tobbee
tobbee merged commit cb90ea2 into Eyevinn:master Dec 2, 2026
11 checks passed
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.

2 participants