Paletted containers: build palettes without hashing

palette_ids (behind PalettedContainer::pack, every saved and sent section,
and from_values, every generated one) scanned the palette for each of the
4096 values while it had up to 16 entries, then hashed every value with
SipHash. A run of one value is now matched once, and past 16 entries a
thread-local table indexed by value (block states and biomes are below
65536) replaces the hash map; only the palette's entries are set, and they
are cleared before returning. The output is unchanged: palette in
first-occurrence order and the same ids.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-18 12:01:12 +02:00
parent 256f7c200a
commit fdee6576c6
+198 -15
View File
@@ -151,33 +151,85 @@ fn write(words: &mut [u64], bits: u8, i: usize, value: u32) {
let (word, shift) = locate(bits, i);
words[word] = (words[word] & !(mask(bits) << shift)) | (u64::from(value) << shift);
}
/// Values below this are looked up in a table indexed by value once a
/// palette outgrows the scan: every block state and biome id of 26.3.
const DENSE_VALUES: u32 = 1 << 16;
thread_local! {
/// `palette_ids`' table: 1 + the id of each value seen, 0 for the others.
/// Only the entries of the palette being built are set, and they are
/// cleared again before it returns.
static DENSE_IDS: std::cell::RefCell<Vec<u32>> = const { std::cell::RefCell::new(Vec::new()) };
}
/// The distinct `values` in first-occurrence order and each value's index in
/// them. Up to 16 distinct values a scan finds them faster than hashing.
/// them. A run of one value (most of a section) is matched once, a scan finds
/// up to 16 distinct values faster than any lookup, and past that a table
/// indexed by value finds them without hashing.
fn palette_ids(values: &[u32]) -> (Vec<u32>, Vec<u32>) {
let mut palette: Vec<u32> = Vec::new();
let mut small = true;
let mut lookup: std::collections::HashMap<u32, u32> = std::collections::HashMap::new();
let mut ids = Vec::with_capacity(values.len());
for &value in values {
let id = if small {
match palette.iter().position(|&v| v == value) {
let mut last: Option<(u32, u32)> = None;
let mut rest = values;
while let Some((&value, tail)) = rest.split_first() {
let id = match last {
Some((v, id)) if v == value => id,
_ if palette.len() > 16 => break,
_ => match palette.iter().position(|&v| v == value) {
Some(id) => id as u32,
None => {
palette.push(value);
if palette.len() > 16 {
small = false;
lookup.extend(palette.iter().enumerate().map(|(i, &v)| (v, i as u32)));
}
palette.len() as u32 - 1
}
},
};
last = Some((value, id));
ids.push(id);
rest = tail;
}
if rest.is_empty() {
return (palette, ids);
}
if palette.iter().chain(rest).all(|&v| v < DENSE_VALUES) {
DENSE_IDS.with(|table| {
let mut table = table.borrow_mut();
for (id, &v) in palette.iter().enumerate() {
let v = v as usize;
if table.len() <= v {
table.resize(v + 1, 0);
}
table[v] = id as u32 + 1;
}
} else {
*lookup.entry(value).or_insert_with(|| {
for &value in rest {
let v = value as usize;
if table.len() <= v {
table.resize(v + 1, 0);
}
let id = match table[v] {
0 => {
palette.push(value);
table[v] = palette.len() as u32;
palette.len() as u32 - 1
}
slot => slot - 1,
};
ids.push(id);
}
for &v in &palette {
table[v as usize] = 0;
}
});
} else {
let mut lookup: std::collections::HashMap<u32, u32> = palette
.iter()
.enumerate()
.map(|(i, &v)| (v, i as u32))
.collect();
for &value in rest {
let id = *lookup.entry(value).or_insert_with(|| {
palette.push(value);
palette.len() as u32 - 1
})
};
ids.push(id);
});
ids.push(id);
}
}
(palette, ids)
}
@@ -828,4 +880,135 @@ mod tests {
);
}
}
/// The pre-optimization `palette_ids` (scan, then a SipHash map), kept to
/// compare against.
fn baseline_palette_ids(values: &[u32]) -> (Vec<u32>, Vec<u32>) {
let mut palette: Vec<u32> = Vec::new();
let mut small = true;
let mut lookup: std::collections::HashMap<u32, u32> = std::collections::HashMap::new();
let mut ids = Vec::with_capacity(values.len());
for &value in values {
let id = if small {
match palette.iter().position(|&v| v == value) {
Some(id) => id as u32,
None => {
palette.push(value);
if palette.len() > 16 {
small = false;
lookup.extend(palette.iter().enumerate().map(|(i, &v)| (v, i as u32)));
}
palette.len() as u32 - 1
}
}
} else {
*lookup.entry(value).or_insert_with(|| {
palette.push(value);
palette.len() as u32 - 1
})
};
ids.push(id);
}
(palette, ids)
}
/// Sections like the ones saved: stone, ores and caves under dirt, grass
/// and air; a mixed surface (plants, leaves, logs) in short runs; a
/// global palette; ids past the table; and biomes.
fn palette_cases() -> Vec<(&'static str, Vec<u32>)> {
let mut state = 0x9E37_79B9_7F4A_7C15u64;
let mut terrain = Vec::with_capacity(4096);
for i in 0..4096u32 {
let y = i >> 8;
let r = rng(&mut state);
terrain.push(match y {
0..=8 if r % 40 == 0 => 2000 + (r >> 8) as u32 % 6,
0..=8 if r % 13 == 0 => 0,
0..=8 => 1,
9..=10 => 10,
11 => 9,
_ => 0,
});
}
let mut surface = Vec::with_capacity(4096);
while surface.len() < 4096 {
let r = rng(&mut state);
let run = 1 + (r % 8) as usize;
let v = 100 + ((r >> 8) % 24) as u32 * 37;
surface.extend(std::iter::repeat_n(v, run.min(4096 - surface.len())));
}
let global: Vec<u32> = (0..4096)
.map(|_| (rng(&mut state) % 600) as u32 * 41)
.collect();
let large: Vec<u32> = (0..4096)
.map(|i| {
if i % 3 == 0 {
70_000 + (rng(&mut state) % 40) as u32
} else {
i as u32 % 20
}
})
.collect();
let biomes: Vec<u32> = (0..64).map(|i| [3, 3, 3, 7, 7, 12][i % 6]).collect();
vec![
("terrain", terrain),
("surface", surface),
("global", global),
("large ids", large),
("biomes", biomes),
("single", vec![5; 4096]),
("seventeen", (0..4096).map(|i| (i % 17) as u32).collect()),
]
}
#[test]
fn palette_ids_match_first_occurrence_order() {
for (name, values) in palette_cases() {
assert_eq!(
palette_ids(&values),
baseline_palette_ids(&values),
"{name}"
);
// The table is left clean for the next section.
assert_eq!(
palette_ids(&values),
baseline_palette_ids(&values),
"{name} again"
);
}
DENSE_IDS.with(|t| assert!(t.borrow().iter().all(|&slot| slot == 0)));
}
/// `palette_ids` against the pre-optimization implementation over the
/// cases above. 25 measured rounds alternate order and report the median
/// microseconds per call.
#[test]
#[ignore = "microbenchmark; run with --ignored --nocapture"]
fn benchmark_palette_ids() {
use std::{hint::black_box, time::Instant};
for (name, values) in palette_cases() {
let mut times = [Vec::new(), Vec::new()];
for round in 0..25 {
for optimized in [round % 2 == 0, round % 2 != 0] {
let started = Instant::now();
for _ in 0..500 {
if optimized {
black_box(palette_ids(black_box(&values)));
} else {
black_box(baseline_palette_ids(black_box(&values)));
}
}
if round > 0 {
times[usize::from(optimized)]
.push(started.elapsed().as_secs_f64() * 1e6 / 500.0);
}
}
}
for t in &mut times {
t.sort_by(f64::total_cmp);
}
let (base, new) = (times[0][times[0].len() / 2], times[1][times[1].len() / 2]);
println!(
"palette_ids {name}: baseline {base:.2} us, new {new:.2} us, {:.2}x",
base / new
);
}
}
}