forge
Explore
Log in

Don't you dare go Hollow

Intra edges on the stack

Browse files at this commit

ee5b8d85381849097eaf39a506488991f4a0f18cAhmet Kaan GümüşParent: e7ac08e
  • perf
    • intra edges on the stack
Show full message
perf(av1): intra edges on the stack

An edge is at most bw + bh + 1 = 129 samples, so Edges holds two arrays
instead of two heap buffers, and the edge filter's working copy is a
stack array too: two allocations gone per predicted block, on a stream
whose intra blocks are mostly 4x4 to 16x16.

Byte-exact on all four gate streams; -0.07% instructions on the 4K
segment, -0.08% on the 1080p one (malloc latency does not show on
instructions:u).
crates/ec-av1/src/intra.rsmodified+19 -10
@@ -136,8 +136,11 @@ const ANGLE_STEP: i32 = 3;
136136 /// Both arrays hold the corner first, so the spec's index `-1` is index 0 here
137137 /// and they run out to the spec's `w + h - 1`.
138138 struct Edges {
139- above: Vec<i32>,
140- left: Vec<i32>,
139+ // lane-intra: `bw + bh + 1` is at most 129 samples, so both edges live on
140+ // the stack -- two heap allocations per predicted block otherwise, and
141+ // most blocks a stream predicts are 4x4 to 16x16.
142+ above: [i32; 129],
143+ left: [i32; 129],
141144 }
142145
143146 impl Edges {
@@ -172,16 +175,21 @@ impl Edges {
172175 // first and the samples are extended/truncated to `want` in place, so
173176 // the contents are what `extend` + `with_corner` produced before.
174177 let build_edge = |samples: Option<&[u16]>, other: Option<&[u16]>, none_fill: i32| {
175- let mut v: Vec<i32> = Vec::with_capacity(want + 1);
176- v.push(corner);
178+ let mut v = [0i32; 129];
179+ v[0] = corner;
177180 match (samples, other) {
178181 (Some(s), _) => {
179- v.extend(s.iter().map(|&x| i32::from(x)));
180- let last = *v.last().expect("an edge that exists has samples");
181- v.resize(want + 1, last);
182+ let n = s.len().min(want);
183+ for (d, &x) in v[1..=n].iter_mut().zip(s.iter()) {
184+ *d = i32::from(x);
185+ }
186+ if n < want {
187+ let last = i32::from(*s.last().expect("an edge that exists has samples"));
188+ v[n + 1..=want].fill(last);
189+ }
182190 }
183- (None, Some(o)) => v.resize(want + 1, i32::from(o[0])),
184- (None, None) => v.resize(want + 1, none_fill),
191+ (None, Some(o)) => v[1..=want].fill(i32::from(o[0])),
192+ (None, None) => v[1..=want].fill(none_fill),
185193 }
186194 v
187195 };
@@ -479,7 +487,8 @@ fn filter_intra_edge(buf: &mut [i32], strength: i32) {
479487 const KERNEL: [[i32; 5]; 3] = [[0, 4, 8, 4, 0], [0, 5, 6, 5, 0], [2, 4, 4, 4, 2]];
480488 let filt = usize::try_from(strength - 1).expect("a filter strength is never negative");
481489 let sz = buf.len() as i32;
482- let edge = buf.to_vec();
490+ let mut edge = [0i32; 129];
491+ edge[..buf.len()].copy_from_slice(buf);
483492 for i in 1..buf.len() {
484493 let mut s = 0;
485494 for (j, &tap) in KERNEL[filt].iter().enumerate() {