forge
Explore
Log in

Don't you dare go Hollow

Cached FFT stage twiddles, stack scratch for a motion-compensated trial

Browse files at this commit

56cc23228a15f88829706c2789ab160c7579a010Ahmet Kaan GümüşParent: 06abb60
  • perf
    • cached FFT stage twiddles, stack scratch for a motion-compensated trial
Show full message
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.rsmodified+13 -4
@@ -2766,7 +2766,12 @@ fn mc_trial(
2766 // lane-hbd r4: `reference` (a DPB `Picture`'s plane) is `u16` now that2766 // lane-hbd r4: `reference` (a DPB `Picture`'s plane) is `u16` now that
2767 // `Picture` is widened; `prediction`/the encoder stay `u8` (encoder is2767 // `Picture` is widened; `prediction`/the encoder stay `u8` (encoder is
2768 // 8-bit only this round, see `intra_predict_u8`'s doc comment).2768 // 8-bit only this round, see `intra_predict_u8`'s doc comment).
2769- let mut prediction16 = vec![0u16; side * side];2769+ // lane-av1speed: both buffers are per-candidate scratch on the hottest
2770+ // path in the encoder, so they live on the stack -- `side` is at most
2771+ // `BLOCK` (32) for luma and half that for chroma. Two heap allocations
2772+ // per motion-compensated trial otherwise.
2773+ let mut prediction16 = [0u16; BLOCK * BLOCK];
2774+ let prediction16 = &mut prediction16[..side * side];
2770 mc::predict(2775 mc::predict(
2771 reference,2776 reference,
2772 stride,2777 stride,
@@ -2776,10 +2781,14 @@ fn mc_trial(
2776 y_q4,2781 y_q4,
2777 side,2782 side,
2778 side,2783 side,
2779- &mut prediction16, fctx,2784+ prediction16, fctx,
2780 );2785 );
2781- let prediction: Vec<u8> = prediction16.iter().map(|&v| v as u8).collect();2786+ let mut prediction = [0u8; BLOCK * BLOCK];
2782- plane.code_from_prediction(x, y, side, &prediction, skip, base_q_idx, deadzone, set)2787+ let prediction = &mut prediction[..side * side];
2788+ for (dst, &src) in prediction.iter_mut().zip(prediction16.iter()) {
2789+ *dst = src as u8;
2790+ }
2791+ plane.code_from_prediction(x, y, side, prediction, skip, base_q_idx, deadzone, set)
2783 }2792 }
2784 2793
2785 /// Codes one 32x32 block of an inter frame as whichever costs least of: each2794 /// Codes one 32x32 block of an inter frame as whichever costs least of: each
crates/ec-av1/src/transform.rsmodified+12 -2
@@ -1444,8 +1444,18 @@ fn fft(a: &mut [Complex]) {
1444 }1444 }
1445 let mut len = 2;1445 let mut len = 2;
1446 while len <= n {1446 while len <= n {
1447- let ang = -2.0 * std::f64::consts::PI / len as f64;1447+ // lane-av1speed: the stage twiddle depends on `len` alone, so the
1448- let wlen = (ang.cos(), ang.sin());1448+ // eight values this loop can ever ask for are computed once instead
1449+ // of two libm `sincos` calls per stage per call (`__sincos_fma` was
1450+ // 9% of the encoder's profile). Same values, so the recurrence below
1451+ // -- and every coefficient it produces -- is bit-identical.
1452+ static WLEN: std::sync::OnceLock<[(f64, f64); 7]> = std::sync::OnceLock::new();
1453+ let wlen = WLEN.get_or_init(|| {
1454+ std::array::from_fn(|i| {
1455+ let ang = -2.0 * std::f64::consts::PI / (2u32 << i) as f64;
1456+ (ang.cos(), ang.sin())
1457+ })
1458+ })[len.trailing_zeros() as usize - 1];
1449 let mut i = 0;1459 let mut i = 0;
1450 while i < n {1460 while i < n {
1451 let mut w = (1.0, 0.0);1461 let mut w = (1.0, 0.0);