forge
Explore
Log in

Don't you dare go Hollow

A plane that codes nothing names the shared zero grid instead of allocating one

Browse files at this commit

833833b59562c12d61a1ae9b8d672a24a6a5a693Ahmet Kaan GümüşParent: cb9db45
  • perf
    • a plane that codes nothing names the shared zero grid instead of allocating one
Show full message
perf(av1): a plane that codes nothing names the shared zero grid instead of allocating one

`decode_inter_block` built its three block-level coefficient-level grids as
fresh `Vec`s (up to 64 KB for a 128x128 luma block) even when the plane coded
nothing at all -- a skipped block, a strip that is not the chroma reference,
luma under a var-tx split whose leaves already wrote their own contexts. The
three bindings become a `Grid` enum that derefs to `[i32]`: `Grid::Zero(n)`
points at the existing `ZERO_RESIDUAL` static, so those blocks allocate and
memset nothing, and the block-tail neighbour writes are byte-identical.

Two more per-block allocations go with it: the mu-chunk chroma collectors
(`u_units`/`v_units`) were allocated and zeroed by every var-tx block although
only a block above 64 px on either side ever writes into them -- they are now
filled in by the first unit that does; and the leaf loop's `vartx_leaves` list
is borrowed instead of cloned per block. The skipped luma/chroma units of the
intra var-tx path take the same zero grid.

Byte-exact: all 12 gate hashes (4 streams x 1/4/16 threads, the 16-thread arm
with 4 filter and 2 recon threads) equal the reference. instructions:u on the
4K segment 405.018 -> 402.630 G at 1 thread (-0.59%) and 408.381 -> 405.961 G
at (16,4,2) (-0.59%).

