Stop allocating per row and per intra prediction
Browse files at this commit013dc910acd0c4f6a77a8f3dad589a66d3f5c9a6Parent: fae8c1b- perf
- stop allocating per row and per intra prediction
1 changed file +38 -12
- crates/ec-av1/src/encode.rs +38 -12
crates/ec-av1/src/encode.rsmodified+38 -12
| @@ -537,20 +537,37 @@ fn intra_predict_u8( | ||
| 537 | 537 | smooth_neighbor: bool, |
| 538 | 538 | dst: &mut [u8], fctx: &crate::decode::FrameCtx, |
| 539 | 539 | ) { |
| 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()]; | |
| 543 | 560 | crate::intra::predict( |
| 544 | 561 | mode, |
| 545 | 562 | angle_delta, |
| 546 | - above16.as_deref(), | |
| 547 | - left16.as_deref(), | |
| 563 | + above16, | |
| 564 | + left16, | |
| 548 | 565 | corner.map(u16::from), |
| 549 | 566 | bw, |
| 550 | 567 | bh, |
| 551 | 568 | enable_edge_filter, |
| 552 | 569 | smooth_neighbor, |
| 553 | - &mut dst16, fctx, | |
| 570 | + dst16, fctx, | |
| 554 | 571 | ); |
| 555 | 572 | for (d, &s) in dst.iter_mut().zip(dst16.iter()) { |
| 556 | 573 | *d = s as u8; |
| @@ -1459,17 +1476,13 @@ impl Plane<'_> { | ||
| 1459 | 1476 | /// `source`), since [`Self::source`] itself is the whole plane, strided |
| 1460 | 1477 | /// by [`Self::width`]. |
| 1461 | 1478 | 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) | |
| 1465 | 1480 | } |
| 1466 | 1481 | |
| 1467 | 1482 | /// The reconstructed samples of one square, so that a partition trial can |
| 1468 | 1483 | /// be undone. |
| 1469 | 1484 | 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) | |
| 1473 | 1486 | } |
| 1474 | 1487 | |
| 1475 | 1488 | /// Puts a snapshot back. |
| @@ -3139,6 +3152,19 @@ fn record_mi(grid: &mut MiGrid, mi_row: usize, mi_col: usize, size: u8, inter: O | ||
| 3139 | 3152 | |
| 3140 | 3153 | /// The reconstructed samples one 32x32 square covers in all three planes, so |
| 3141 | 3154 | /// 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 | + | |
| 3142 | 3168 | fn snapshot( |
| 3143 | 3169 | luma: &Plane, |
| 3144 | 3170 | chroma: &[Plane; 2], |