Intra edges on the stack
Browse files at this commitee5b8d85381849097eaf39a506488991f4a0f18cParent: e7ac08e- perf
- intra edges on the stack
1 changed file +19 -10
- crates/ec-av1/src/intra.rs +19 -10
crates/ec-av1/src/intra.rsmodified+19 -10
| @@ -136,8 +136,11 @@ const ANGLE_STEP: i32 = 3; | ||
| 136 | 136 | /// Both arrays hold the corner first, so the spec's index `-1` is index 0 here |
| 137 | 137 | /// and they run out to the spec's `w + h - 1`. |
| 138 | 138 | 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], | |
| 141 | 144 | } |
| 142 | 145 | |
| 143 | 146 | impl Edges { |
| @@ -172,16 +175,21 @@ impl Edges { | ||
| 172 | 175 | // first and the samples are extended/truncated to `want` in place, so |
| 173 | 176 | // the contents are what `extend` + `with_corner` produced before. |
| 174 | 177 | 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; | |
| 177 | 180 | match (samples, other) { |
| 178 | 181 | (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 | + } | |
| 182 | 190 | } |
| 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), | |
| 185 | 193 | } |
| 186 | 194 | v |
| 187 | 195 | }; |
| @@ -479,7 +487,8 @@ fn filter_intra_edge(buf: &mut [i32], strength: i32) { | ||
| 479 | 487 | const KERNEL: [[i32; 5]; 3] = [[0, 4, 8, 4, 0], [0, 5, 6, 5, 0], [2, 4, 4, 4, 2]]; |
| 480 | 488 | let filt = usize::try_from(strength - 1).expect("a filter strength is never negative"); |
| 481 | 489 | 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); | |
| 483 | 492 | for i in 1..buf.len() { |
| 484 | 493 | let mut s = 0; |
| 485 | 494 | for (j, &tap) in KERNEL[filt].iter().enumerate() { |