From dcaf6bb94c99f7891dc187ec0eb1986edc94331d 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:51:21 +0300 Subject: [PATCH] perf(mp3): the encoder stops shuffling its own buffers Three allocation/memmove habits from the profile's tail: the packet layer drained one frame at a time, memmoving the whole remaining push per frame (quadratic on a single large push); encode_block built a fresh deinterleave Vec per channel per frame; emit_frame cloned every held granule (subbands and thresholds) to satisfy a borrow that a drain before the loop removes. Byte-identical on all eight fixtures, suite green. encode: stereo CBR 96.0x -> 109.8x realtime (ratio 0.693), VBR 52.4x -> 55.7x. --- crates/ec-mp3/src/encode.rs | 66 +++++++++++++++++++++++-------------- 1 file changed, 41 insertions(+), 25 deletions(-) diff --git a/crates/ec-mp3/src/encode.rs b/crates/ec-mp3/src/encode.rs index 4adc9336..faace113 100644 --- a/crates/ec-mp3/src/encode.rs +++ b/crates/ec-mp3/src/encode.rs @@ -137,6 +137,8 @@ pub struct Mp3Encode { fft: RealFft, fft_window: Window, psy_history: Vec>, + /// Deinterleave scratch for `encode_block`, grown once and reused. + pcm_scratch: Vec, /// The main-data byte stream and how much of it frames have carried. stream: Vec, used: usize, @@ -219,6 +221,7 @@ impl Mp3Encode { assigned: 0, written: 0, queued: std::collections::VecDeque::new(), + pcm_scratch: Vec::new(), ready: std::collections::VecDeque::new(), frames: 0, frame_bytes: 0, @@ -350,26 +353,34 @@ impl Mp3Encoder { } fn drain(&mut self, flush: bool) { - let Some(core) = self.core.as_mut() else { + let Self { + core, pcm, frames, .. + } = self; + let Some(core) = core.as_mut() else { return; }; let channels = core.channels; let need = core.frame_samples() * channels; - while self.pcm.len() >= need { - let block: Vec = self.pcm.drain(..need).collect(); - core.encode_block(&block); - } + // Encode behind a read cursor and shift the leftovers once: draining + // per frame memmoved the whole remaining push for every frame, which + // on a single large push is quadratic in the file length. + let mut pos = 0usize; + while pcm.len() - pos >= need { + core.encode_block(&pcm[pos..pos + need]); + pos += need; + } + pcm.drain(..pos); if flush { - if !self.pcm.is_empty() { - let mut block = std::mem::take(&mut self.pcm); - block.resize(need, 0.0); - core.encode_block(&block); + if !pcm.is_empty() { + pcm.resize(need, 0.0); + core.encode_block(pcm); + pcm.clear(); } core.flush_pending(); core.flush_frames(); } while let Some(frame) = core.ready.pop_front() { - self.frames.push_back(frame); + frames.push_back(frame); } } @@ -514,10 +525,15 @@ impl Mp3Encode { let granules = self.granules(); // Polyphase analysis, one channel at a time. for ch in 0..channels { - let mut mono: Vec = Vec::with_capacity(pcm.len() / channels); + // One deinterleave buffer per encoder, grown once: this frame's + // mono channel used to be a fresh allocation per channel per + // frame. + self.pcm_scratch.clear(); + self.pcm_scratch.reserve(pcm.len() / channels); for frame in pcm.chunks(channels) { - mono.push(frame[ch]); + self.pcm_scratch.push(frame[ch]); } + let mono = &self.pcm_scratch; for chunk in mono.chunks(32) { if chunk.len() < 32 { break; @@ -526,7 +542,13 @@ impl Mp3Encode { self.slots[ch].push(slot); } } - self.psy_push(ch, &mono); + // The masking model's history update, inlined from `psy_push` so + // it can touch `psy_history` while `mono` borrows `pcm_scratch`. + let n = mono.len().min(1024); + let history = &mut self.psy_history[ch]; + history.copy_within(n.., 0); + let keep = 1024 - n; + history[keep..].copy_from_slice(&mono[mono.len() - n..]); } // A granule is 18 slots; hold one back so block switching can look // ahead at the next granule's energy. @@ -557,15 +579,6 @@ impl Mp3Encode { } } - /// One FFT per granule of input, kept for the masking model. - fn psy_push(&mut self, ch: usize, mono: &[f32]) { - let n = 1024.min(mono.len()); - let history = &mut self.psy_history[ch]; - history.copy_within(n.., 0); - let keep = 1024 - n; - history[keep..].copy_from_slice(&mono[mono.len() - n..]); - } - /// Allowed noise per long scalefactor band, from the masking model, in /// the units `code_with` measures quantisation noise in (summed squared /// MDCT lines), so noise / threshold is a noise-to-mask ratio. @@ -702,18 +715,21 @@ impl Mp3Encode { // every bitrate candidate VBR tries below; compute them once. let mut xrs: Vec> = Vec::with_capacity(granules * channels); let mut thresholds: Vec> = Vec::with_capacity(granules * channels); + // Take the held-back granules once: the per-granule clone that used + // to feed `mdct` copied every subband and threshold just to keep the + // borrow checker happy about `history`. + let held: Vec = self.pending.drain(..granules * channels).collect(); for gr in 0..granules { for ch in 0..channels { let index = gr * channels + ch; - let granule = self.pending[index].clone(); + let granule = &held[index]; xrs.push(self.mdct(ch, &granule.subband, types[index])); thresholds.push(match vbr { Some(quality) => quality_threshold(&granule.threshold, quality), - None => granule.threshold, + None => granule.threshold.clone(), }); } } - self.pending.drain(..granules * channels); // Mid/side stereo. The two channels of real music are mostly the same // signal, and coding their sum and difference puts nearly all of the