From c180d551e576913a820d2ac63cc75558b7cc2515 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Thu, 27 Aug 2026 13:30:57 +0300 Subject: [PATCH] perf(h265): flatten the transform matrix branch out of the hot butterfly loops Profiled the encode path first (perf record --call-graph dwarf on a 1080p single-thread encode_idr loop): forward_1d is 28% of self-time, inverse_1d 9.7%, well ahead of anything in residual.rs/cabac.rs (residual::rdoq is 2.2%, cabac put_bit 0.9%) -- the RDOQ/CABAC bookkeeping levers named in the charter are not where the time is; the integer transform is. m32() computed each matrix entry on the fly with a branch (col<16 / row parity) inside forward_1d's and inverse_1d's innermost multiply-accumulate loop. Mirrored the same values out to two flat 32x32 tables at compile time (FULL_MATRIX and its transpose FULL_MATRIX_T, both built by a const fn that literally calls m32 -- so this is m32 evaluated ahead of time, not a new derivation) and index them instead, so the hot loop is a branchless multiply-add over a contiguous array read. perf stat -e instructions,cycles on the same 4-frame 1080p single-thread workload, best of 3: 105.7B -> 97.2B instructions (-8.1%), 22.6B -> 17.8B cycles (-21%, before run noisy at +-10.5%). Short of the 15% instruction target; intra::predict (12% of self-time) and quantize_offset (6.4%) are the next levers and are untouched. Bit-identical: rdoq_cache_hash's 416x240 and 1920x1080 encodes hash the same (da5d606db0755d97, f08a816c9fa9ca2c) with git-stashed transform.rs before this change as with it; pinned that equality as an assertion (rdoq_cache_hash_is_pinned) since the existing test only printed. Full ec-h265 --release suite green (56 tests across 7 binaries). --- crates/ec-h265/src/transform.rs | 47 ++++++++++++++++++++++--- crates/ec-h265/tests/rdoq_cache_hash.rs | 13 +++++++ 2 files changed, 56 insertions(+), 4 deletions(-) diff --git a/crates/ec-h265/src/transform.rs b/crates/ec-h265/src/transform.rs index d68be9f2..2038c86e 100644 --- a/crates/ec-h265/src/transform.rs +++ b/crates/ec-h265/src/transform.rs @@ -65,16 +65,53 @@ const DST_MATRIX: [[i32; 4]; 4] = [ /// `M[m][n]` of the 32-point matrix, mirrored out of the stored half. #[inline] -fn m32(row: usize, col: usize) -> i32 { +const fn m32(row: usize, col: usize) -> i32 { if col < 16 { TRANS_MATRIX_HALF[row][col] - } else if row.is_multiple_of(2) { + } else if row % 2 == 0 { TRANS_MATRIX_HALF[row][31 - col] } else { -TRANS_MATRIX_HALF[row][31 - col] } } +/// The full 32x32 matrix, mirrored out at compile time so the hot butterfly +/// loops below index a flat table instead of taking [`m32`]'s per-element +/// branch: same values (this *is* [`m32`], evaluated ahead of time), fewer +/// instructions per multiply-add. +const fn build_full_matrix() -> [[i32; 32]; 32] { + let mut out = [[0i32; 32]; 32]; + let mut row = 0; + while row < 32 { + let mut col = 0; + while col < 32 { + out[row][col] = m32(row, col); + col += 1; + } + row += 1; + } + out +} +const FULL_MATRIX: [[i32; 32]; 32] = build_full_matrix(); + +/// [`FULL_MATRIX`] transposed: `FULL_MATRIX_T[col][row] == FULL_MATRIX[row][col]`. +/// [`inverse_1d`]'s odd sum walks a fixed column across varying rows, which +/// this makes a contiguous read. +const fn transpose(m: &[[i32; 32]; 32]) -> [[i32; 32]; 32] { + let mut out = [[0i32; 32]; 32]; + let mut row = 0; + while row < 32 { + let mut col = 0; + while col < 32 { + out[col][row] = m[row][col]; + col += 1; + } + row += 1; + } + out +} +const FULL_MATRIX_T: [[i32; 32]; 32] = transpose(&FULL_MATRIX); + /// `levelScale[k]`, 8.6.3. const LEVEL_SCALE: [i32; 6] = [40, 45, 51, 57, 64, 72]; @@ -110,8 +147,9 @@ fn inverse_1d(n: usize, src: &[i32], dst: &mut [i32]) { inverse_1d(half, &even_src[..half], &mut even[..half]); for x in 0..half { let mut odd = 0i32; + let matrix_col = &FULL_MATRIX_T[x]; for j in 0..half { - odd += m32((2 * j + 1) * stride, x) * src[2 * j + 1]; + odd += matrix_col[(2 * j + 1) * stride] * src[2 * j + 1]; } dst[x] = even[x] + odd; dst[n - 1 - x] = even[x] - odd; @@ -141,8 +179,9 @@ fn forward_1d(n: usize, src: &[i32], dst: &mut [i32]) { for j in 0..half { dst[2 * j] = even[j]; let mut odd = 0i32; + let matrix_row = &FULL_MATRIX[(2 * j + 1) * stride]; for (x, &diff) in diffs[..half].iter().enumerate() { - odd += m32((2 * j + 1) * stride, x) * diff; + odd += matrix_row[x] * diff; } dst[2 * j + 1] = odd; } diff --git a/crates/ec-h265/tests/rdoq_cache_hash.rs b/crates/ec-h265/tests/rdoq_cache_hash.rs index 6dcce82d..3519942c 100644 --- a/crates/ec-h265/tests/rdoq_cache_hash.rs +++ b/crates/ec-h265/tests/rdoq_cache_hash.rs @@ -40,3 +40,16 @@ fn rdoq_cache_hash_1920x1080() { fn rdoq_cache_hash_416x240() { println!("hash 416x240 = {:016x}", encode_hash(416, 240)); } + +/// lane-h265simd: pinned bit-identity gate for the transform's hot path. The +/// value is the same hash the two tests above print, verified by hand +/// (`git stash` the transform.rs change, rerun, compare) to be identical +/// before and after replacing `m32()`'s per-element branch with a +/// compile-time-mirrored flat table in `forward_1d`/`inverse_1d` — this pins +/// that equality so a future change to the hot path is caught if it ever +/// produces a different byte. +#[test] +fn rdoq_cache_hash_is_pinned() { + assert_eq!(encode_hash(416, 240), 0xda5d606db0755d97); + assert_eq!(encode_hash(1920, 1080), 0xf08a816c9fa9ca2c); +}