forge
Explore
Log in

Don't you dare go Hollow

Commits

Commits on mainFiltered to crates/engine/examples/mkv_open_cost.rsClear filter

2026-08-12

⚡ perf(demux): open a Matroska file off its Cues, not off every clusterAhmet Kaan Gümüş · 84ee43f
⚡ perf(demux): open a Matroska file off its Cues, not off every cluster

Opening a Matroska file walked the whole segment -- every cluster header
of every gigabyte -- because Matroska carries no sample table and the
frame count and the sync points had to come from somewhere. That is
seconds of disk per open, and an open happens on every seek, every clip
span and every export source, not once per file.

Matroska has an index of its own and nothing here read it: `Cues`, one
entry per keyframe, written at the far end of the segment and found
through the `SeekHead` in kilobytes. Now the open parses that, and the
clusters are walked only where a caller asks to read -- one cue interval
at a time, ten seconds of film.

Measured, cold (page cache dropped per file) and warm, on the library:

  file                       open cold      open warm     bytes read
  4.9 GB AV1 remux         4027 -> 26 ms   469 -> 0.6 ms  9.9 MB -> 99 KB
  12.1 GB 2160p HEVC       9508 -> 54 ms   958 -> 8.6 ms  13.2 MB -> 1.3 MB
  1.4 GB 720p H.264 (ep)    395 -> 1.8 ms   64 -> 0.2 ms  1.5 MB -> 20 KB

Same answers, not similar ones
- The frame count stays exact at open: the last cue's index plus the
  blocks after it, counted by walking from that cue to the end of the
  segment. No duration-times-rate estimate, nothing corrected under the
  UI later.
- A cue states a *timestamp*, and a stream with B-frames does not store
  its blocks in that order: `anchor` counts the blocks written after a
  cue and shown before it, which is the difference between the frames
  shown before it and its place in the file. Without that a 60-frame
  HEVC fixture reports 62 and every seek lands two frames late.
- `cues_agree` checks that arithmetic against the file's own blocks
  wherever a walk meets a cue. A file that disagrees -- variable rate,
  cue times naming no block -- falls back to the whole walk rather than
  seeking to the wrong frame. So do files with no `Cues` at all, and
  tracks with no `DefaultDuration` (nothing to turn a cue time into a
  frame number with).
- `probe_bitrate` completes the index first: it adds block lengths up
  and wants the file, so it pays the walk it always paid.
- The `Cues` element is read whole and parsed in memory. Element by
  element through the file, a 2160p film's ten thousand cue points cost
  a `pread` apiece: 118 ms of open against 8.6.

Checks
- `demux::tests::the_cue_index_and_the_whole_walk_are_the_same_index`:
  for all 20 Matroska fixtures, the cue index and the whole walk agree
  on the block list, the frame count, and where every frame of the file
  seeks to -- asserted per frame, forwards and backwards.
- `a_file_with_no_cues_is_the_walk_it_always_was`: a fixture with its
  `Cues` id and `SeekHead` pointer overwritten (no muxer here writes a
  file without an index) opens, counts and seeks identically.
- `examples/mkv_open_cost.rs`: open ms, bytes through the read syscalls,
  seek landings and a hash of every access unit. Run against the build
  before this change, the three library files above agree line for line
  on every landing and every hash.

Approach referenced from `matroska-demuxer 0.8.1` (hasenbanck), which
resolves a seek the same way; the parsing is this file's own EBML
reader, no dependency added.