forge
Explore
Log in

Don't you dare go Hollow

Dequantize into a reused buffer, with the loop invariants hoisted

Browse files at this commit

32f9ae5db038134aed2f847e18139c5b9a779ca3Ahmet Kaan GümüşParent: ee4e975
  • perf
    • dequantize into a reused buffer, with the loop invariants hoisted
Show full message
perf(av1): dequantize into a reused buffer, with the loop invariants hoisted

`dequant_wh` was a `map`/`collect` building a fresh `Vec<i32>` per transform
unit (7.8% self time, the third-hottest symbol in the decode profile), and
inside it every coefficient repeated the whole per-coefficient spec function:
two quantizer-table lookups with their own clamps, the area denominator's
match, and the bound shift -- all of which depend on the transform, not on
the coefficient.

`dequant_wh_into` hoists them and writes a caller-owned buffer, which the
one hot caller (`dequant_and_inverse_typed_wh`) keeps thread-local. A zero
level dequantizes to zero for every parameter set, so the all-but-EOB tail
of a grid now costs a compare rather than a 64-bit multiply and divide. The
arithmetic on a non-zero level is untouched; `dequant_wh` keeps its `Vec`
signature for the other callers.

`buffered_dequant_matches_the_per_coefficient_form` checks the buffered form
against `dequant_coeff_wh` over six transform shapes, three bit depths, three
quantizer/delta sets and both level signs.

