From 5081e9a6c6f89bcb1a0db8be859cb1d039b4180b 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:57:56 +0300 Subject: [PATCH] perf(mp3): the region walk prices zero pairs once and drops out-of-range tables upfront The per-pair range check never fired: the shortlist is built from the region's maximum, so a candidate that would reject any pair rejects the one carrying that maximum, and the check now happens once per candidate before the walk. Zero pairs cost every candidate the same single code length and take a dedicated path. Same accept/reject decisions in the same order; seven of eight fixtures byte-identical (music3s inherited its one-ulp difference from the gain-search step, correlation unchanged to six decimals). Suite green. encode: stereo CBR 127.9x -> 139.2x realtime (ratio 0.886), VBR 67.5x -> 74.3x, mono tone 55.5x -> 78.3x. --- crates/ec-mp3/src/encode.rs | 66 ++++++++++++++++++++++--------------- 1 file changed, 39 insertions(+), 27 deletions(-) diff --git a/crates/ec-mp3/src/encode.rs b/crates/ec-mp3/src/encode.rs index cae2a40a..589da150 100644 --- a/crates/ec-mp3/src/encode.rs +++ b/crates/ec-mp3/src/encode.rs @@ -1799,61 +1799,73 @@ fn best_table_cost(ix: &[i32], from: usize, to: usize) -> (u8, u32) { max = max.max(v.unsigned_abs()); } let shortlist = candidates(max); - // Per-candidate state, fetched once: the table, its escape shape, and a - // running cost. A candidate a pair rejects never revives, so `feasible` - // only ever clears. + // Per-candidate state, fetched once: the table, its escape shape, the + // cost of a (0, 0) pair, and a running cost. A candidate the region's + // largest magnitude falls outside is dropped before the walk — exactly + // what the per-pair range check did, since some pair carries `max`. let mut tables = [None; 9]; let mut tmax = [0u32; 9]; - let mut escape = [false; 9]; let mut linbits = [0u32; 9]; let mut dim = [0u32; 9]; + let mut zero_bits = [0u32; 9]; let mut feasible = [false; 9]; let mut costs = [0u32; 9]; let mut live = 0usize; for (slot, &select) in shortlist.iter().enumerate() { - if let Ok(table) = huffman::big_table(usize::from(select)) { - tables[slot] = Some(table); - tmax[slot] = u32::from(table.dim) - 1; - escape[slot] = table.linbits > 0; - linbits[slot] = u32::from(table.linbits); - dim[slot] = u32::from(table.dim); - feasible[slot] = true; - live += 1; - } + let Ok(table) = huffman::big_table(usize::from(select)) else { + continue; + }; + let cap = u32::from(table.dim) - 1; + let escape = table.linbits > 0; + let reach = cap + if escape { (1 << table.linbits) - 1 } else { 0 }; + if max > reach { + continue; + } + tables[slot] = Some(table); + tmax[slot] = cap; + linbits[slot] = if escape { u32::from(table.linbits) } else { 0 }; + dim[slot] = u32::from(table.dim); + zero_bits[slot] = u32::from(table.codes[0].0); + feasible[slot] = true; + live += 1; } let mut i = from; while i + 1 < to && live > 0 { let (x, y) = (ix[i].unsigned_abs(), ix[i + 1].unsigned_abs()); + if x == 0 && y == 0 { + for slot in 0..shortlist.len() { + if feasible[slot] { + costs[slot] += zero_bits[slot]; + } + } + i += 2; + continue; + } let signs = u32::from(x != 0) + u32::from(y != 0); for slot in 0..shortlist.len() { if !feasible[slot] { continue; } - let (cap, esc) = (tmax[slot], escape[slot]); - let out_of_range = - |value: u32| value > cap && (!esc || value > cap + (1 << linbits[slot]) - 1); - if out_of_range(x) || out_of_range(y) { - feasible[slot] = false; - live -= 1; - continue; - } let table = tables[slot].expect("feasible candidates hold a table"); + let (cap, lin) = (tmax[slot], linbits[slot]); let (len, _) = table.codes[(x.min(cap) * dim[slot] + y.min(cap)) as usize]; - if len == 0 && !(x == 0 && y == 0) { + if len == 0 { + // A hole in the table: this candidate cannot code the pair, + // so it is out for the whole region. feasible[slot] = false; live -= 1; continue; } - let mut bits = u32::from(len); - if esc { + let mut bits = u32::from(len) + signs; + if lin > 0 { if x >= cap { - bits += linbits[slot]; + bits += lin; } if y >= cap { - bits += linbits[slot]; + bits += lin; } } - costs[slot] += bits + signs; + costs[slot] += bits; } i += 2; }