forge
Explore
Log in

Don't you dare go Hollow

Refill the arithmetic decoder a field at a time, not a bit at a time

Browse files at this commit

bea686b75c3343c0eee044d06eeef0141c032959Ahmet Kaan GümüşParent: 6081769
  • perf
    • refill the arithmetic decoder a field at a time, not a bit at a time
Show full message
perf(av1): refill the arithmetic decoder a field at a time, not a bit at a time

`SymbolDecoder::symbol_fixed` was 6.8% self time after the transform work,
and most of what it does per symbol beyond the alphabet walk is `f(n)` --
the renormalisation refill, which looped once per bit, fetching, masking and
shifting a byte each time. Five bytes cover any 32-bit field at any bit
offset, so the whole field is now one assemble-and-shift; a byte past the end
of the payload still reads as zero, exactly as the per-bit `unwrap_or(0)`
did. The alphabet walk also hoists `range >> 8`, which cannot move until the
symbol is chosen.

`bulk_bit_reads_match_the_per_bit_form` checks the new `f` against the loop
it replaced at every start offset over a 40-byte payload, every width from 0
to 32, and 16 bits past the end.

Byte-exact: `EC_PROBE_OUT16` dumps hash-identical to the lane's baseline
binary on 1080p 10-bit, 2160p HDR and the 8-bit palette fixture. Interleaved
medians against the previous commit: 1080p 12.35 s -> 12.07 s, 4K 64.44 s ->
63.94 s.
crates/ec-av1/src/msac.rsmodified+47 -7
@@ -279,13 +279,22 @@ impl<'a> SymbolDecoder<'a> {
279 279
280 /// `f(n)`: the next `n` bits, most significant first, zero past the end.280 /// `f(n)`: the next `n` bits, most significant first, zero past the end.
281 fn f(&mut self, n: u32) -> u32 {281 fn f(&mut self, n: u32) -> u32 {
282- let mut v = 0;282+ debug_assert!(n <= 32, "f(n) reads at most 32 bits");
283- for _ in 0..n {283+ if n == 0 {
284- let byte = self.data.get(self.bit >> 3).copied().unwrap_or(0);284+ return 0;
285- v = (v << 1) | u32::from((byte >> (7 - (self.bit & 7))) & 1);
286- self.bit += 1;
287 }285 }
288- v286+ // The bit-at-a-time form of this loop was the renormalisation cost
287+ // inside every `symbol_fixed`. Five bytes cover any 32-bit field at
288+ // any bit offset, and a byte past the end reads as zero exactly as
289+ // the per-bit `unwrap_or(0)` did.
290+ let start = self.bit >> 3;
291+ let off = (self.bit & 7) as u32;
292+ let mut acc = 0u64;
293+ for k in 0..5 {
294+ acc = (acc << 8) | u64::from(self.data.get(start + k).copied().unwrap_or(0));
295+ }
296+ self.bit += n as usize;
297+ ((acc >> (40 - off - n)) & ((1u64 << n) - 1)) as u32
289 }298 }
290 299
291 /// `decode_symbol` (spec 8.2.6), with the adaptation of 8.3.2.300 /// `decode_symbol` (spec 8.2.6), with the adaptation of 8.3.2.
@@ -303,10 +312,13 @@ impl<'a> SymbolDecoder<'a> {
303 let mut cur = self.range;312 let mut cur = self.range;
304 let mut prev;313 let mut prev;
305 let mut symbol = 0;314 let mut symbol = 0;
315+ // Loop-invariant: the coder's range does not move until the symbol is
316+ // chosen, so its top byte is computed once rather than per candidate.
317+ let base = self.range >> 8;
306 loop {318 loop {
307 prev = cur;319 prev = cur;
308 let f = u32::from(CDF_TOP - cdf[symbol]);320 let f = u32::from(CDF_TOP - cdf[symbol]);
309- cur = (((self.range >> 8) * (f >> EC_PROB_SHIFT)) >> (7 - EC_PROB_SHIFT))321+ cur = ((base * (f >> EC_PROB_SHIFT)) >> (7 - EC_PROB_SHIFT))
310 + EC_MIN_PROB * (nsyms - 1 - symbol) as u32;322 + EC_MIN_PROB * (nsyms - 1 - symbol) as u32;
311 if self.value >= cur {323 if self.value >= cur {
312 break;324 break;
@@ -358,6 +370,34 @@ impl<'a> SymbolDecoder<'a> {
358 pub(crate) mod tests {370 pub(crate) mod tests {
359 use super::*;371 use super::*;
360 372
373+ /// `f(n)` now assembles five bytes and shifts instead of walking bits.
374+ /// This pins it against the per-bit form it replaced, at every start
375+ /// offset, every width up to 32, and past the end of the buffer (where
376+ /// both forms must read zeros).
377+ #[test]
378+ fn bulk_bit_reads_match_the_per_bit_form() {
379+ let data: Vec<u8> = (0..40u32).map(|i| (i * 37 + 11) as u8).collect();
380+ let per_bit = |start: usize, n: u32| -> u32 {
381+ let mut bit = start;
382+ let mut v = 0u32;
383+ for _ in 0..n {
384+ let byte = data.get(bit >> 3).copied().unwrap_or(0);
385+ v = (v << 1) | u32::from((byte >> (7 - (bit & 7))) & 1);
386+ bit += 1;
387+ }
388+ v
389+ };
390+ for start in 0..(data.len() * 8 + 16) {
391+ for n in 0..=32u32 {
392+ let mut d = SymbolDecoder::new(&data);
393+ d.bit = start;
394+ let got = d.f(n);
395+ assert_eq!(got, per_bit(start, n), "start {start} width {n}");
396+ assert_eq!(d.bit, start + n as usize, "start {start} width {n} advance");
397+ }
398+ }
399+ }
400+
361 // `tile.rs`'s own tests reach `SymbolDecoder` through this path; it now401 // `tile.rs`'s own tests reach `SymbolDecoder` through this path; it now
362 // lives in the outer module (non-test code decodes with it too), so this402 // lives in the outer module (non-test code decodes with it too), so this
363 // re-export is what keeps those call sites resolving unmodified.403 // re-export is what keeps those call sites resolving unmodified.