Measured and reverted: pooling the six compound-prediction intermediates per
thread was flat (-0.04% / +0.05%), and moving the sub-8x8 4x4 prediction
buffers to the stack was flat both ways -- those paths are cold on this stream.
crates/ec-av1/src/decode.rsmodified+139 -75
@@ -2199,6 +2199,37 @@ fn push_pred_samples(v: &[u16], fctx: &crate::decode::FrameCtx) -> u32 {
21992199 /// (a 128-wide inter block's prediction stride).
22002200 static ZERO_RESIDUAL: [i32; 128 * 128] = [0; 128 * 128];
22012201
2202+/// lane-blockscratch: one plane's coefficient levels as the block-tail
2203+/// neighbour writes want them. A plane that coded nothing (a skipped block, a
2204+/// strip with no chroma, luma under a var-tx split whose units already wrote
2205+/// themselves) names the shared [`ZERO_RESIDUAL`] instead of allocating and
2206+/// zeroing up to 64 KB of its own per block.
2207+enum Grid {
2208+ Zero(usize),
2209+ Own(Vec<i32>),
2210+}
2211+
2212+/// lane-blockscratch: the mu-chunk chroma collector, allocated by the first
2213+/// unit that writes into it. Only a block wider or taller than 64 px codes its
2214+/// chroma per 64x64 chunk; every smaller block used to allocate and zero both
2215+/// collectors per block and then drop them untouched.
2216+fn mu_units(v: &mut Vec<i32>, n: usize) -> &mut Vec<i32> {
2217+ if v.is_empty() {
2218+ *v = vec![0i32; n];
2219+ }
2220+ v
2221+}
2222+
2223+impl std::ops::Deref for Grid {
2224+ type Target = [i32];
2225+ fn deref(&self) -> &[i32] {
2226+ match self {
2227+ Grid::Zero(n) => &ZERO_RESIDUAL[..*n],
2228+ Grid::Own(v) => v,
2229+ }
2230+ }
2231+}
2232+
22022233 /// `residual`, or `n` zeros when it is the empty all-zero marker.
22032234 fn dense_residual(residual: &[i32], n: usize) -> &[i32] {
22042235 if residual.is_empty() { &ZERO_RESIDUAL[..n] } else { residual }
@@ -25028,9 +25059,9 @@ fn decode_inter_block(
2502825059 sv,
2502925060 &ZERO_RESIDUAL[..chroma_side * chroma_side], fctx,
2503025061 );
25031- luma_grid = vec![0i32; side * side];
25032- u_grid = vec![0i32; chroma_side * chroma_side];
25033- v_grid = vec![0i32; chroma_side * chroma_side];
25062+ luma_grid = Grid::Zero(side * side);
25063+ u_grid = Grid::Zero(chroma_side * chroma_side);
25064+ v_grid = Grid::Zero(chroma_side * chroma_side);
2503425065 } else {
2503525066 // lane-inter4 r2: a rect strip's coefficient context spans its
2503625067 // OWN width above and height left -- reading `side` cells both
@@ -25044,9 +25075,9 @@ fn decode_inter_block(
2504425075 // lane-sb128c r1: a 128x128 inter block's four chroma units
2504525076 // are read INSIDE the luma leaf loop (one per 64x64 mu chunk);
2504625077 // these collect them for the block-level neighbour write-back.
25047- let mut u_units = vec![0i32; chroma_side * chroma_side];
25048- let mut v_units = vec![0i32; chroma_side * chroma_side];
25049- if let Some(leaves) = vartx_leaves.clone() {
25078+ let mut u_units: Vec<i32> = Vec::new();
25079+ let mut v_units: Vec<i32> = Vec::new();
25080+ if let Some(leaves) = vartx_leaves.as_ref() {
2505025081 // spec 5.11.17's transform tree, leaf by leaf in the order
2505125082 // `read_var_tx_size` coded it: each unit reads its own
2505225083 // coefficients against the neighbour magnitudes the
@@ -25168,11 +25199,10 @@ fn decode_inter_block(
2516825199 );
2516925200 CHROMA_SPLIT_TX_HITS.with(|c| c.set(c.get() + 1));
2517025201 mu_chroma = true;
25171- let dst = if plane_idx == 1 {
25172- &mut u_units
25173- } else {
25174- &mut v_units
25175- };
25202+ let dst = mu_units(
25203+ if plane_idx == 1 { &mut u_units } else { &mut v_units },
25204+ chroma_side * chroma_side,
25205+ );
2517625206 for rr in 0..cu_tx {
2517725207 let start =
2517825208 (cr * cu_tx + rr) * chroma_side + cc * cu_tx;
@@ -25187,7 +25217,7 @@ fn decode_inter_block(
2518725217 // position back to luma, so an inter block's chroma
2518825218 // inherits the *first* (top-left) luma unit's coded type.
2518925219 luma_tx_type = first_tx_type;
25190- luma_grid = vec![0i32; side * side];
25220+ luma_grid = Grid::Zero(side * side);
2519125221 } else if rect_tu {
2519225222 // The whole-block rectangular transform unit
2519325223 // (`txfm_partition` said 0 at depth 0).
@@ -25208,9 +25238,10 @@ fn decode_inter_block(
2520825238 None, fctx,
2520925239 )?;
2521025240 luma_tx_type = tx_type;
25211- luma_grid = embed_rect_grid(&grid, write_w, write_h, side);
25241+ luma_grid = Grid::Own(embed_rect_grid(&grid, write_w, write_h, side));
2521225242 } else {
25213- (luma_grid, luma_tx_type) = read_inter_plane(
25243+ {
25244+ let coded = read_inter_plane(
2521425245 dec,
2521525246 cdfs,
2521625247 luma_set_inter,
@@ -25227,20 +25258,31 @@ fn decode_inter_block(
2522725258 None,
2522825259 None, fctx,
2522925260 )?;
25261+ luma_grid = Grid::Own(coded.0);
25262+ luma_tx_type = coded.1;
25263+ }
2523025264 }
2523125265 if side > 64 {
2523225266 // Already read inside the mu-chunk loop above -- at the 128
2523325267 // root a rect (128x64/64x128) block's chroma is per mu
2523425268 // chunk too, so this test comes BEFORE the `rect_tu` one.
25235- u_grid = std::mem::take(&mut u_units);
25236- v_grid = std::mem::take(&mut v_units);
25269+ u_grid = if u_units.is_empty() {
25270+ Grid::Zero(chroma_side * chroma_side)
25271+ } else {
25272+ Grid::Own(std::mem::take(&mut u_units))
25273+ };
25274+ v_grid = if v_units.is_empty() {
25275+ Grid::Zero(chroma_side * chroma_side)
25276+ } else {
25277+ Grid::Own(std::mem::take(&mut v_units))
25278+ };
2523725279 } else if rect_tu {
2523825280 // `av1_get_max_uv_txsize(bsize)`: the chroma transform of a
2523925281 // 2:1 strip is the strip's own rect chroma size (16x8 /
2524025282 // 8x16), one unit for the whole block, and it codes no
2524125283 // `tx_type` symbol -- it inherits the luma unit's.
2524225284 let (uw, uh) = (write_chroma_w, write_chroma_h);
25243- u_grid = embed_rect_grid(
25285+ u_grid = Grid::Own(embed_rect_grid(
2524425286 &read_inter_plane_rect(
2524525287 dec,
2524625288 cdfs,
@@ -25261,8 +25303,8 @@ fn decode_inter_block(
2526125303 uw,
2526225304 uh,
2526325305 chroma_side,
25264- );
25265- v_grid = embed_rect_grid(
25306+ ));
25307+ v_grid = Grid::Own(embed_rect_grid(
2526625308 &read_inter_plane_rect(
2526725309 dec,
2526825310 cdfs,
@@ -25283,9 +25325,9 @@ fn decode_inter_block(
2528325325 uw,
2528425326 uh,
2528525327 chroma_side,
25286- );
25328+ ));
2528725329 } else {
25288- u_grid = read_inter_plane(
25330+ u_grid = Grid::Own(read_inter_plane(
2528925331 dec,
2529025332 cdfs,
2529125333 chroma_set,
@@ -25302,8 +25344,8 @@ fn decode_inter_block(
2530225344 Some(luma_tx_type),
2530325345 None, fctx,
2530425346 )?
25305- .0;
25306- v_grid = read_inter_plane(
25347+ .0);
25348+ v_grid = Grid::Own(read_inter_plane(
2530725349 dec,
2530825350 cdfs,
2530925351 chroma_set,
@@ -25320,7 +25362,7 @@ fn decode_inter_block(
2532025362 Some(luma_tx_type),
2532125363 None, fctx,
2532225364 )?
25323- .0;
25365+ .0);
2532425366 }
2532525367 }
2532625368 } else {
@@ -26279,9 +26321,9 @@ fn decode_inter_block(
2627926321 &ZERO_RESIDUAL[..chroma_side * chroma_side], fctx,
2628026322 );
2628126323 }
26282- luma_grid = vec![0i32; side * side];
26283- u_grid = vec![0i32; chroma_side * chroma_side];
26284- v_grid = vec![0i32; chroma_side * chroma_side];
26324+ luma_grid = Grid::Zero(side * side);
26325+ u_grid = Grid::Zero(chroma_side * chroma_side);
26326+ v_grid = Grid::Zero(chroma_side * chroma_side);
2628526327 } else {
2628626328 // lane-inter4 r2: a rect strip's coefficient context spans its
2628726329 // OWN width above and height left -- reading `side` cells both
@@ -26307,9 +26349,9 @@ fn decode_inter_block(
2630726349 // lane-sb128c r1: a 128x128 inter block's four chroma units
2630826350 // are read INSIDE the luma leaf loop (one per 64x64 mu chunk);
2630926351 // these collect them for the block-level neighbour write-back.
26310- let mut u_units = vec![0i32; chroma_side * chroma_side];
26311- let mut v_units = vec![0i32; chroma_side * chroma_side];
26312- if let Some(leaves) = vartx_leaves.clone() {
26352+ let mut u_units: Vec<i32> = Vec::new();
26353+ let mut v_units: Vec<i32> = Vec::new();
26354+ if let Some(leaves) = vartx_leaves.as_ref() {
2631326355 // spec 5.11.17's transform tree, leaf by leaf in the order
2631426356 // `read_var_tx_size` coded it: each unit reads its own
2631526357 // coefficients against the neighbour magnitudes the
@@ -26431,11 +26473,10 @@ fn decode_inter_block(
2643126473 );
2643226474 CHROMA_SPLIT_TX_HITS.with(|c| c.set(c.get() + 1));
2643326475 mu_chroma = true;
26434- let dst = if plane_idx == 1 {
26435- &mut u_units
26436- } else {
26437- &mut v_units
26438- };
26476+ let dst = mu_units(
26477+ if plane_idx == 1 { &mut u_units } else { &mut v_units },
26478+ chroma_side * chroma_side,
26479+ );
2643926480 for rr in 0..cu_tx {
2644026481 let start =
2644126482 (cr * cu_tx + rr) * chroma_side + cc * cu_tx;
@@ -26450,7 +26491,7 @@ fn decode_inter_block(
2645026491 // position back to luma, so an inter block's chroma
2645126492 // inherits the *first* (top-left) luma unit's coded type.
2645226493 luma_tx_type = first_tx_type;
26453- luma_grid = vec![0i32; side * side];
26494+ luma_grid = Grid::Zero(side * side);
2645426495 } else if rect_tu {
2645526496 // The whole-block rectangular transform unit
2645626497 // (`txfm_partition` said 0 at depth 0).
@@ -26471,9 +26512,10 @@ fn decode_inter_block(
2647126512 None, fctx,
2647226513 )?;
2647326514 luma_tx_type = tx_type;
26474- luma_grid = embed_rect_grid(&grid, write_w, write_h, side);
26515+ luma_grid = Grid::Own(embed_rect_grid(&grid, write_w, write_h, side));
2647526516 } else {
26476- (luma_grid, luma_tx_type) = read_inter_plane(
26517+ {
26518+ let coded = read_inter_plane(
2647726519 dec,
2647826520 cdfs,
2647926521 luma_set_inter,
@@ -26490,6 +26532,9 @@ fn decode_inter_block(
2649026532 None,
2649126533 None, fctx,
2649226534 )?;
26535+ luma_grid = Grid::Own(coded.0);
26536+ luma_tx_type = coded.1;
26537+ }
2649326538 }
2649426539 if !has_chroma {
2649526540 // `is_chroma_reference` is false for this strip: libaom's
@@ -26498,21 +26543,29 @@ fn decode_inter_block(
2649826543 // `reconstruct_inter_block`'s `for (plane...)` guarded by
2649926544 // `xd->is_chroma_ref`) codes NO chroma transform here, so
2650026545 // neither symbol nor context exists.
26501- u_grid = vec![0i32; chroma_side * chroma_side];
26502- v_grid = vec![0i32; chroma_side * chroma_side];
26546+ u_grid = Grid::Zero(chroma_side * chroma_side);
26547+ v_grid = Grid::Zero(chroma_side * chroma_side);
2650326548 } else if side > 64 {
2650426549 // Already read inside the mu-chunk loop above -- at the 128
2650526550 // root a rect (128x64/64x128) block's chroma is per mu
2650626551 // chunk too, so this test comes BEFORE the `rect_tu` one.
26507- u_grid = std::mem::take(&mut u_units);
26508- v_grid = std::mem::take(&mut v_units);
26552+ u_grid = if u_units.is_empty() {
26553+ Grid::Zero(chroma_side * chroma_side)
26554+ } else {
26555+ Grid::Own(std::mem::take(&mut u_units))
26556+ };
26557+ v_grid = if v_units.is_empty() {
26558+ Grid::Zero(chroma_side * chroma_side)
26559+ } else {
26560+ Grid::Own(std::mem::take(&mut v_units))
26561+ };
2650926562 } else if rect_tu {
2651026563 // `av1_get_max_uv_txsize(bsize)`: the chroma transform of a
2651126564 // 2:1 strip is the strip's own rect chroma size (16x8 /
2651226565 // 8x16), one unit for the whole block, and it codes no
2651326566 // `tx_type` symbol -- it inherits the luma unit's.
2651426567 let (uw, uh) = (write_chroma_w, write_chroma_h);
26515- u_grid = embed_rect_grid(
26568+ u_grid = Grid::Own(embed_rect_grid(
2651626569 &read_inter_plane_rect(
2651726570 dec,
2651826571 cdfs,
@@ -26533,8 +26586,8 @@ fn decode_inter_block(
2653326586 uw,
2653426587 uh,
2653526588 chroma_side,
26536- );
26537- v_grid = embed_rect_grid(
26589+ ));
26590+ v_grid = Grid::Own(embed_rect_grid(
2653826591 &read_inter_plane_rect(
2653926592 dec,
2654026593 cdfs,
@@ -26555,9 +26608,9 @@ fn decode_inter_block(
2655526608 uw,
2655626609 uh,
2655726610 chroma_side,
26558- );
26611+ ));
2655926612 } else {
26560- u_grid = read_inter_plane(
26613+ u_grid = Grid::Own(read_inter_plane(
2656126614 dec,
2656226615 cdfs,
2656326616 chroma_set,
@@ -26574,8 +26627,8 @@ fn decode_inter_block(
2657426627 Some(luma_tx_type),
2657526628 None, fctx,
2657626629 )?
26577- .0;
26578- v_grid = read_inter_plane(
26630+ .0);
26631+ v_grid = Grid::Own(read_inter_plane(
2657926632 dec,
2658026633 cdfs,
2658126634 chroma_set,
@@ -26592,7 +26645,7 @@ fn decode_inter_block(
2659226645 Some(luma_tx_type),
2659326646 None, fctx,
2659426647 )?
26595- .0;
26648+ .0);
2659626649 }
2659726650 }
2659826651 }
@@ -26974,9 +27027,9 @@ fn decode_inter_block(
2697427027 // `decode_token_recon_block` walks `mu_blocks_wide/high` and codes
2697527028 // every plane of a chunk before the next chunk's luma. These collect
2697627029 // the four TX_32X32 units per plane for the block-level write-back.
26977- let mut u_units = vec![0i32; chroma_side * chroma_side];
26978- let mut v_units = vec![0i32; chroma_side * chroma_side];
26979- if let Some(leaves) = vartx_leaves.clone() {
27030+ let mut u_units: Vec<i32> = Vec::new();
27031+ let mut v_units: Vec<i32> = Vec::new();
27032+ if let Some(leaves) = vartx_leaves.as_ref() {
2698027033 let reduced = fctx.reduced_tx_set_inter.with(std::cell::Cell::get);
2698127034 for (idx, &(row, col, tx_px, _)) in leaves.iter().enumerate() {
2698227035 let tu_mi = (at_mi.0 + row, at_mi.1 + col);
@@ -27012,13 +27065,13 @@ fn decode_inter_block(
2701227065 filter_intra,
2701327066 smooth_neighbor, fctx,
2701427067 );
27015- vec![0i32; tx_px * tx_px]
27068+ Grid::Zero(tx_px * tx_px)
2701627069 } else {
2701727070 // The unit's bsize is smaller than the block it sits in,
2701827071 // so `txb_skip_ctx` is the neighbour-magnitude table, not
2701927072 // the lone-TU 0 (`get_txb_ctx_general`).
2702027073 let tu_skip_ctx = neighbours.luma_skip_ctx(tu_mi, tx_px / MI);
27021- read_plane(
27074+ Grid::Own(read_plane(
2702227075 dec,
2702327076 cdfs,
2702427077 txbset_for(tx_px, reduced),
@@ -27043,7 +27096,7 @@ fn decode_inter_block(
2704327096 filter_intra,
2704427097 Some(tu_skip_ctx),
2704527098 smooth_neighbor, fctx,
27046- )?
27099+ )?)
2704727100 };
2704827101 neighbours.record_mi_luma(tu_mi, tx_px, &tu_grid);
2704927102 if side > 64 {
@@ -27082,9 +27135,9 @@ fn decode_inter_block(
2708227135 None,
2708327136 smooth_neighbor_uv, fctx,
2708427137 );
27085- vec![0i32; cu_tx * cu_tx]
27138+ Grid::Zero(cu_tx * cu_tx)
2708627139 } else {
27087- read_plane(
27140+ Grid::Own(read_plane(
2708827141 dec,
2708927142 cdfs,
2709027143 chroma_set,
@@ -27111,7 +27164,7 @@ fn decode_inter_block(
2711127164 // (lane-sb128b r3).
2711227165 Some(3),
2711327166 smooth_neighbor_uv, fctx,
27114- )?
27167+ )?)
2711527168 };
2711627169 // Immediately, so the NEXT chunk's unit reads this
2711727170 // one's coefficient context.
@@ -27119,7 +27172,10 @@ fn decode_inter_block(
2711927172 cu_mi, luma_span, luma_span, plane_idx, &cu_grid,
2712027173 );
2712127174 CHROMA_SPLIT_TX_HITS.with(|c| c.set(c.get() + 1));
27122- let dst = if plane_idx == 1 { &mut u_units } else { &mut v_units };
27175+ let dst = mu_units(
27176+ if plane_idx == 1 { &mut u_units } else { &mut v_units },
27177+ chroma_side * chroma_side,
27178+ );
2712327179 for rr in 0..cu_tx {
2712427180 let start = (cr * cu_tx + rr) * chroma_side + cc * cu_tx;
2712527181 dst[start..start + cu_tx]
@@ -27131,9 +27187,17 @@ fn decode_inter_block(
2713127187 }
2713227188 if side > 64 {
2713327189 // Chroma is already coded and reconstructed per mu chunk above.
27134- luma_grid = vec![0i32; side * side];
27135- u_grid = std::mem::take(&mut u_units);
27136- v_grid = std::mem::take(&mut v_units);
27190+ luma_grid = Grid::Zero(side * side);
27191+ u_grid = if u_units.is_empty() {
27192+ Grid::Zero(chroma_side * chroma_side)
27193+ } else {
27194+ Grid::Own(std::mem::take(&mut u_units))
27195+ };
27196+ v_grid = if v_units.is_empty() {
27197+ Grid::Zero(chroma_side * chroma_side)
27198+ } else {
27199+ Grid::Own(std::mem::take(&mut v_units))
27200+ };
2713727201 INTRA_128_IN_INTER_HITS.with(|c| c.set(c.get() + 1));
2713827202 } else if skip {
2713927203 if !split_luma {
@@ -27184,21 +27248,21 @@ fn decode_inter_block(
2718427248 None,
2718527249 smooth_neighbor_uv, fctx,
2718627250 );
27187- luma_grid = vec![0i32; side * side];
27188- u_grid = vec![0i32; chroma_side * chroma_side];
27189- v_grid = vec![0i32; chroma_side * chroma_side];
27251+ luma_grid = Grid::Zero(side * side);
27252+ u_grid = Grid::Zero(chroma_side * chroma_side);
27253+ v_grid = Grid::Zero(chroma_side * chroma_side);
2719027254 } else {
2719127255 let around = neighbours.around_mi(at, side);
27192- luma_grid = if split_luma {
27256+ if split_luma {
2719327257 // Already coded and reconstructed unit by unit above; the
2719427258 // block-level grid is only the neighbour write-back's input,
2719527259 // which `record_split_luma` ignores for plane 0.
27196- vec![0i32; side * side]
27260+ luma_grid = Grid::Zero(side * side);
2719727261 } else {
2719827262 if let Some(buf) = &palette_y_buf {
2719927263 set_palette_pred(buf.clone(), fctx);
2720027264 }
27201- read_plane(
27265+ luma_grid = Grid::Own(read_plane(
2720227266 dec,
2720327267 cdfs,
2720427268 luma_set_intra,
@@ -27219,13 +27283,13 @@ fn decode_inter_block(
2721927283 filter_intra,
2722027284 None,
2722127285 smooth_neighbor, fctx,
27222- )?
27223- };
27286+ )?);
27287+ }
2722427288 let ac = alpha.map(|_| cfl_src(px, py, side));
2722527289 if let Some((ub, _)) = &palette_uv_bufs {
2722627290 set_palette_pred(ub.clone(), fctx);
2722727291 }
27228- u_grid = read_plane(
27292+ u_grid = Grid::Own(read_plane(
2722927293 dec,
2723027294 cdfs,
2723127295 chroma_set,
@@ -27246,11 +27310,11 @@ fn decode_inter_block(
2724627310 None,
2724727311 None,
2724827312 smooth_neighbor_uv, fctx,
27249- )?;
27313+ )?);
2725027314 if let Some((_, vb)) = &palette_uv_bufs {
2725127315 set_palette_pred(vb.clone(), fctx);
2725227316 }
27253- v_grid = read_plane(
27317+ v_grid = Grid::Own(read_plane(
2725427318 dec,
2725527319 cdfs,
2725627320 chroma_set,
@@ -27271,7 +27335,7 @@ fn decode_inter_block(
2727127335 None,
2727227336 None,
2727327337 smooth_neighbor_uv, fctx,
27274- )?;
27338+ )?);
2727527339 }
2727627340 }
2727727341 // lane-comppin r9: end-of-block checkpoint -- everything between