forge
Explore
Log in

Don't you dare go Hollow

One walk over the region prices every candidate table

Browse files at this commit

19b1017f435b719453910fd3f13632c1d8b74742Ahmet Kaan GümüşParent: f4ce2b8
  • perf
    • one walk over the region prices every candidate table
Show full message
perf(mp3): one walk over the region prices every candidate table

best_table walked the region's pairs once per shortlisted table (up to
nine pair_bits passes per region) and plan_spectrum walked a tenth time
to price the winner; half the remaining profile lived in that loop. The
region is now walked once, accumulating every candidate's cost in
parallel, with a dead candidate dropped the moment a pair falls outside
it — the same accept/reject decisions in the same order, so every bench
fixture still encodes byte-identically and the suite is green. The
orphaned pair_bits helper is folded into the new walk. encode: stereo
CBR 37.3x -> 43.7x realtime, VBR 17.8x -> 20.9x.
crates/ec-mp3/src/encode.rsmodified+80 -36
@@ -19,7 +19,7 @@
19 19
20 use crate::filterbank::{Analysis, alias_expand};20 use crate::filterbank::{Analysis, alias_expand};
21 use crate::header::{ChannelMode, FrameHeader, Version};21 use crate::header::{ChannelMode, FrameHeader, Version};
22-use crate::huffman::{self, BitBuf, Table};22+use crate::huffman::{self, BitBuf};
23 use crate::tables::{23 use crate::tables::{
24 MAX_QUANT, SLEN, forward_mdct_long, forward_mdct_short, long_starts, power43, short_starts,24 MAX_QUANT, SLEN, forward_mdct_long, forward_mdct_short, long_starts, power43, short_starts,
25 short_widths, windows,25 short_widths, windows,
@@ -1608,15 +1608,9 @@ fn plan_spectrum(ix: &[i32; 576], block_type: u8, sample_rate: u32) -> SpectrumP
1608 for (region, to) in bounds.iter().enumerate() {1608 for (region, to) in bounds.iter().enumerate() {
1609 let to = (*to).min(big_end);1609 let to = (*to).min(big_end);
1610 if to > from {1610 if to > from {
1611- let select = best_table(ix, from, to);1611+ let (select, region_cost) = best_table_cost(ix, from, to);
1612 tables[region] = select;1612 tables[region] = select;
1613- let table = huffman::big_table(usize::from(select)).expect("table exists");1613+ cost += region_cost;
1614- let mut i = from;
1615- while i + 1 < to {
1616- cost += huffman::pair_bits(table, ix[i].unsigned_abs(), ix[i + 1].unsigned_abs())
1617- .expect("the chosen table codes every pair in its region");
1618- i += 2;
1619- }
1620 }1614 }
1621 from = to;1615 from = to;
1622 }1616 }
@@ -1694,40 +1688,90 @@ fn candidates(max: u32) -> &'static [u8] {
1694 }1688 }
1695 }1689 }
1696 1690
1697-/// The cheapest table for one region.1691+/// The cheapest table for one region, with its cost.
1698-fn best_table(ix: &[i32], from: usize, to: usize) -> u8 {1692+///
1699- let max = ix[from..to]1693+/// One walk over the pairs accumulates every shortlisted candidate's cost at
1700- .iter()1694+/// once; the former shape — a full `pair_bits` pass per candidate, up to nine
1701- .map(|v| v.unsigned_abs())1695+/// walks over the same pairs — was half of the encoder's remaining runtime.
1702- .max()1696+/// A candidate any pair falls outside dies on the spot, exactly as the
1703- .unwrap_or(0);1697+/// per-candidate `pair_bits` propagation did, and ties keep the earlier
1698+/// (nominally cheaper) candidate, as the strict less-than did.
1699+fn best_table_cost(ix: &[i32], from: usize, to: usize) -> (u8, u32) {
1700+ let mut max = 0u32;
1701+ for &v in &ix[from..to] {
1702+ max = max.max(v.unsigned_abs());
1703+ }
1704+ let shortlist = candidates(max);
1705+ // Per-candidate state, fetched once: the table, its escape shape, and a
1706+ // running cost. A candidate a pair rejects never revives, so `feasible`
1707+ // only ever clears.
1708+ let mut tables = [None; 9];
1709+ let mut tmax = [0u32; 9];
1710+ let mut escape = [false; 9];
1711+ let mut linbits = [0u32; 9];
1712+ let mut dim = [0u32; 9];
1713+ let mut feasible = [false; 9];
1714+ let mut costs = [0u32; 9];
1715+ let mut live = 0usize;
1716+ for (slot, &select) in shortlist.iter().enumerate() {
1717+ if let Ok(table) = huffman::big_table(usize::from(select)) {
1718+ tables[slot] = Some(table);
1719+ tmax[slot] = u32::from(table.dim) - 1;
1720+ escape[slot] = table.linbits > 0;
1721+ linbits[slot] = u32::from(table.linbits);
1722+ dim[slot] = u32::from(table.dim);
1723+ feasible[slot] = true;
1724+ live += 1;
1725+ }
1726+ }
1727+ let mut i = from;
1728+ while i + 1 < to && live > 0 {
1729+ let (x, y) = (ix[i].unsigned_abs(), ix[i + 1].unsigned_abs());
1730+ let signs = u32::from(x != 0) + u32::from(y != 0);
1731+ for slot in 0..shortlist.len() {
1732+ if !feasible[slot] {
1733+ continue;
1734+ }
1735+ let (cap, esc) = (tmax[slot], escape[slot]);
1736+ let out_of_range =
1737+ |value: u32| value > cap && (!esc || value > cap + (1 << linbits[slot]) - 1);
1738+ if out_of_range(x) || out_of_range(y) {
1739+ feasible[slot] = false;
1740+ live -= 1;
1741+ continue;
1742+ }
1743+ let table = tables[slot].expect("feasible candidates hold a table");
1744+ let (len, _) = table.codes[(x.min(cap) * dim[slot] + y.min(cap)) as usize];
1745+ if len == 0 && !(x == 0 && y == 0) {
1746+ feasible[slot] = false;
1747+ live -= 1;
1748+ continue;
1749+ }
1750+ let mut bits = u32::from(len);
1751+ if esc {
1752+ if x >= cap {
1753+ bits += linbits[slot];
1754+ }
1755+ if y >= cap {
1756+ bits += linbits[slot];
1757+ }
1758+ }
1759+ costs[slot] += bits + signs;
1760+ }
1761+ i += 2;
1762+ }
1704 let mut best = (u32::MAX, 0u8);1763 let mut best = (u32::MAX, 0u8);
1705- for &select in candidates(max) {1764+ for (slot, &select) in shortlist.iter().enumerate() {
1706- let Ok(table) = huffman::big_table(usize::from(select)) else {1765+ if feasible[slot] && costs[slot] < best.0 {
1707- continue;1766+ best = (costs[slot], select);
1708- };
1709- if let Some(cost) = table_cost(ix, from, to, table)
1710- && cost < best.0
1711- {
1712- best = (cost, select);
1713 }1767 }
1714 }1768 }
1715 if best.1 == 0 {1769 if best.1 == 0 {
1716 // Nothing in the shortlist fits, which only a value above the coding1770 // Nothing in the shortlist fits, which only a value above the coding
1717 // range can cause; fall back to the widest escape table.1771 // range can cause; fall back to the widest escape table.
1718- return 23;1772+ return (23, 0);
1719- }
1720- best.1
1721-}
1722-
1723-fn table_cost(ix: &[i32], from: usize, to: usize, table: Table) -> Option<u32> {
1724- let mut sum = 0;
1725- let mut i = from;
1726- while i + 1 < to {
1727- sum += huffman::pair_bits(table, ix[i].unsigned_abs(), ix[i + 1].unsigned_abs())?;
1728- i += 2;
1729 }1773 }
1730- Some(sum)1774+ (best.1, best.0)
1731 }1775 }
1732 1776
1733 /// The `scalefac_compress` index whose two lengths hold these scalefactors.1777 /// The `scalefac_compress` index whose two lengths hold these scalefactors.
crates/ec-mp3/src/huffman.rsmodified+0 -27
@@ -262,33 +262,6 @@ impl BitBuf {
262 }262 }
263 }263 }
264 264
265-/// Bits one pair costs in `table`, or `None` when the pair is out of range.
266-pub(crate) fn pair_bits(table: Table, x: u32, y: u32) -> Option<u32> {
267- let max = u32::from(table.dim) - 1;
268- let escape = u32::from(table.linbits) > 0;
269- let (ix, iy) = (x.min(max), y.min(max));
270- if (x > max && !escape) || (y > max && !escape) {
271- return None;
272- }
273- if escape && (x > max + (1 << table.linbits) - 1 || y > max + (1 << table.linbits) - 1) {
274- return None;
275- }
276- let (len, _) = table.codes[(ix * u32::from(table.dim) + iy) as usize];
277- if len == 0 && !(x == 0 && y == 0) {
278- return None;
279- }
280- let mut bits = u32::from(len);
281- for value in [x, y] {
282- if value >= max && escape {
283- bits += u32::from(table.linbits);
284- }
285- if value != 0 {
286- bits += 1;
287- }
288- }
289- Some(bits)
290-}
291-
292 /// Writes one pair; the caller has already checked it fits the table.265 /// Writes one pair; the caller has already checked it fits the table.
293 pub(crate) fn write_pair(writer: &mut BitBuf, table: Table, x: i32, y: i32) {266 pub(crate) fn write_pair(writer: &mut BitBuf, table: Table, x: i32, y: i32) {
294 let max = u32::from(table.dim) - 1;267 let max = u32::from(table.dim) - 1;