From 56cc23228a15f88829706c2789ab160c7579a010 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 05:27:01 +0300 Subject: [PATCH] perf(av1): cached FFT stage twiddles, stack scratch for a motion-compensated trial Two allocation/libm costs on the encoder's hottest paths, neither of which changes a single output byte: - `fft`'s stage twiddle depends only on the stage length, so its two `sincos` calls per stage per call become one lazily-built table of the seven values that exist (`__sincos_fma` was 9% of the encoder profile). - `mc_trial` heap-allocated a `u16` prediction and a `u8` copy of it for every candidate trial; both are at most 32x32 and now live on the stack. instructions:u on the 24-frame 1280x720 sequence bench: 283.6G -> 263.5G (-7.1%); output bytes unchanged (64931), BD-rate unchanged on all three gate clips. --- crates/ec-av1/src/encode.rs | 17 +++++++++++++---- crates/ec-av1/src/transform.rs | 14 ++++++++++++-- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs index 713a1aa8..69d9d67c 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -2766,7 +2766,12 @@ fn mc_trial( // lane-hbd r4: `reference` (a DPB `Picture`'s plane) is `u16` now that // `Picture` is widened; `prediction`/the encoder stay `u8` (encoder is // 8-bit only this round, see `intra_predict_u8`'s doc comment). - let mut prediction16 = vec![0u16; side * side]; + // lane-av1speed: both buffers are per-candidate scratch on the hottest + // path in the encoder, so they live on the stack -- `side` is at most + // `BLOCK` (32) for luma and half that for chroma. Two heap allocations + // per motion-compensated trial otherwise. + let mut prediction16 = [0u16; BLOCK * BLOCK]; + let prediction16 = &mut prediction16[..side * side]; mc::predict( reference, stride, @@ -2776,10 +2781,14 @@ fn mc_trial( y_q4, side, side, - &mut prediction16, fctx, + prediction16, fctx, ); - let prediction: Vec = prediction16.iter().map(|&v| v as u8).collect(); - plane.code_from_prediction(x, y, side, &prediction, skip, base_q_idx, deadzone, set) + let mut prediction = [0u8; BLOCK * BLOCK]; + let prediction = &mut prediction[..side * side]; + for (dst, &src) in prediction.iter_mut().zip(prediction16.iter()) { + *dst = src as u8; + } + plane.code_from_prediction(x, y, side, prediction, skip, base_q_idx, deadzone, set) } /// Codes one 32x32 block of an inter frame as whichever costs least of: each diff --git a/crates/ec-av1/src/transform.rs b/crates/ec-av1/src/transform.rs index 6e4546bd..03584d55 100644 --- a/crates/ec-av1/src/transform.rs +++ b/crates/ec-av1/src/transform.rs @@ -1444,8 +1444,18 @@ fn fft(a: &mut [Complex]) { } let mut len = 2; while len <= n { - let ang = -2.0 * std::f64::consts::PI / len as f64; - let wlen = (ang.cos(), ang.sin()); + // lane-av1speed: the stage twiddle depends on `len` alone, so the + // eight values this loop can ever ask for are computed once instead + // of two libm `sincos` calls per stage per call (`__sincos_fma` was + // 9% of the encoder's profile). Same values, so the recurrence below + // -- and every coefficient it produces -- is bit-identical. + static WLEN: std::sync::OnceLock<[(f64, f64); 7]> = std::sync::OnceLock::new(); + let wlen = WLEN.get_or_init(|| { + std::array::from_fn(|i| { + let ang = -2.0 * std::f64::consts::PI / (2u32 << i) as f64; + (ang.cos(), ang.sin()) + }) + })[len.trailing_zeros() as usize - 1]; let mut i = 0; while i < n { let mut w = (1.0, 0.0);