forge
Explore
Log in

Don't you dare go Hollow

One packed cell for the four skip/pu-edge mi maps, one pass for around_mi

Browse files at this commit

dcb88a28afecf7237b31395b0c10f08aa1082d5bAhmet Kaan GümüşParent: cb9db45
  • perf
    • one packed cell for the four skip/pu-edge mi maps, one pass for around_mi
Show full message
perf(av1): one packed cell for the four skip/pu-edge mi maps, one pass for around_mi

Block publication walked the same mi rectangle once per map. `skip`, its
written-instrument bit, and the covering block's origin and dims are written
by `fill_skip_grid_rect` and by nothing else, so they become one 8-byte
`BlkCell` and one fill per row instead of four -- the deblocker's `pu_edge`
also reads origin and dims off one cache line now.

`around_mi`/`around_mi_rect` gathered the neighbour coded/DC-sign state one
plane at a time, so the three planes of a cell (one `[Neighbour; 3]`) were
loaded three times; one pass over the cells accumulates all three.

Byte-exact: 12/12 gate hashes at 1, 4 and 16 threads. seg4k instructions:u
405.019 -> 402.414 G at 1 thread (-0.64%) and 408.299 -> 405.731 G at
(16,4,2) (-0.63%); task-clock at (16,4,2) 41.08 -> 40.78/41.03 s interleaved.
crates/ec-av1/src/decode.rsmodified+75 -55
@@ -6283,6 +6283,19 @@ const SKIP_CONTEXTS: [[usize; 5]; 5] = [
62836283 [3, 5, 5, 5, 6],
62846284 ];
62856285
6286+/// The `skip` flag, its written-instrument bit, and the covering coded
6287+/// block's origin and dims, packed into ONE 8-byte cell (lane-publish): the
6288+/// four maps are written together by `Neighbours::fill_skip_grid_rect` and by
6289+/// nothing else, so a block's row is one fill instead of four, and the
6290+/// deblocker's `pu_edge` reads origin and dims off one cache line.
6291+#[derive(Clone, Copy)]
6292+struct BlkCell {
6293+ skip: bool,
6294+ written: bool,
6295+ org: [u16; 2],
6296+ dim: [u8; 2],
6297+}
6298+
62866299 /// The neighbour state a block's contexts are read from — a duplicate of
62876300 /// [`crate::tile`]'s own private `Neighbours` (that module is another lane's
62886301 /// territory this round), kept at the same two granularities the real writer
@@ -6418,27 +6431,25 @@ struct Neighbours {
64186431 /// grain [`Self::record_mi`] already writes at), read back at 8x8
64196432 /// granularity by [`apply_cdef`] since `skip` is constant across every
64206433 /// mi cell one coded block covers.
6421- skip_grid: Vec<bool>,
6434+ /// The four per-mi maps [`Neighbours::blk_grid`] packs, in one cell.
64226435 /// lane-cdefstrip r1 instrument: whether any coded block actually WROTE
6423- /// [`Self::skip_grid`] at this mi cell during this frame. A fresh
6436+ /// the `skip` bit at this mi cell during this frame. A fresh
64246437 /// `Neighbours` seeds `skip_grid` all-`false` ("not skip"), so a decode
64256438 /// arm that forgets [`Self::fill_skip_grid_rect`] silently makes
64266439 /// [`apply_cdef`] filter an 8x8 unit libaom's `is_8x8_block_skip`
64276440 /// (`cdef.c:29-38`) excludes -- exactly the lane-cdef defect, and
64286441 /// unobservable from the pixels alone. Counted by
64296442 /// [`cdef_unwritten_skip_units`].
6430- skip_written: Vec<bool>,
64316443 /// Per-4x4-mi origin `(mi_row, mi_col)` of the coded (prediction) block
64326444 /// covering that cell -- spec 7.14.2's `isBlockEdge`/libaom `pu_edge`,
64336445 /// which re-enables the loop filter at a prediction boundary between two
64346446 /// skipped inter blocks (lane-mergefix r4). Written by
64356447 /// [`Self::fill_skip_grid_rect`], whose `at_mi` IS that origin.
6436- blk_org_grid: Vec<[u16; 2]>,
64376448 /// Per-4x4-mi `(width, height)` in mi units of the coded block covering
64386449 /// that cell -- the other half of `pu_edge`: libaom tests the edge
64396450 /// coordinate against the block's size in the FILTERED plane's own
64406451 /// pixels, which for chroma is `max(luma_dim / 2, 4)` (lane-t900 r30b).
6441- blk_dim_grid: Vec<[u8; 2]>,
6452+ blk_grid: Vec<BlkCell>,
64426453 skip_grid_cols_mi: usize,
64436454 /// Per-4x4-mi luma transform width in pixels (8/16/32/64) -- this decoder
64446455 /// never splits a coded block's transform below its own size (round 2:
@@ -6560,10 +6571,10 @@ impl Neighbours {
65606571 left_filter: vec![[3u8; 2]; rows * (SUB / MI)],
65616572 mi_cols,
65626573 mi_rows,
6563- skip_grid: vec![false; cols * (SUB / MI) * rows * (SUB / MI)],
6564- skip_written: vec![false; cols * (SUB / MI) * rows * (SUB / MI)],
6565- blk_org_grid: vec![[0u16; 2]; cols * (SUB / MI) * rows * (SUB / MI)],
6566- blk_dim_grid: vec![[1u8; 2]; cols * (SUB / MI) * rows * (SUB / MI)],
6574+ blk_grid: vec![
6575+ BlkCell { skip: false, written: false, org: [0; 2], dim: [1; 2] };
6576+ cols * (SUB / MI) * rows * (SUB / MI)
6577+ ],
65676578 skip_grid_cols_mi: cols * (SUB / MI),
65686579 tx_grid: vec![0u8; cols * (SUB / MI) * rows * (SUB / MI)],
65696580 tx_h_grid: vec![0u8; cols * (SUB / MI) * rows * (SUB / MI)],
@@ -6971,10 +6982,17 @@ impl Neighbours {
69716982 }
69726983 for rr in 0..h_mi {
69736984 let start = (mi_r + rr) * self.skip_grid_cols_mi + mi_c;
6974- fill_span(&mut self.skip_grid, start, w_mi, skip);
6975- fill_span(&mut self.skip_written, start, w_mi, true);
6976- fill_span(&mut self.blk_org_grid, start, w_mi, [mi_r as u16, mi_c as u16]);
6977- fill_span(&mut self.blk_dim_grid, start, w_mi, [w_mi as u8, h_mi as u8]);
6985+ fill_span(
6986+ &mut self.blk_grid,
6987+ start,
6988+ w_mi,
6989+ BlkCell {
6990+ skip,
6991+ written: true,
6992+ org: [mi_r as u16, mi_c as u16],
6993+ dim: [w_mi as u8, h_mi as u8],
6994+ },
6995+ );
69786996 }
69796997 }
69806998
@@ -6988,10 +7006,9 @@ impl Neighbours {
69887006 /// filter's `curr_skipped`/`pv_skip_txfm` (spec 7.14.2) want, since a
69897007 /// filtered edge's two sides are each exactly one coded block wide.
69907008 fn skip_at(&self, mi_r: usize, mi_c: usize) -> bool {
6991- self.skip_grid
7009+ self.blk_grid
69927010 .get(mi_r * self.skip_grid_cols_mi + mi_c)
6993- .copied()
6994- .unwrap_or(false)
7011+ .is_some_and(|c| c.skip)
69957012 }
69967013
69977014 /// The `skip_txfm` context of a block whose top-left mi cell is
@@ -7007,10 +7024,9 @@ impl Neighbours {
70077024 fn is_skip_txfm(&self, mi_r: usize, mi_c: usize) -> bool {
70087025 (0..2).all(|rr| {
70097026 (0..2).all(|cc| {
7010- self.skip_grid
7027+ self.blk_grid
70117028 .get((mi_r + rr) * self.skip_grid_cols_mi + (mi_c + cc))
7012- .copied()
7013- .unwrap_or(false)
7029+ .is_some_and(|c| c.skip)
70147030 })
70157031 })
70167032 }
@@ -7262,19 +7278,21 @@ impl Neighbours {
72627278 /// units, for the same reason [`Self::record_mi`] does.
72637279 fn around_mi(&self, (mi_r, mi_c): (usize, usize), side: usize) -> [(bool, bool, i32); 3] {
72647280 let side_mi = side / MI;
7265- std::array::from_fn(|plane| {
7266- let mut above_coded = false;
7267- let mut left_coded = false;
7268- let mut vote = 0;
7269- for cell in 0..side_mi {
7270- let above = &self.above[mi_c + cell][plane];
7271- let left = &self.left[mi_r + cell][plane];
7272- above_coded |= above.level != 0;
7273- left_coded |= left.level != 0;
7274- vote += dc_vote(above.dc) + dc_vote(left.dc);
7275- }
7276- (above_coded, left_coded, vote)
7277- })
7281+ // One pass over the cells accumulating all three planes, not one pass
7282+ // per plane: the three planes of a cell live in the same
7283+ // `[Neighbour; 3]` (6 bytes), so the per-plane form loaded the same
7284+ // cache lines three times (lane-publish).
7285+ let mut out = [(false, false, 0i32); 3];
7286+ for cell in 0..side_mi {
7287+ let above = &self.above[mi_c + cell];
7288+ let left = &self.left[mi_r + cell];
7289+ for (plane, o) in out.iter_mut().enumerate() {
7290+ o.0 |= above[plane].level != 0;
7291+ o.1 |= left[plane].level != 0;
7292+ o.2 += dc_vote(above[plane].dc) + dc_vote(left[plane].dc);
7293+ }
7294+ }
7295+ out
72787296 }
72797297
72807298 /// [`Self::around`] with independent above (`w`)/left (`h`) extents, in
@@ -7288,21 +7306,25 @@ impl Neighbours {
72887306 /// mode-info units, mirroring [`Self::around_mi`].
72897307 fn around_mi_rect(&self, (mi_r, mi_c): (usize, usize), w: usize, h: usize) -> [(bool, bool, i32); 3] {
72907308 let (w_mi, h_mi) = (w / MI, h / MI);
7291- std::array::from_fn(|plane| {
7292- let mut above_coded = false;
7293- let mut left_coded = false;
7294- let mut vote = 0;
7295- for cell in 0..w_mi {
7296- let above = &self.above[mi_c + cell][plane];
7297- above_coded |= above.level != 0;
7298- vote += dc_vote(above.dc);
7309+ // [`Self::around_mi`]'s single pass, with the two extents split.
7310+ let mut out = [(false, false, 0i32); 3];
7311+ for cell in 0..w_mi {
7312+ let above = &self.above[mi_c + cell];
7313+ for (plane, o) in out.iter_mut().enumerate() {
7314+ o.0 |= above[plane].level != 0;
7315+ o.2 += dc_vote(above[plane].dc);
72997316 }
7300- for cell in 0..h_mi {
7301- let left = &self.left[mi_r + cell][plane];
7302- left_coded |= left.level != 0;
7303- vote += dc_vote(left.dc);
7317+ }
7318+ for cell in 0..h_mi {
7319+ let left = &self.left[mi_r + cell];
7320+ for (plane, o) in out.iter_mut().enumerate() {
7321+ o.1 |= left[plane].level != 0;
7322+ o.2 += dc_vote(left[plane].dc);
73047323 }
7305- if crate::envflags::env_flag!("EC_DCDUMP") {
7324+ }
7325+ if crate::envflags::env_flag!("EC_DCDUMP") {
7326+ for (plane, o) in out.iter().enumerate() {
7327+ let vote = o.2;
73067328 let ab: Vec<String> = (0..w_mi)
73077329 .map(|k| format!("{:?}/{}", self.above[mi_c + k][plane].dc, self.above[mi_c + k][plane].level))
73087330 .collect();
@@ -7311,8 +7333,8 @@ impl Neighbours {
73117333 .collect();
73127334 eprintln!("EC_DCDUMP mi=({mi_r},{mi_c}) plane={plane} wh=({w},{h}) vote={vote} above=[{}] left=[{}]", ab.join(","), lf.join(","));
73137335 }
7314- (above_coded, left_coded, vote)
7315- })
7336+ }
7337+ out
73167338 }
73177339
73187340 /// The luma `txb_skip_ctx` of a transform unit smaller than its own block
@@ -18025,8 +18047,8 @@ fn edge_params_body(
1802518047 // bottom/right mi does not start there. Comparing mi origins instead is
1802618048 // the same test only while the block's own origin is aligned to its
1802718049 // size, which a 4x4 luma block read at the `| 1` mi is not.
18028- let cur_org = n.blk_org_grid[mi_r * n.skip_grid_cols_mi + mi_c];
18029- let cur_dim = n.blk_dim_grid[mi_r * n.skip_grid_cols_mi + mi_c];
18050+ let cur_cell = n.blk_grid[mi_r * n.skip_grid_cols_mi + mi_c];
18051+ let (cur_org, cur_dim) = (cur_cell.org, cur_cell.dim);
1803018052 let (org_px, dim_mi) = if c.dir == 0 {
1803118053 (usize::from(cur_org[1]), usize::from(cur_dim[0]))
1803218054 } else {
@@ -19345,10 +19367,9 @@ fn apply_cdef(
1934519367 if (0..2).any(|rr| {
1934619368 (0..2).any(|cc| {
1934719369 !skip_grid
19348- .skip_written
19370+ .blk_grid
1934919371 .get((r + rr) * skip_grid.skip_grid_cols_mi + (c + cc))
19350- .copied()
19351- .unwrap_or(true)
19372+ .map_or(true, |x| x.written)
1935219373 })
1935319374 }) {
1935419375 CDEF_UNWRITTEN_SKIP_UNITS.with(|c2| c2.set(c2.get() + 1));
@@ -19358,10 +19379,9 @@ fn apply_cdef(
1935819379 }
1935919380 let cell = |rr: usize, cc: usize| {
1936019381 skip_grid
19361- .skip_grid
19382+ .blk_grid
1936219383 .get((r + rr) * skip_grid.skip_grid_cols_mi + (c + cc))
19363- .copied()
19364- .unwrap_or(false)
19384+ .is_some_and(|x| x.skip)
1936519385 };
1936619386 let first = cell(0, 0);
1936719387 if (0..2).any(|rr| (0..2).any(|cc| cell(rr, cc) != first)) {