Byte-exact: `EC_PROBE_OUT16` dumps cmp IDENTICAL against the lane's baseline
binary on 1080p 10-bit, 2160p HDR and the 8-bit palette fixture. Interleaved
medians against the previous commit: 1080p 13.46 s -> 12.15 s, 4K 67.11 s ->
63.38 s.
crates/ec-av1/src/quant.rsmodified+62 -5
@@ -226,11 +226,45 @@ pub fn dequant(levels: &[i32], side: usize, bit_depth: u8, q_idx: i32) -> Vec<i3
226 /// [`QuantDeltas`]-shaped `dc_delta`/`ac_delta` pair as [`dequant_coeff_wh`].226 /// [`QuantDeltas`]-shaped `dc_delta`/`ac_delta` pair as [`dequant_coeff_wh`].
227 #[allow(clippy::too_many_arguments)]227 #[allow(clippy::too_many_arguments)]
228 pub fn dequant_wh(levels: &[i32], w: usize, h: usize, bit_depth: u8, q_idx: i32, dc_delta: i32, ac_delta: i32) -> Vec<i32> {228 pub fn dequant_wh(levels: &[i32], w: usize, h: usize, bit_depth: u8, q_idx: i32, dc_delta: i32, ac_delta: i32) -> Vec<i32> {
229- levels229+ let mut out = Vec::new();
230- .iter()230+ dequant_wh_into(levels, &mut out, w, h, bit_depth, q_idx, dc_delta, ac_delta);
231- .enumerate()231+ out
232- .map(|(i, &l)| dequant_coeff_wh(l, i == 0, bit_depth, q_idx, dc_delta, ac_delta, w, h))232+}
233- .collect()233+
234+/// [`dequant_wh`] into a caller-owned buffer, with the two quantizer-table
235+/// lookups, the area denominator and the clamp bound hoisted out of the
236+/// per-coefficient loop -- they depend only on the transform, and this loop
237+/// runs once per coefficient of every transform unit in the stream (7.8%
238+/// self time). A zero level dequantizes to zero for every parameter set, so
239+/// the common all-but-EOB tail costs a compare instead of a 64-bit multiply
240+/// and divide. The arithmetic on a non-zero level is the spec's, unchanged.
241+#[allow(clippy::too_many_arguments)]
242+pub fn dequant_wh_into(
243+ levels: &[i32],
244+ out: &mut Vec<i32>,
245+ w: usize,
246+ h: usize,
247+ bit_depth: u8,
248+ q_idx: i32,
249+ dc_delta: i32,
250+ ac_delta: i32,
251+) {
252+ let dcq = i64::from(dc_q(bit_depth, q_idx + dc_delta));
253+ let acq = i64::from(ac_q(bit_depth, q_idx + ac_delta));
254+ let denom = dq_denom_area(w * h);
255+ let bound = 1i64 << (7 + u32::from(bit_depth));
256+ let apply = |l: i32, q: i64| -> i32 {
257+ let dq = i64::from(l) * q;
258+ let sign = if dq < 0 { -1 } else { 1 };
259+ let dq2 = sign * ((dq.abs() & 0xFF_FFFF) / denom);
260+ dq2.clamp(-bound, bound - 1) as i32
261+ };
262+ out.clear();
263+ out.reserve(levels.len());
264+ out.extend(levels.iter().map(|&l| if l == 0 { 0 } else { apply(l, acq) }));
265+ if let (Some(dst), Some(&l)) = (out.first_mut(), levels.first()) {
266+ *dst = if l == 0 { 0 } else { apply(l, dcq) };
267+ }
234 }268 }
235 269
236 #[cfg(test)]270 #[cfg(test)]
@@ -254,6 +288,29 @@ mod tests {
254 assert_eq!([4, 8, 16, 32, 64].map(dq_denom), [1, 1, 1, 2, 4]);288 assert_eq!([4, 8, 16, 32, 64].map(dq_denom), [1, 1, 1, 2, 4]);
255 }289 }
256 290
291+ /// The hoisted buffer form must agree with the per-coefficient spec
292+ /// function it replaced, at every level sign, both quantizer roles and
293+ /// every denominator band.
294+ #[test]
295+ fn buffered_dequant_matches_the_per_coefficient_form() {
296+ let levels: Vec<i32> = (0..64).map(|i| (i as i32 - 32) * 37).collect();
297+ let mut out = Vec::new();
298+ for (w, h) in [(4, 4), (16, 16), (32, 32), (64, 64), (8, 32), (16, 4)] {
299+ for bit_depth in [8u8, 10, 12] {
300+ for (q_idx, dc, ac) in [(20, 0, 0), (140, -8, 5), (255, 12, -12)] {
301+ let n = (w * h).min(levels.len());
302+ dequant_wh_into(&levels[..n], &mut out, w, h, bit_depth, q_idx, dc, ac);
303+ for (i, &got) in out.iter().enumerate() {
304+ let want = dequant_coeff_wh(
305+ levels[i], i == 0, bit_depth, q_idx, dc, ac, w, h,
306+ );
307+ assert_eq!(got, want, "{w}x{h} depth {bit_depth} q {q_idx} coeff {i}");
308+ }
309+ }
310+ }
311+ }
312+ }
313+
257 /// Negative levels round toward zero, so a level and its negation314 /// Negative levels round toward zero, so a level and its negation
258 /// dequantize to values that differ only in sign.315 /// dequantize to values that differ only in sign.
259 #[test]316 #[test]
crates/ec-av1/src/transform.rsmodified+12 -1
@@ -1003,8 +1003,19 @@ pub fn dequant_and_inverse_typed_wh(
1003 ac_delta: i32,1003 ac_delta: i32,
1004 tx_type: TxType,1004 tx_type: TxType,
1005 ) -> Vec<i32> {1005 ) -> Vec<i32> {
1006- let dq = crate::quant::dequant_wh(levels, w, h, bit_depth, q_idx, dc_delta, ac_delta);1006+ DQ_SCRATCH.with(|cell| {
1007+ let mut dq = cell.borrow_mut();
1008+ crate::quant::dequant_wh_into(levels, &mut dq, w, h, bit_depth, q_idx, dc_delta, ac_delta);
1007 inverse_transform_2d_typed_wh(&dq, w, h, bit_depth, tx_type)1009 inverse_transform_2d_typed_wh(&dq, w, h, bit_depth, tx_type)
1010+ })
1011+}
1012+
1013+thread_local! {
1014+ /// The dequantized grid, reused per thread: it lives only until the
1015+ /// inverse transform has read it, so it was a `Vec` allocated and dropped
1016+ /// once per transform unit.
1017+ static DQ_SCRATCH: std::cell::RefCell<Vec<i32>> =
1018+ const { std::cell::RefCell::new(Vec::new()) };
1008 }1019 }
1009 1020
1010 /// [`dequant_and_inverse_typed`] at `DCT_DCT`, no per-plane quantizer delta.1021 /// [`dequant_and_inverse_typed`] at `DCT_DCT`, no per-plane quantizer delta.