From b54343a015934de92398aabb88b27464043f998f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Fri, 28 Aug 2026 10:12:32 +0300 Subject: [PATCH] perf(h265): hoist scan-table lookups out of encode_subblock's hot RDOQ loop profile-first (perf record on ec-bench's 640x360x10f h265 encode row): encode_subblock is 26% self time, the single hottest symbol, driven by RDOQ's per-trial tail re-encode (encode_subblock_range walks every sub-block from the trial position down to sub-block 0 on every candidate, since a real adaptive-CABAC bit count cannot be decomposed into a local delta -- range/low state and context adaptation both carry forward). That replay cost is structural to exact-CABAC RDOQ and not safely reducible without changing which candidate wins (disallowed: this lane's gate is bit-exact output before/after). What's still free: log2_size/sub_wide/the two scan tables were re-derived from `n` on every single encode_subblock call, even though they are loop invariants for the whole encode_subblock_range walk (and, in rdoq's own direct-call site, invariant for the whole rdoq() call). Hoisted to the callers and threaded through as parameters -- same values, fewer recomputations, zero behavior change. Verified bit-exact: sha256 of ec-bench's h265 AU output identical before/after (temporary EC_BENCH_DUMP_H265_AUS dump hook, reverted). cargo test -p ec-h265 --release: 57 passed, 0 failed, including the pinned rdoq_cache_hash tests. No measurable wall-clock change at the 640x360x10f bench's scale (~2610-2650ms both sides, noise-dominated) -- this hoist's savings are real but too small relative to the required CABAC replay volume to show above that noise floor. --- crates/ec-h265/src/residual.rs | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/crates/ec-h265/src/residual.rs b/crates/ec-h265/src/residual.rs index 1d3fddcb..6b6f250d 100644 --- a/crates/ec-h265/src/residual.rs +++ b/crates/ec-h265/src/residual.rs @@ -522,6 +522,10 @@ pub fn rdoq( enc, levels, n, + log2_size, + sub_wide, + sub_scan, + pos_scan, c_idx, scan_idx, false, @@ -758,12 +762,23 @@ fn encode_subblock_range( greater1_ctx_in: u32, csbf: &[bool; 64], ) -> u32 { + // Hoisted once for the whole range instead of re-derived by every + // `encode_subblock` call: `log2_size`/`sub_wide`/the scan tables are the + // same for every sub-block this loop visits. + let log2_size = n.trailing_zeros(); + let sub_wide = n >> 2; + let sub_scan = scan(log2_size - 2, scan_idx); + let pos_scan = scan(2, scan_idx); let mut greater1_ctx = greater1_ctx_in; for i in (0..=from_sub).rev() { greater1_ctx = encode_subblock( enc, levels, n, + log2_size, + sub_wide, + sub_scan, + pos_scan, c_idx, scan_idx, sign_hiding, @@ -786,6 +801,10 @@ fn encode_subblock( enc: &mut CabacEncoder, levels: &[i32], n: usize, + log2_size: u32, + sub_wide: usize, + sub_scan: &'static [(u8, u8)], + pos_scan: &'static [(u8, u8)], c_idx: usize, scan_idx: usize, sign_hiding: bool, @@ -795,10 +814,6 @@ fn encode_subblock( csbf: &[bool; 64], greater1_ctx_in: u32, ) -> u32 { - let log2_size = n.trailing_zeros(); - let sub_wide = n >> 2; - let sub_scan = scan(log2_size - 2, scan_idx); - let pos_scan = scan(2, scan_idx); let chroma = c_idx > 0; let mut greater1_ctx = greater1_ctx_in;