forge
Explore
Log in

Don't you dare go Hollow

The region walk prices zero pairs once and drops out-of-range tables upfront

Browse files at this commit

5081e9a6c6f89bcb1a0db8be859cb1d039b4180bAhmet Kaan GümüşParent: 59ae240
  • perf
    • the region walk prices zero pairs once and drops out-of-range tables upfront
Show full message
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.rsmodified+39 -27
@@ -1799,61 +1799,73 @@ fn best_table_cost(ix: &[i32], from: usize, to: usize) -> (u8, u32) {
1799 max = max.max(v.unsigned_abs());1799 max = max.max(v.unsigned_abs());
1800 }1800 }
1801 let shortlist = candidates(max);1801 let shortlist = candidates(max);
1802- // Per-candidate state, fetched once: the table, its escape shape, and a1802+ // Per-candidate state, fetched once: the table, its escape shape, the
1803- // running cost. A candidate a pair rejects never revives, so `feasible`1803+ // cost of a (0, 0) pair, and a running cost. A candidate the region's
1804- // only ever clears.1804+ // largest magnitude falls outside is dropped before the walk — exactly
1805+ // what the per-pair range check did, since some pair carries `max`.
1805 let mut tables = [None; 9];1806 let mut tables = [None; 9];
1806 let mut tmax = [0u32; 9];1807 let mut tmax = [0u32; 9];
1807- let mut escape = [false; 9];
1808 let mut linbits = [0u32; 9];1808 let mut linbits = [0u32; 9];
1809 let mut dim = [0u32; 9];1809 let mut dim = [0u32; 9];
1810+ let mut zero_bits = [0u32; 9];
1810 let mut feasible = [false; 9];1811 let mut feasible = [false; 9];
1811 let mut costs = [0u32; 9];1812 let mut costs = [0u32; 9];
1812 let mut live = 0usize;1813 let mut live = 0usize;
1813 for (slot, &select) in shortlist.iter().enumerate() {1814 for (slot, &select) in shortlist.iter().enumerate() {
1814- if let Ok(table) = huffman::big_table(usize::from(select)) {1815+ let Ok(table) = huffman::big_table(usize::from(select)) else {
1815- tables[slot] = Some(table);1816+ continue;
1816- tmax[slot] = u32::from(table.dim) - 1;1817+ };
1817- escape[slot] = table.linbits > 0;1818+ let cap = u32::from(table.dim) - 1;
1818- linbits[slot] = u32::from(table.linbits);1819+ let escape = table.linbits > 0;
1819- dim[slot] = u32::from(table.dim);1820+ let reach = cap + if escape { (1 << table.linbits) - 1 } else { 0 };
1820- feasible[slot] = true;1821+ if max > reach {
1821- live += 1;1822+ continue;
1822- }1823+ }
1824+ tables[slot] = Some(table);
1825+ tmax[slot] = cap;
1826+ linbits[slot] = if escape { u32::from(table.linbits) } else { 0 };
1827+ dim[slot] = u32::from(table.dim);
1828+ zero_bits[slot] = u32::from(table.codes[0].0);
1829+ feasible[slot] = true;
1830+ live += 1;
1823 }1831 }
1824 let mut i = from;1832 let mut i = from;
1825 while i + 1 < to && live > 0 {1833 while i + 1 < to && live > 0 {
1826 let (x, y) = (ix[i].unsigned_abs(), ix[i + 1].unsigned_abs());1834 let (x, y) = (ix[i].unsigned_abs(), ix[i + 1].unsigned_abs());
1835+ if x == 0 && y == 0 {
1836+ for slot in 0..shortlist.len() {
1837+ if feasible[slot] {
1838+ costs[slot] += zero_bits[slot];
1839+ }
1840+ }
1841+ i += 2;
1842+ continue;
1843+ }
1827 let signs = u32::from(x != 0) + u32::from(y != 0);1844 let signs = u32::from(x != 0) + u32::from(y != 0);
1828 for slot in 0..shortlist.len() {1845 for slot in 0..shortlist.len() {
1829 if !feasible[slot] {1846 if !feasible[slot] {
1830 continue;1847 continue;
1831 }1848 }
1832- let (cap, esc) = (tmax[slot], escape[slot]);
1833- let out_of_range =
1834- |value: u32| value > cap && (!esc || value > cap + (1 << linbits[slot]) - 1);
1835- if out_of_range(x) || out_of_range(y) {
1836- feasible[slot] = false;
1837- live -= 1;
1838- continue;
1839- }
1840 let table = tables[slot].expect("feasible candidates hold a table");1849 let table = tables[slot].expect("feasible candidates hold a table");
1850+ let (cap, lin) = (tmax[slot], linbits[slot]);
1841 let (len, _) = table.codes[(x.min(cap) * dim[slot] + y.min(cap)) as usize];1851 let (len, _) = table.codes[(x.min(cap) * dim[slot] + y.min(cap)) as usize];
1842- if len == 0 && !(x == 0 && y == 0) {1852+ if len == 0 {
1853+ // A hole in the table: this candidate cannot code the pair,
1854+ // so it is out for the whole region.
1843 feasible[slot] = false;1855 feasible[slot] = false;
1844 live -= 1;1856 live -= 1;
1845 continue;1857 continue;
1846 }1858 }
1847- let mut bits = u32::from(len);1859+ let mut bits = u32::from(len) + signs;
1848- if esc {1860+ if lin > 0 {
1849 if x >= cap {1861 if x >= cap {
1850- bits += linbits[slot];1862+ bits += lin;
1851 }1863 }
1852 if y >= cap {1864 if y >= cap {
1853- bits += linbits[slot];1865+ bits += lin;
1854 }1866 }
1855 }1867 }
1856- costs[slot] += bits + signs;1868+ costs[slot] += bits;
1857 }1869 }
1858 i += 2;1870 i += 2;
1859 }1871 }