forge
Explore
Log in

Don't you dare go Hollow

Flatten the transform matrix branch out of the hot butterfly loops

Browse files at this commit

c180d551e576913a820d2ac63cc75558b7cc2515Ahmet Kaan GümüşParent: 6efce81
  • perf
    • flatten the transform matrix branch out of the hot butterfly loops
Show full message
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.rsmodified+43 -4
@@ -65,16 +65,53 @@ const DST_MATRIX: [[i32; 4]; 4] = [
65 65
66 /// `M[m][n]` of the 32-point matrix, mirrored out of the stored half.66 /// `M[m][n]` of the 32-point matrix, mirrored out of the stored half.
67 #[inline]67 #[inline]
68-fn m32(row: usize, col: usize) -> i32 {68+const fn m32(row: usize, col: usize) -> i32 {
69 if col < 16 {69 if col < 16 {
70 TRANS_MATRIX_HALF[row][col]70 TRANS_MATRIX_HALF[row][col]
71- } else if row.is_multiple_of(2) {71+ } else if row % 2 == 0 {
72 TRANS_MATRIX_HALF[row][31 - col]72 TRANS_MATRIX_HALF[row][31 - col]
73 } else {73 } else {
74 -TRANS_MATRIX_HALF[row][31 - col]74 -TRANS_MATRIX_HALF[row][31 - col]
75 }75 }
76 }76 }
77 77
78+/// The full 32x32 matrix, mirrored out at compile time so the hot butterfly
79+/// loops below index a flat table instead of taking [`m32`]'s per-element
80+/// branch: same values (this *is* [`m32`], evaluated ahead of time), fewer
81+/// instructions per multiply-add.
82+const fn build_full_matrix() -> [[i32; 32]; 32] {
83+ let mut out = [[0i32; 32]; 32];
84+ let mut row = 0;
85+ while row < 32 {
86+ let mut col = 0;
87+ while col < 32 {
88+ out[row][col] = m32(row, col);
89+ col += 1;
90+ }
91+ row += 1;
92+ }
93+ out
94+}
95+const FULL_MATRIX: [[i32; 32]; 32] = build_full_matrix();
96+
97+/// [`FULL_MATRIX`] transposed: `FULL_MATRIX_T[col][row] == FULL_MATRIX[row][col]`.
98+/// [`inverse_1d`]'s odd sum walks a fixed column across varying rows, which
99+/// this makes a contiguous read.
100+const fn transpose(m: &[[i32; 32]; 32]) -> [[i32; 32]; 32] {
101+ let mut out = [[0i32; 32]; 32];
102+ let mut row = 0;
103+ while row < 32 {
104+ let mut col = 0;
105+ while col < 32 {
106+ out[col][row] = m[row][col];
107+ col += 1;
108+ }
109+ row += 1;
110+ }
111+ out
112+}
113+const FULL_MATRIX_T: [[i32; 32]; 32] = transpose(&FULL_MATRIX);
114+
78 /// `levelScale[k]`, 8.6.3.115 /// `levelScale[k]`, 8.6.3.
79 const LEVEL_SCALE: [i32; 6] = [40, 45, 51, 57, 64, 72];116 const LEVEL_SCALE: [i32; 6] = [40, 45, 51, 57, 64, 72];
80 117
@@ -110,8 +147,9 @@ fn inverse_1d(n: usize, src: &[i32], dst: &mut [i32]) {
110 inverse_1d(half, &even_src[..half], &mut even[..half]);147 inverse_1d(half, &even_src[..half], &mut even[..half]);
111 for x in 0..half {148 for x in 0..half {
112 let mut odd = 0i32;149 let mut odd = 0i32;
150+ let matrix_col = &FULL_MATRIX_T[x];
113 for j in 0..half {151 for j in 0..half {
114- odd += m32((2 * j + 1) * stride, x) * src[2 * j + 1];152+ odd += matrix_col[(2 * j + 1) * stride] * src[2 * j + 1];
115 }153 }
116 dst[x] = even[x] + odd;154 dst[x] = even[x] + odd;
117 dst[n - 1 - x] = even[x] - odd;155 dst[n - 1 - x] = even[x] - odd;
@@ -141,8 +179,9 @@ fn forward_1d(n: usize, src: &[i32], dst: &mut [i32]) {
141 for j in 0..half {179 for j in 0..half {
142 dst[2 * j] = even[j];180 dst[2 * j] = even[j];
143 let mut odd = 0i32;181 let mut odd = 0i32;
182+ let matrix_row = &FULL_MATRIX[(2 * j + 1) * stride];
144 for (x, &diff) in diffs[..half].iter().enumerate() {183 for (x, &diff) in diffs[..half].iter().enumerate() {
145- odd += m32((2 * j + 1) * stride, x) * diff;184+ odd += matrix_row[x] * diff;
146 }185 }
147 dst[2 * j + 1] = odd;186 dst[2 * j + 1] = odd;
148 }187 }
crates/ec-h265/tests/rdoq_cache_hash.rsmodified+13 -0
@@ -40,3 +40,16 @@ fn rdoq_cache_hash_1920x1080() {
40 fn rdoq_cache_hash_416x240() {40 fn rdoq_cache_hash_416x240() {
41 println!("hash 416x240 = {:016x}", encode_hash(416, 240));41 println!("hash 416x240 = {:016x}", encode_hash(416, 240));
42 }42 }
43+
44+/// lane-h265simd: pinned bit-identity gate for the transform's hot path. The
45+/// value is the same hash the two tests above print, verified by hand
46+/// (`git stash` the transform.rs change, rerun, compare) to be identical
47+/// before and after replacing `m32()`'s per-element branch with a
48+/// compile-time-mirrored flat table in `forward_1d`/`inverse_1d` — this pins
49+/// that equality so a future change to the hot path is caught if it ever
50+/// produces a different byte.
51+#[test]
52+fn rdoq_cache_hash_is_pinned() {
53+ assert_eq!(encode_hash(416, 240), 0xda5d606db0755d97);
54+ assert_eq!(encode_hash(1920, 1080), 0xf08a816c9fa9ca2c);
55+}