From 8e31689dbe4287432178abea964cfc0544991d12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Sun, 6 Sep 2026 22:08:07 +0300 Subject: [PATCH] perf(av1): skip the all-zero re-inflate and the pricer's table restore 60% of an inter stream's transform units carry no coefficient. Two places paid full price for them anyway: the square dequant/inverse entry point re-inflated the empty all-zero marker into side*side zeros (an allocate and memset per trial) only for the reconstruction sum to add them to the prediction, and coeff_bits restored the eight coefficient tables write_coeffs might touch (~800 bytes of copies) before pricing what is one all-zero flag and nothing else. Both stay byte-exact: clamp(p + 0) is p for a u8, and the restored txb_skip row the flag is priced against is the pristine row it was copied from. Byte pins and the enc_probe FNV sets over the three gate clips at q150/120/90/60 are identical. --- crates/ec-av1/src/encode.rs | 56 +++++++++++++++++++++++++++---------- crates/ec-av1/src/tile.rs | 19 +++++++++++-- 2 files changed, 58 insertions(+), 17 deletions(-) diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs index 5bc81066..bb9e272f 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -41,7 +41,7 @@ use crate::tile::{ BlockCoeffs, Coeff, INTRA_MODE_CTX, InterInfo, InterMode, Quadrant, Superblock, partition_bits, }; use crate::transform::{ - TxType, dequant_and_inverse, dequant_and_inverse_typed, forward_and_quantize, + TxType, dequant_and_inverse_typed_wh, forward_and_quantize, forward_and_quantize_typed, }; @@ -1539,16 +1539,28 @@ impl Plane<'_> { let t = std::time::Instant::now(); let levels = forward_and_quantize_typed(residual, side, 8, i32::from(base_q_idx), deadzone, tx_type); - let coded = - dequant_and_inverse_typed(&levels, side, 8, i32::from(base_q_idx), 0, 0, tx_type); + // lane-av1speed2: 60% of the transform units of an inter stream carry + // no coefficient at all, and the square entry point re-inflates that + // case into `side * side` zeros (one allocate-and-memset per trial) + // only for the sum below to add them to the prediction. The `_wh` + // entry hands back the empty all-zero marker instead, and a zero + // residual reconstructs to the prediction itself -- `clamp(p + 0)` is + // `p` for a `u8`, so the bytes are the same. + let coded = dequant_and_inverse_typed_wh( + &levels, side, side, 8, i32::from(base_q_idx), 0, 0, tx_type, + ); #[cfg(test)] stage_add(2, t.elapsed()); - let reconstruction: Vec = prediction - .iter() - .zip(&coded) - .map(|(&p, &c)| (i32::from(p) + c).clamp(0, 255) as u8) - .collect(); + let reconstruction: Vec = if coded.is_empty() { + prediction.to_vec() + } else { + prediction + .iter() + .zip(&coded) + .map(|(&p, &c)| (i32::from(p) + c).clamp(0, 255) as u8) + .collect() + }; let sse = self.block_sse(x, y, side, &reconstruction); // What the levels cost is priced through the same CDFs the tile writer // will code them with, so the search ranks modes -- and the partition @@ -1626,14 +1638,28 @@ impl Plane<'_> { #[cfg(test)] let t = std::time::Instant::now(); let levels = forward_and_quantize(residual, side, 8, i32::from(base_q_idx), deadzone); - let coded = dequant_and_inverse(&levels, side, 8, i32::from(base_q_idx)); + // The same all-zero shortcut [`Self::trial_typed`] takes. + let coded = dequant_and_inverse_typed_wh( + &levels, + side, + side, + 8, + i32::from(base_q_idx), + 0, + 0, + TxType::DctDct, + ); #[cfg(test)] stage_add(2, t.elapsed()); - let reconstruction: Vec = prediction - .iter() - .zip(&coded) - .map(|(&p, &c)| (i32::from(p) + c).clamp(0, 255) as u8) - .collect(); + let reconstruction: Vec = if coded.is_empty() { + prediction.to_vec() + } else { + prediction + .iter() + .zip(&coded) + .map(|(&p, &c)| (i32::from(p) + c).clamp(0, 255) as u8) + .collect() + }; let sse = self.block_sse(x, y, side, &reconstruction); #[cfg(test)] let t = std::time::Instant::now(); @@ -11039,7 +11065,7 @@ mod tests { let t = Instant::now(); for _ in 0..N { - std::hint::black_box(dequant_and_inverse(&levels, side, 8, 100)); + std::hint::black_box(crate::transform::dequant_and_inverse(&levels, side, 8, 100)); } let inverse_t = t.elapsed() / N; diff --git a/crates/ec-av1/src/tile.rs b/crates/ec-av1/src/tile.rs index 883e3939..1234fad1 100644 --- a/crates/ec-av1/src/tile.rs +++ b/crates/ec-av1/src/tile.rs @@ -4257,14 +4257,29 @@ pub(crate) fn coeff_bits( write_coeffs(&mut enc, &mut coding, grid, scan, skip_ctx, sign_ctx, None); enc.bits() }; + // lane-av1speed2: 60% of the transform units of an inter stream hold no + // coefficient, and for those `write_coeffs` codes ONE symbol -- the + // all-zero flag -- and returns. Restoring the eight tables it might have + // touched (~800 bytes of `copy_from_slice`, the largest `memmove` in the + // encoder's profile) buys nothing there, so price that symbol straight + // off the pristine table on a copy of its own row. Identical bits: the + // scratch's `txb_skip[skip_ctx]` was restored from exactly this row, and + // nothing after the flag runs. + let zero_price = |slot: &mut (Cdfs, Cdfs)| { + let mut row = slot.0.txb(set, DC_PRED).txb_skip[skip_ctx]; + let mut enc = SymbolEncoder::pricer(); + enc.symbol(1, &mut row); + enc.bits() + }; + let all_zero = grid.iter().all(|&level| level == 0); PRICING_BASE.with_borrow_mut(|base| { if let Some(slot) = base.as_deref_mut().filter(|s| s.0.q_ctx == q_ctx) { - return price(slot); + return if all_zero { zero_price(slot) } else { price(slot) }; } PRICING.with_borrow_mut(|slots| { let slot = slots[q_ctx.min(3)] .get_or_insert_with(|| Box::new((Cdfs::new(q_ctx), Cdfs::new(q_ctx)))); - price(slot) + if all_zero { zero_price(slot) } else { price(slot) } }) }) }