forge
Explore
Log in

Don't you dare go Hollow

Hoist sig_ctx_inc's sub-block-invariant part out of the position loop

Browse files at this commit

07800cd04909e45ba20931381ea4e6c01f584c90Ahmet Kaan GümüşParent: b9e8391
  • perf
    • hoist sig_ctx_inc's sub-block-invariant part out of the position loop
Show full message
perf(h265): hoist sig_ctx_inc's sub-block-invariant part out of the position loop

prev_csbf and the additive context base in sig_ctx_inc depend only on the
sub-block's own coordinates (xs/ys), never on the scan position within it,
but were recomputed on every one of up to 15 significant-flag positions per
sub-block. Split into sig_ctx_base (once per sub-block) + sig_ctx_inc (per
position), shared by encode_subblock and decode_residual so both stay in
sync. Correctness gate green (cargo test -p ec-h265 --release,
rdoq_cache_hash_is_pinned unmoved). Instructions over the 4-frame 1080p
single-thread profile loop, stacked on the transform SIMD commit:
142,473,095,572 -> 141,277,415,462 (-0.84%).
crates/ec-h265/src/residual.rsmodified+52 -27
@@ -836,6 +836,7 @@ fn encode_subblock(
836836 } else {
837837 15
838838 };
839+ let sig_base = sig_ctx_base(xs, ys, sub_wide, csbf, log2_size, c_idx, scan_idx);
839840 let mut scan_pos = first;
840841 while scan_pos >= 0 {
841842 let p = scan_pos as usize;
@@ -845,7 +846,7 @@ fn encode_subblock(
845846 let yc = ys * 4 + yp as usize;
846847 let sig = levels[yc * n + xc] != 0;
847848 if p > 0 || !infer_dc_sig {
848- let inc = sig_ctx_inc(xc, yc, xs, ys, sub_wide, csbf, log2_size, c_idx, scan_idx);
849+ let inc = sig_ctx_inc(xc, yc, log2_size, c_idx, sig_base);
849850 enc.encode_bin(ctx::SIG_COEFF + inc, u32::from(sig));
850851 }
851852 significant[p] = sig;
@@ -1095,6 +1096,7 @@ pub fn decode_residual(
10951096 } else {
10961097 15
10971098 };
1099+ let sig_base = sig_ctx_base(xs, ys, sub_wide, &csbf, log2_size, c_idx, scan_idx);
10981100 let mut scan_pos = first;
10991101 while scan_pos >= 0 {
11001102 let p = scan_pos as usize;
@@ -1103,7 +1105,7 @@ pub fn decode_residual(
11031105 let xc = xs * 4 + xp as usize;
11041106 let yc = ys * 4 + yp as usize;
11051107 let sig = if p > 0 || !infer_dc_sig {
1106- let inc = sig_ctx_inc(xc, yc, xs, ys, sub_wide, &csbf, log2_size, c_idx, scan_idx);
1108+ let inc = sig_ctx_inc(xc, yc, log2_size, c_idx, sig_base);
11071109 dec.decode_bin(ctx::SIG_COEFF + inc) != 0
11081110 } else {
11091111 false // filled in by the post-loop inference below
@@ -1202,9 +1204,13 @@ pub fn decode_residual(
12021204
12031205 /// `ctxInc` for `sig_coeff_flag` (9.3.4.2.5).
12041206 #[allow(clippy::too_many_arguments)]
1205-fn sig_ctx_inc(
1206- xc: usize,
1207- yc: usize,
1207+/// Sub-block-invariant part of [`sig_ctx_inc`]'s context: `prev_csbf` and the
1208+/// additive base both depend only on the sub-block's own coordinates
1209+/// (`xs`/`ys`), never on the position within it, so a caller stepping through
1210+/// up to 15 significant positions in one sub-block computes this once instead
1211+/// of on every position. `None` when `log2_size == 2` (the small-block path
1212+/// doesn't use `prev_csbf`/base at all — [`sig_ctx_inc`] handles it directly).
1213+fn sig_ctx_base(
12081214 xs: usize,
12091215 ys: usize,
12101216 sub_wide: usize,
@@ -1212,20 +1218,52 @@ fn sig_ctx_inc(
12121218 log2_size: u32,
12131219 c_idx: usize,
12141220 scan_idx: usize,
1221+) -> Option<(usize, usize)> {
1222+ if log2_size == 2 {
1223+ return None;
1224+ }
1225+ let mut prev_csbf = 0usize;
1226+ if xs < sub_wide - 1 && csbf[ys * sub_wide + xs + 1] {
1227+ prev_csbf += 1;
1228+ }
1229+ if ys < sub_wide - 1 && csbf[(ys + 1) * sub_wide + xs] {
1230+ prev_csbf += 2;
1231+ }
1232+ let mut base = 0usize;
1233+ if c_idx == 0 {
1234+ if xs + ys > 0 {
1235+ base += 3;
1236+ }
1237+ if log2_size == 3 {
1238+ base += if scan_idx == 0 { 9 } else { 15 };
1239+ } else {
1240+ base += 21;
1241+ }
1242+ } else if log2_size == 3 {
1243+ base += 9;
1244+ } else {
1245+ base += 12;
1246+ }
1247+ if c_idx != 0 {
1248+ base += 27;
1249+ }
1250+ Some((prev_csbf, base))
1251+}
1252+
1253+fn sig_ctx_inc(
1254+ xc: usize,
1255+ yc: usize,
1256+ log2_size: u32,
1257+ c_idx: usize,
1258+ base: Option<(usize, usize)>,
12151259 ) -> usize {
1216- let mut sig_ctx;
1260+ let sig_ctx;
12171261 if log2_size == 2 {
12181262 sig_ctx = CTX_IDX_MAP[(yc << 2) + xc];
12191263 } else if xc + yc == 0 {
12201264 sig_ctx = 0;
12211265 } else {
1222- let mut prev_csbf = 0usize;
1223- if xs < sub_wide - 1 && csbf[ys * sub_wide + xs + 1] {
1224- prev_csbf += 1;
1225- }
1226- if ys < sub_wide - 1 && csbf[(ys + 1) * sub_wide + xs] {
1227- prev_csbf += 2;
1228- }
1266+ let (prev_csbf, base) = base.expect("sig_ctx_base returns Some when log2_size != 2");
12291267 let (xp, yp) = (xc & 3, yc & 3);
12301268 sig_ctx = match prev_csbf {
12311269 0 => {
@@ -1257,20 +1295,7 @@ fn sig_ctx_inc(
12571295 }
12581296 _ => 2,
12591297 };
1260- if c_idx == 0 {
1261- if xs + ys > 0 {
1262- sig_ctx += 3;
1263- }
1264- if log2_size == 3 {
1265- sig_ctx += if scan_idx == 0 { 9 } else { 15 };
1266- } else {
1267- sig_ctx += 21;
1268- }
1269- } else if log2_size == 3 {
1270- sig_ctx += 9;
1271- } else {
1272- sig_ctx += 12;
1273- }
1298+ return sig_ctx + base;
12741299 }
12751300 if c_idx == 0 { sig_ctx } else { 27 + sig_ctx }
12761301 }