Repository navigation
perf(mp4): preallocate flat subsample senc storage - #735
Conversation
tobbee
left a comment
There was a problem hiding this comment.
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 withoutsaiz, 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.
- Each commit should pass the tests on its own.
b6c2aa4addsTestSencParseAllocationsbefore the change that makes it pass, so all its subtests fail on that commit, andgit bisectwould 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 inTestAddSamplesMixedSubSamples. - 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.
- 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
TestSencParseAllocationscan 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,truncatedandtrailing) 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.- Optional: cap the IV slices too, with
data[:ivSize:ivSize]. Each IV still has the rest of the box data as capacity, so appending toIVs[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]>
52ab677 to
6539632
Compare
|
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 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. |
Parsing subsample-encrypted fragments allocates a pattern slice for every sample. In
BenchmarkDecodeCenconprog_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
BenchmarkDecodeCencNo 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,
saizfiltering 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-386job 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:A further reproduction command is:
Isolated parsing result
SencBox.ParseReadBoxwith an authoritative 8-byte IV size on the committedmp4/testdata/seg_cenc_no_seig.m4vfixture, measured with a harness not included in this PR:SencBox.ParseReadBoxCase and evidence
Made by Perfloop. Human sponsor: Tomás Senart.