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(
836 } else {836 } else {
837 15837 15
838 };838 };
839+ let sig_base = sig_ctx_base(xs, ys, sub_wide, csbf, log2_size, c_idx, scan_idx);
839 let mut scan_pos = first;840 let mut scan_pos = first;
840 while scan_pos >= 0 {841 while scan_pos >= 0 {
841 let p = scan_pos as usize;842 let p = scan_pos as usize;
@@ -845,7 +846,7 @@ fn encode_subblock(
845 let yc = ys * 4 + yp as usize;846 let yc = ys * 4 + yp as usize;
846 let sig = levels[yc * n + xc] != 0;847 let sig = levels[yc * n + xc] != 0;
847 if p > 0 || !infer_dc_sig {848 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);
849 enc.encode_bin(ctx::SIG_COEFF + inc, u32::from(sig));850 enc.encode_bin(ctx::SIG_COEFF + inc, u32::from(sig));
850 }851 }
851 significant[p] = sig;852 significant[p] = sig;
@@ -1095,6 +1096,7 @@ pub fn decode_residual(
1095 } else {1096 } else {
1096 151097 15
1097 };1098 };
1099+ let sig_base = sig_ctx_base(xs, ys, sub_wide, &csbf, log2_size, c_idx, scan_idx);
1098 let mut scan_pos = first;1100 let mut scan_pos = first;
1099 while scan_pos >= 0 {1101 while scan_pos >= 0 {
1100 let p = scan_pos as usize;1102 let p = scan_pos as usize;
@@ -1103,7 +1105,7 @@ pub fn decode_residual(
1103 let xc = xs * 4 + xp as usize;1105 let xc = xs * 4 + xp as usize;
1104 let yc = ys * 4 + yp as usize;1106 let yc = ys * 4 + yp as usize;
1105 let sig = if p > 0 || !infer_dc_sig {1107 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);
1107 dec.decode_bin(ctx::SIG_COEFF + inc) != 01109 dec.decode_bin(ctx::SIG_COEFF + inc) != 0
1108 } else {1110 } else {
1109 false // filled in by the post-loop inference below1111 false // filled in by the post-loop inference below
@@ -1202,9 +1204,13 @@ pub fn decode_residual(
1202 1204
1203 /// `ctxInc` for `sig_coeff_flag` (9.3.4.2.5).1205 /// `ctxInc` for `sig_coeff_flag` (9.3.4.2.5).
1204 #[allow(clippy::too_many_arguments)]1206 #[allow(clippy::too_many_arguments)]
1205-fn sig_ctx_inc(1207+/// Sub-block-invariant part of [`sig_ctx_inc`]'s context: `prev_csbf` and the
1206- xc: usize,1208+/// additive base both depend only on the sub-block's own coordinates
1207- yc: usize,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(
1208 xs: usize,1214 xs: usize,
1209 ys: usize,1215 ys: usize,
1210 sub_wide: usize,1216 sub_wide: usize,
@@ -1212,20 +1218,52 @@ fn sig_ctx_inc(
1212 log2_size: u32,1218 log2_size: u32,
1213 c_idx: usize,1219 c_idx: usize,
1214 scan_idx: usize,1220 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)>,
1215 ) -> usize {1259 ) -> usize {
1216- let mut sig_ctx;1260+ let sig_ctx;
1217 if log2_size == 2 {1261 if log2_size == 2 {
1218 sig_ctx = CTX_IDX_MAP[(yc << 2) + xc];1262 sig_ctx = CTX_IDX_MAP[(yc << 2) + xc];
1219 } else if xc + yc == 0 {1263 } else if xc + yc == 0 {
1220 sig_ctx = 0;1264 sig_ctx = 0;
1221 } else {1265 } else {
1222- let mut prev_csbf = 0usize;1266+ let (prev_csbf, base) = base.expect("sig_ctx_base returns Some when log2_size != 2");
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- }
1229 let (xp, yp) = (xc & 3, yc & 3);1267 let (xp, yp) = (xc & 3, yc & 3);
1230 sig_ctx = match prev_csbf {1268 sig_ctx = match prev_csbf {
1231 0 => {1269 0 => {
@@ -1257,20 +1295,7 @@ fn sig_ctx_inc(
1257 }1295 }
1258 _ => 2,1296 _ => 2,
1259 };1297 };
1260- if c_idx == 0 {1298+ return sig_ctx + base;
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- }
1274 }1299 }
1275 if c_idx == 0 { sig_ctx } else { 27 + sig_ctx }1300 if c_idx == 0 { sig_ctx } else { 27 + sig_ctx }
1276 }1301 }