From 6d09e0a133d2be310b24e77e63efb2037fc52b19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Sun, 13 Sep 2026 05:31:31 +0300 Subject: [PATCH] perf(aac): the frame's noise search is memoised per (channel, band, scalefactor) -- band_noise is pure for one frame's coefficients, and the outer bisection's probes re-ask the same pairs, so each pair computes at most once per frame (stamp/cur generation, no clearing pass); fresh NoiseMemo per write_frame, capacity channels*max_sfb*256. The first cut keyed (band, sf) only and the byte-identity gate caught it instantly: stereo's two channels collided in the slots and one channel replayed the other's noise (10x deltas) -- the key now carries the channel slot. Byte-identical on stereo 256k and 5.1 512k vs base; stereo 42.1 -> 59.1x RT, 5.1 15.6 -> 21.8x (quiet-core min-of-7, both harnesses agreeing); ec-aac suite green 75/75 --- crates/ec-aac/src/encode.rs | 65 ++++++++++++++++++++++++++++++++++--- 1 file changed, 61 insertions(+), 4 deletions(-) diff --git a/crates/ec-aac/src/encode.rs b/crates/ec-aac/src/encode.rs index c5503f8e..201b39a1 100644 --- a/crates/ec-aac/src/encode.rs +++ b/crates/ec-aac/src/encode.rs @@ -535,9 +535,14 @@ impl AacEncoder { .collect() }) .collect(); - let cost = |enc: &AacEncoder, offset: i32| -> (i32, Vec) { + // Every noise query a probe makes is memoised for the frame: the + // bisection probes revisit the same (band, scalefactor) pairs across + // the outer loop, and band_noise is pure per frame. + let mut memo = NoiseMemo::new(self.channels * max_sfb * 256); + let mut cost = |enc: &AacEncoder, offset: i32| -> (i32, Vec) { let mut coded = Vec::new(); let mut bits = 10i32; // END element plus byte-alignment slack + let mut ch_slot = 0usize; for (plan, profiles) in elements.iter().zip(&profiles) { bits += 3 + 4 @@ -547,9 +552,11 @@ impl AacEncoder { 0 }; for (frame, profile) in plan.frames.iter().zip(profiles) { - let c = enc.quantise(frame, profile, seq, swb, max_sfb, offset); + let c = + enc.quantise(frame, profile, &mut memo, ch_slot, seq, swb, max_sfb, offset); bits += c.bits; coded.push(c); + ch_slot += 1; } } (bits, coded) @@ -620,6 +627,8 @@ impl AacEncoder { &self, frame: &ChannelFrame, profile: &BandProfile, + memo: &mut NoiseMemo, + ch_slot: usize, seq: WindowSequence, swb: &'static [u16], max_sfb: usize, @@ -648,11 +657,20 @@ impl AacEncoder { // on first overage; bisect for the largest level still under // target instead of walking it one step at a time. let mut best = floor[b]; - if self.band_noise(frame, swb, b, floor[b], windows, short) <= target { + if memo.noise( + ch_slot, + max_sfb, + b, + floor[b], + || self.band_noise(frame, swb, b, floor[b], windows, short), + ) <= target + { let (mut lo, mut hi) = (floor[b], 255); while lo < hi { let mid = lo + (hi - lo + 1) / 2; - if self.band_noise(frame, swb, b, mid, windows, short) <= target { + if memo.noise(ch_slot, max_sfb, b, mid, || { + self.band_noise(frame, swb, b, mid, windows, short) + }) <= target { lo = mid; } else { hi = mid - 1; @@ -859,6 +877,45 @@ fn band_profile(frame: &ChannelFrame, swb: &[u16], max_sfb: usize, short: bool) } } +/// Per-frame cache for the noise search: `band_noise(band, sf)` is pure for +/// one frame's coefficients, and the outer rate loop's probes re-ask the same +/// (band, scalefactor) pairs, so each pair is computed at most once per frame +/// clearing pass. +struct NoiseMemo { + stamp: Vec, + val: Vec, + cur: u32, +} + +impl NoiseMemo { + fn new(capacity: usize) -> NoiseMemo { + NoiseMemo { + stamp: vec![0; capacity], + val: vec![0.0; capacity], + cur: 1, + } + } + + /// The memoised noise at `(band, sf)`, computing through `f` on a miss. + fn noise( + &mut self, + ch_slot: usize, + max_sfb: usize, + band: usize, + sf: i32, + f: impl FnOnce() -> f32, + ) -> f32 { + let idx = (ch_slot * max_sfb + band) * 256 + sf as usize; + if self.stamp[idx] == self.cur { + return self.val[idx]; + } + let noise = f(); + self.stamp[idx] = self.cur; + self.val[idx] = noise; + noise + } +} + fn apply_ms(pair: &mut [ChannelFrame], ms: &[bool], swb: &[u16], max_sfb: usize, short: bool) { let windows = if short { 8 } else { 1 }; for b in 0..max_sfb {