forge
Explore
Log in

Don't you dare go Hollow

The intra trial path stops allocating and over-zeroing per mode

Browse files at this commit

09d07394fd00bed0e176e08bb28c75e41d99813eAhmet Kaan GümüşParent: 8e31689
  • perf
    • the intra trial path stops allocating and over-zeroing per mode
Show full message
perf(av1): the intra trial path stops allocating and over-zeroing per mode

The per-block edges were a Vec each -- a malloc and a free per mode of
every block -- and intra_predict_u8 zeroed a 32x32 scratch to predict a
4x4. Edges now land in the caller's stack buffers (an edge is at most
2 * BLOCK samples) and the prediction scratch is sized to the block.

-0.47% instructions:u at 1920x1080 (61.741e9 -> 61.447e9, ABAB x2,
enc_probe 4 frames q120). Byte pins green and the FNV sets over the
three gate clips at q150/120/90/60 are unchanged.
crates/ec-av1/src/encode.rsmodified+69 -35
@@ -689,7 +689,12 @@ fn intra_predict_u8(
689689 // a block is at most `BLOCK * BLOCK`. Same samples either way.
690690 let mut above_buf = [0u16; 2 * BLOCK];
691691 let mut left_buf = [0u16; 2 * BLOCK];
692- let mut dst_buf = [0u16; BLOCK * BLOCK];
692+ // lane-av1speed2: a `BLOCK * BLOCK` scratch is 2 KB zeroed on every mode
693+ // trial of every block, and a 4x4 block writes 32 bytes of it. The
694+ // predictor fills every sample it is handed, so the zeros are dead
695+ // either way -- size the scratch to the block instead of to the largest
696+ // block there is.
697+
693698 let widen = |src: &[u8], buf: &mut [u16; 2 * BLOCK]| {
694699 for (d, &v) in buf[..src.len()].iter_mut().zip(src) {
695700 *d = u16::from(v);
@@ -703,21 +708,30 @@ fn intra_predict_u8(
703708 }
704709 let above16 = above.map(|s| &above_buf[..s.len()]);
705710 let left16 = left.map(|s| &left_buf[..s.len()]);
706- let dst16 = &mut dst_buf[..dst.len()];
707- crate::intra::predict(
708- mode,
709- angle_delta,
710- above16,
711- left16,
712- corner.map(u16::from),
713- bw,
714- bh,
715- enable_edge_filter,
716- smooth_neighbor,
717- dst16, fctx,
718- );
719- for (d, &s) in dst.iter_mut().zip(dst16.iter()) {
720- *d = s as u8;
711+ let mut run = |dst16: &mut [u16]| {
712+ crate::intra::predict(
713+ mode,
714+ angle_delta,
715+ above16,
716+ left16,
717+ corner.map(u16::from),
718+ bw,
719+ bh,
720+ enable_edge_filter,
721+ smooth_neighbor,
722+ dst16, fctx,
723+ );
724+ for (d, &s) in dst.iter_mut().zip(dst16.iter()) {
725+ *d = s as u8;
726+ }
727+ };
728+ let n = bw * bh;
729+ if n <= 64 {
730+ run(&mut [0u16; 64][..n]);
731+ } else if n <= 256 {
732+ run(&mut [0u16; 256][..n]);
733+ } else {
734+ run(&mut [0u16; BLOCK * BLOCK][..n]);
721735 }
722736 }
723737
@@ -1392,13 +1406,19 @@ impl Plane<'_> {
13921406 /// they are not, or where the frame ends first, the edge is shorter and
13931407 /// the predictor repeats its last sample, exactly as the decoder's clamp
13941408 /// to `aboveLimit` and `leftLimit` does (spec 7.11.2.2).
1395- fn edges(
1409+ /// lane-av1speed2: the two edges used to be a `Vec` each -- a malloc and
1410+ /// a free per mode per block, on the search's hottest path -- while an
1411+ /// edge is at most `2 * side` samples, which is `2 * BLOCK`. Same
1412+ /// samples, on the caller's stack.
1413+ fn edges_into<'a>(
13961414 &self,
13971415 x: usize,
13981416 y: usize,
13991417 side: usize,
14001418 reach: Reach,
1401- ) -> (Option<Vec<u8>>, Option<Vec<u8>>, Option<u8>) {
1419+ above_buf: &'a mut [u8; 2 * BLOCK],
1420+ left_buf: &'a mut [u8; 2 * BLOCK],
1421+ ) -> (Option<&'a [u8]>, Option<&'a [u8]>, Option<u8>) {
14021422 // Even a block's *own* edge is clamped to the true frame bound, not
14031423 // just a `reach` extension: libaom's `av1_predict_intra_block`
14041424 // (`av1/common/reconintra.c`) derives `xr`/`yd` -- the distance from
@@ -1436,16 +1456,24 @@ impl Plane<'_> {
14361456 // samples above or left of it at all, same as `y == 0`/`x == 0`.
14371457 let across = across.min(self.tile_x1);
14381458 let down = down.min(self.tile_y1);
1439- let above = (y > self.tile_y0 && across > x)
1440- .then(|| self.reconstruction[(y - 1) * self.width + x..][..across - x].to_vec());
1441- let left = (x > self.tile_x0 && down > y).then(|| {
1442- (y..down)
1443- .map(|row| self.reconstruction[row * self.width + x - 1])
1444- .collect::<Vec<_>>()
1445- });
1459+ let above_n = (y > self.tile_y0 && across > x).then(|| across - x);
1460+ if let Some(n) = above_n {
1461+ above_buf[..n]
1462+ .copy_from_slice(&self.reconstruction[(y - 1) * self.width + x..][..n]);
1463+ }
1464+ let left_n = (x > self.tile_x0 && down > y).then(|| down - y);
1465+ if let Some(n) = left_n {
1466+ for (d, row) in left_buf[..n].iter_mut().zip(y..down) {
1467+ *d = self.reconstruction[row * self.width + x - 1];
1468+ }
1469+ }
14461470 let corner = (x > self.tile_x0 && y > self.tile_y0)
14471471 .then(|| self.reconstruction[(y - 1) * self.width + x - 1]);
1448- (above, left, corner)
1472+ (
1473+ above_n.map(|n| &above_buf[..n]),
1474+ left_n.map(|n| &left_buf[..n]),
1475+ corner,
1476+ )
14491477 }
14501478
14511479 /// Codes one block under one mode without committing it: hands back the
@@ -1507,7 +1535,9 @@ impl Plane<'_> {
15071535 let At {
15081536 x, y, side, reach, ..
15091537 } = at;
1510- let (above, left, corner) = self.edges(x, y, side, reach);
1538+ let (mut above_buf, mut left_buf) = ([0u8; 2 * BLOCK], [0u8; 2 * BLOCK]);
1539+ let (above, left, corner) =
1540+ self.edges_into(x, y, side, reach, &mut above_buf, &mut left_buf);
15111541 // Per-candidate scratch on the search's hottest path, so both buffers
15121542 // live on the stack -- `side` is at most `BLOCK` -- rather than
15131543 // costing a malloc/free pair per trial (`mc_trial` already does this).
@@ -1516,8 +1546,8 @@ impl Plane<'_> {
15161546 intra_predict_u8(
15171547 mode,
15181548 0,
1519- above.as_deref(),
1520- left.as_deref(),
1549+ above,
1550+ left,
15211551 corner,
15221552 side,
15231553 side,
@@ -1920,7 +1950,9 @@ impl Plane<'_> {
19201950 let At {
19211951 x, y, side, reach, ..
19221952 } = at;
1923- let (above, left, corner) = self.edges(x, y, side, reach);
1953+ let (mut above_buf, mut left_buf) = ([0u8; 2 * BLOCK], [0u8; 2 * BLOCK]);
1954+ let (above, left, corner) =
1955+ self.edges_into(x, y, side, reach, &mut above_buf, &mut left_buf);
19241956 modes
19251957 .iter()
19261958 .map(|&mode| {
@@ -1928,8 +1960,8 @@ impl Plane<'_> {
19281960 intra_predict_u8(
19291961 mode,
19301962 0,
1931- above.as_deref(),
1932- left.as_deref(),
1963+ above,
1964+ left,
19331965 corner,
19341966 side,
19351967 side,
@@ -1962,7 +1994,9 @@ impl Plane<'_> {
19621994 let At {
19631995 x, y, side, reach, ..
19641996 } = at;
1965- let (above, left, corner) = self.edges(x, y, side, reach);
1997+ let (mut above_buf, mut left_buf) = ([0u8; 2 * BLOCK], [0u8; 2 * BLOCK]);
1998+ let (above, left, corner) =
1999+ self.edges_into(x, y, side, reach, &mut above_buf, &mut left_buf);
19662000 // Nothing ranks by the SAD pre-pass unless `top_k` prunes by it (or
19672001 // the census reports it), so summing it under the default unpruned
19682002 // search is `side * side` absolute differences per mode spent on a
@@ -1976,8 +2010,8 @@ impl Plane<'_> {
19762010 intra_predict_u8(
19772011 mode,
19782012 0,
1979- above.as_deref(),
1980- left.as_deref(),
2013+ above,
2014+ left,
19812015 corner,
19822016 side,
19832017 side,