forge
Explore
Log in

Don't you dare go Hollow

Hoist scan-table lookups out of encode_subblock's hot RDOQ loop

Browse files at this commit

b54343a015934de92398aabb88b27464043f998fAhmet Kaan GümüşParent: 81586f2
  • perf
    • hoist scan-table lookups out of encode_subblock's hot RDOQ loop
Show full message
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.rsmodified+19 -4
@@ -522,6 +522,10 @@ pub fn rdoq(
522 enc,522 enc,
523 levels,523 levels,
524 n,524 n,
525+ log2_size,
526+ sub_wide,
527+ sub_scan,
528+ pos_scan,
525 c_idx,529 c_idx,
526 scan_idx,530 scan_idx,
527 false,531 false,
@@ -758,12 +762,23 @@ fn encode_subblock_range(
758 greater1_ctx_in: u32,762 greater1_ctx_in: u32,
759 csbf: &[bool; 64],763 csbf: &[bool; 64],
760 ) -> u32 {764 ) -> u32 {
765+ // Hoisted once for the whole range instead of re-derived by every
766+ // `encode_subblock` call: `log2_size`/`sub_wide`/the scan tables are the
767+ // same for every sub-block this loop visits.
768+ let log2_size = n.trailing_zeros();
769+ let sub_wide = n >> 2;
770+ let sub_scan = scan(log2_size - 2, scan_idx);
771+ let pos_scan = scan(2, scan_idx);
761 let mut greater1_ctx = greater1_ctx_in;772 let mut greater1_ctx = greater1_ctx_in;
762 for i in (0..=from_sub).rev() {773 for i in (0..=from_sub).rev() {
763 greater1_ctx = encode_subblock(774 greater1_ctx = encode_subblock(
764 enc,775 enc,
765 levels,776 levels,
766 n,777 n,
778+ log2_size,
779+ sub_wide,
780+ sub_scan,
781+ pos_scan,
767 c_idx,782 c_idx,
768 scan_idx,783 scan_idx,
769 sign_hiding,784 sign_hiding,
@@ -786,6 +801,10 @@ fn encode_subblock(
786 enc: &mut CabacEncoder,801 enc: &mut CabacEncoder,
787 levels: &[i32],802 levels: &[i32],
788 n: usize,803 n: usize,
804+ log2_size: u32,
805+ sub_wide: usize,
806+ sub_scan: &'static [(u8, u8)],
807+ pos_scan: &'static [(u8, u8)],
789 c_idx: usize,808 c_idx: usize,
790 scan_idx: usize,809 scan_idx: usize,
791 sign_hiding: bool,810 sign_hiding: bool,
@@ -795,10 +814,6 @@ fn encode_subblock(
795 csbf: &[bool; 64],814 csbf: &[bool; 64],
796 greater1_ctx_in: u32,815 greater1_ctx_in: u32,
797 ) -> u32 {816 ) -> u32 {
798- let log2_size = n.trailing_zeros();
799- let sub_wide = n >> 2;
800- let sub_scan = scan(log2_size - 2, scan_idx);
801- let pos_scan = scan(2, scan_idx);
802 let chroma = c_idx > 0;817 let chroma = c_idx > 0;
803 let mut greater1_ctx = greater1_ctx_in;818 let mut greater1_ctx = greater1_ctx_in;
804 819