forge
Explore
Log in

Don't you dare go Hollow

Stop allocating per row and per intra prediction

Browse files at this commit

013dc910acd0c4f6a77a8f3dad589a66d3f5c9a6Ahmet Kaan GümüşParent: fae8c1b
  • perf
    • stop allocating per row and per intra prediction
Show full message
perf(av1): stop allocating per row and per intra prediction

`Plane::source_block`/`snapshot` built one `Vec` per row of the square they
copied and then collected those; `rows_of` copies straight into one buffer.
`intra_predict_u8` allocated its two widened edges and its `u16`
destination on every call -- once per mode per trial -- and all three fit on
the stack at the sizes the encoder uses.

No arithmetic changed; the enc_probe FNV set on the three gate clips is
unmoved. 1080p ladder: 101.65e9 -> 98.27e9 instructions:u (-11.5% against
the b5373c65 baseline's 111.09e9, ABAB x2).
crates/ec-av1/src/encode.rsmodified+38 -12
@@ -537,20 +537,37 @@ fn intra_predict_u8(
537537 smooth_neighbor: bool,
538538 dst: &mut [u8], fctx: &crate::decode::FrameCtx,
539539 ) {
540- let above16: Option<Vec<u16>> = above.map(|s| s.iter().map(|&v| u16::from(v)).collect());
541- let left16: Option<Vec<u16>> = left.map(|s| s.iter().map(|&v| u16::from(v)).collect());
542- let mut dst16 = vec![0u16; dst.len()];
540+ // Three heap allocations per call became three stack buffers: this runs
541+ // once per mode per trial, and an edge is at most `bw + bh` samples while
542+ // a block is at most `BLOCK * BLOCK`. Same samples either way.
543+ let mut above_buf = [0u16; 2 * BLOCK];
544+ let mut left_buf = [0u16; 2 * BLOCK];
545+ let mut dst_buf = [0u16; BLOCK * BLOCK];
546+ let widen = |src: &[u8], buf: &mut [u16; 2 * BLOCK]| {
547+ for (d, &v) in buf[..src.len()].iter_mut().zip(src) {
548+ *d = u16::from(v);
549+ }
550+ };
551+ if let Some(s) = above {
552+ widen(s, &mut above_buf);
553+ }
554+ if let Some(s) = left {
555+ widen(s, &mut left_buf);
556+ }
557+ let above16 = above.map(|s| &above_buf[..s.len()]);
558+ let left16 = left.map(|s| &left_buf[..s.len()]);
559+ let dst16 = &mut dst_buf[..dst.len()];
543560 crate::intra::predict(
544561 mode,
545562 angle_delta,
546- above16.as_deref(),
547- left16.as_deref(),
563+ above16,
564+ left16,
548565 corner.map(u16::from),
549566 bw,
550567 bh,
551568 enable_edge_filter,
552569 smooth_neighbor,
553- &mut dst16, fctx,
570+ dst16, fctx,
554571 );
555572 for (d, &s) in dst.iter_mut().zip(dst16.iter()) {
556573 *d = s as u8;
@@ -1459,17 +1476,13 @@ impl Plane<'_> {
14591476 /// `source`), since [`Self::source`] itself is the whole plane, strided
14601477 /// by [`Self::width`].
14611478 fn source_block(&self, x: usize, y: usize, side: usize) -> Vec<u8> {
1462- (y..y + side)
1463- .flat_map(|row| self.source[row * self.width + x..][..side].to_vec())
1464- .collect()
1479+ rows_of(self.source, self.width, x, y, side)
14651480 }
14661481
14671482 /// The reconstructed samples of one square, so that a partition trial can
14681483 /// be undone.
14691484 fn snapshot(&self, x: usize, y: usize, side: usize) -> Vec<u8> {
1470- (y..y + side)
1471- .flat_map(|row| self.reconstruction[row * self.width + x..][..side].to_vec())
1472- .collect()
1485+ rows_of(&self.reconstruction, self.width, x, y, side)
14731486 }
14741487
14751488 /// Puts a snapshot back.
@@ -3139,6 +3152,19 @@ fn record_mi(grid: &mut MiGrid, mi_row: usize, mi_col: usize, size: u8, inter: O
31393152
31403153 /// The reconstructed samples one 32x32 square covers in all three planes, so
31413154 /// that a partition trial can be undone.
3155+/// One square of a plane, row-major, the inverse of [`Plane::restore`].
3156+///
3157+/// Same bytes as the `flat_map(|row| ..to_vec()).collect()` this replaces,
3158+/// which allocated and freed a `Vec` per row of every block it copied --
3159+/// `Plane::snapshot` runs once per partition trial.
3160+fn rows_of(plane: &[u8], width: usize, x: usize, y: usize, side: usize) -> Vec<u8> {
3161+ let mut out = vec![0u8; side * side];
3162+ for row in 0..side {
3163+ out[row * side..][..side].copy_from_slice(&plane[(y + row) * width + x..][..side]);
3164+ }
3165+ out
3166+}
3167+
31423168 fn snapshot(
31433169 luma: &Plane,
31443170 chroma: &[Plane; 2],