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(
689 // a block is at most `BLOCK * BLOCK`. Same samples either way.689 // a block is at most `BLOCK * BLOCK`. Same samples either way.
690 let mut above_buf = [0u16; 2 * BLOCK];690 let mut above_buf = [0u16; 2 * BLOCK];
691 let mut left_buf = [0u16; 2 * BLOCK];691 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+
693 let widen = |src: &[u8], buf: &mut [u16; 2 * BLOCK]| {698 let widen = |src: &[u8], buf: &mut [u16; 2 * BLOCK]| {
694 for (d, &v) in buf[..src.len()].iter_mut().zip(src) {699 for (d, &v) in buf[..src.len()].iter_mut().zip(src) {
695 *d = u16::from(v);700 *d = u16::from(v);
@@ -703,21 +708,30 @@ fn intra_predict_u8(
703 }708 }
704 let above16 = above.map(|s| &above_buf[..s.len()]);709 let above16 = above.map(|s| &above_buf[..s.len()]);
705 let left16 = left.map(|s| &left_buf[..s.len()]);710 let left16 = left.map(|s| &left_buf[..s.len()]);
706- let dst16 = &mut dst_buf[..dst.len()];711+ let mut run = |dst16: &mut [u16]| {
707- crate::intra::predict(712+ crate::intra::predict(
708- mode,713+ mode,
709- angle_delta,714+ angle_delta,
710- above16,715+ above16,
711- left16,716+ left16,
712- corner.map(u16::from),717+ corner.map(u16::from),
713- bw,718+ bw,
714- bh,719+ bh,
715- enable_edge_filter,720+ enable_edge_filter,
716- smooth_neighbor,721+ smooth_neighbor,
717- dst16, fctx,722+ dst16, fctx,
718- );723+ );
719- for (d, &s) in dst.iter_mut().zip(dst16.iter()) {724+ for (d, &s) in dst.iter_mut().zip(dst16.iter()) {
720- *d = s as u8;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]);
721 }735 }
722 }736 }
723 737
@@ -1392,13 +1406,19 @@ impl Plane<'_> {
1392 /// they are not, or where the frame ends first, the edge is shorter and1406 /// they are not, or where the frame ends first, the edge is shorter and
1393 /// the predictor repeats its last sample, exactly as the decoder's clamp1407 /// the predictor repeats its last sample, exactly as the decoder's clamp
1394 /// to `aboveLimit` and `leftLimit` does (spec 7.11.2.2).1408 /// 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>(
1396 &self,1414 &self,
1397 x: usize,1415 x: usize,
1398 y: usize,1416 y: usize,
1399 side: usize,1417 side: usize,
1400 reach: Reach,1418 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>) {
1402 // Even a block's *own* edge is clamped to the true frame bound, not1422 // Even a block's *own* edge is clamped to the true frame bound, not
1403 // just a `reach` extension: libaom's `av1_predict_intra_block`1423 // just a `reach` extension: libaom's `av1_predict_intra_block`
1404 // (`av1/common/reconintra.c`) derives `xr`/`yd` -- the distance from1424 // (`av1/common/reconintra.c`) derives `xr`/`yd` -- the distance from
@@ -1436,16 +1456,24 @@ impl Plane<'_> {
1436 // samples above or left of it at all, same as `y == 0`/`x == 0`.1456 // samples above or left of it at all, same as `y == 0`/`x == 0`.
1437 let across = across.min(self.tile_x1);1457 let across = across.min(self.tile_x1);
1438 let down = down.min(self.tile_y1);1458 let down = down.min(self.tile_y1);
1439- let above = (y > self.tile_y0 && across > x)1459+ let above_n = (y > self.tile_y0 && across > x).then(|| across - x);
1440- .then(|| self.reconstruction[(y - 1) * self.width + x..][..across - x].to_vec());1460+ if let Some(n) = above_n {
1441- let left = (x > self.tile_x0 && down > y).then(|| {1461+ above_buf[..n]
1442- (y..down)1462+ .copy_from_slice(&self.reconstruction[(y - 1) * self.width + x..][..n]);
1443- .map(|row| self.reconstruction[row * self.width + x - 1])1463+ }
1444- .collect::<Vec<_>>()1464+ let left_n = (x > self.tile_x0 && down > y).then(|| down - y);
1445- });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+ }
1446 let corner = (x > self.tile_x0 && y > self.tile_y0)1470 let corner = (x > self.tile_x0 && y > self.tile_y0)
1447 .then(|| self.reconstruction[(y - 1) * self.width + x - 1]);1471 .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+ )
1449 }1477 }
1450 1478
1451 /// Codes one block under one mode without committing it: hands back the1479 /// Codes one block under one mode without committing it: hands back the
@@ -1507,7 +1535,9 @@ impl Plane<'_> {
1507 let At {1535 let At {
1508 x, y, side, reach, ..1536 x, y, side, reach, ..
1509 } = at;1537 } = 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);
1511 // Per-candidate scratch on the search's hottest path, so both buffers1541 // Per-candidate scratch on the search's hottest path, so both buffers
1512 // live on the stack -- `side` is at most `BLOCK` -- rather than1542 // live on the stack -- `side` is at most `BLOCK` -- rather than
1513 // costing a malloc/free pair per trial (`mc_trial` already does this).1543 // costing a malloc/free pair per trial (`mc_trial` already does this).
@@ -1516,8 +1546,8 @@ impl Plane<'_> {
1516 intra_predict_u8(1546 intra_predict_u8(
1517 mode,1547 mode,
1518 0,1548 0,
1519- above.as_deref(),1549+ above,
1520- left.as_deref(),1550+ left,
1521 corner,1551 corner,
1522 side,1552 side,
1523 side,1553 side,
@@ -1920,7 +1950,9 @@ impl Plane<'_> {
1920 let At {1950 let At {
1921 x, y, side, reach, ..1951 x, y, side, reach, ..
1922 } = at;1952 } = 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);
1924 modes1956 modes
1925 .iter()1957 .iter()
1926 .map(|&mode| {1958 .map(|&mode| {
@@ -1928,8 +1960,8 @@ impl Plane<'_> {
1928 intra_predict_u8(1960 intra_predict_u8(
1929 mode,1961 mode,
1930 0,1962 0,
1931- above.as_deref(),1963+ above,
1932- left.as_deref(),1964+ left,
1933 corner,1965 corner,
1934 side,1966 side,
1935 side,1967 side,
@@ -1962,7 +1994,9 @@ impl Plane<'_> {
1962 let At {1994 let At {
1963 x, y, side, reach, ..1995 x, y, side, reach, ..
1964 } = at;1996 } = 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);
1966 // Nothing ranks by the SAD pre-pass unless `top_k` prunes by it (or2000 // Nothing ranks by the SAD pre-pass unless `top_k` prunes by it (or
1967 // the census reports it), so summing it under the default unpruned2001 // the census reports it), so summing it under the default unpruned
1968 // search is `side * side` absolute differences per mode spent on a2002 // search is `side * side` absolute differences per mode spent on a
@@ -1976,8 +2010,8 @@ impl Plane<'_> {
1976 intra_predict_u8(2010 intra_predict_u8(
1977 mode,2011 mode,
1978 0,2012 0,
1979- above.as_deref(),2013+ above,
1980- left.as_deref(),2014+ left,
1981 corner,2015 corner,
1982 side,2016 side,
1983 side,2017 side,