PalettedContainer: look up a word's filled bits, not divide for them
`locate` found an entry's word by multiplication (the MAGIC table) but still divided `64 / bits` at run time for the shift, on every block read and write of a section stored in 5, 6 or 7 bits. A division by a value the compiler does not know costs tens of cycles on x86: `perf annotate` of a 10-bot server on the ThinkPad put 12% of `PalettedContainer::get`, the server thread's top function, on that `div`. The table now also holds the bits a word's entries fill, so the shift is `i * bits - word * filled`. A test checks `locate` against the division for every width and entry. ThinkPad (i5-7200U), 4M random reads, 5-7 bit palettes: 28.5-36.9 -> 24.6-32.4 ns over 2,000 sections, 6.7-7.1 -> 5.4-5.8 ns within one; 4 and 8 bits (shifts, unchanged) the same. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -106,14 +106,16 @@ enum Palette {
|
||||
List(Vec<u32>),
|
||||
Global,
|
||||
}
|
||||
/// `n / per_word` for `n < 4096` by multiplication: `MAGIC[bits] = ceil(2^20 /
|
||||
/// (64 / bits))` is exact while `n * (64 / bits) < 2^20`.
|
||||
const MAGIC: [u32; 33] = {
|
||||
let mut table = [0; 33];
|
||||
/// `n / per_word` for `n < 4096` by multiplication: `MAGIC[bits].0 = ceil(2^20 /
|
||||
/// (64 / bits))` is exact while `n * (64 / bits) < 2^20`. `.1` is the bits a
|
||||
/// word's entries fill (`64 / bits * bits`): looked up, not divided for, as a
|
||||
/// division by a run-time value costs tens of cycles on x86.
|
||||
const MAGIC: [(u32, u32); 33] = {
|
||||
let mut table = [(0, 0); 33];
|
||||
let mut bits = 1;
|
||||
while bits <= 32 {
|
||||
let per = 64 / bits as u32;
|
||||
table[bits] = (1u32 << 20).div_ceil(per);
|
||||
table[bits] = ((1u32 << 20).div_ceil(per), per * bits as u32);
|
||||
bits += 1;
|
||||
}
|
||||
table
|
||||
@@ -134,9 +136,9 @@ fn locate(bits: u8, i: usize) -> (usize, u32) {
|
||||
let word = i >> (6 - log);
|
||||
(word, ((i & ((64 >> log) - 1)) as u32) << log)
|
||||
} else {
|
||||
let per = 64 / u32::from(bits);
|
||||
let word = (i as u32 * MAGIC[usize::from(bits)]) >> 20;
|
||||
(word as usize, (i as u32 - word * per) * u32::from(bits))
|
||||
let (magic, filled) = MAGIC[usize::from(bits)];
|
||||
let word = (i as u32 * magic) >> 20;
|
||||
(word as usize, i as u32 * u32::from(bits) - word * filled)
|
||||
}
|
||||
}
|
||||
#[inline]
|
||||
@@ -720,6 +722,17 @@ mod tests {
|
||||
use super::*;
|
||||
const BLOCKS: Strategy = Strategy::blocks(16);
|
||||
const BIOMES: Strategy = Strategy::biomes(7);
|
||||
|
||||
#[test]
|
||||
fn locate_matches_division_for_every_width() {
|
||||
for bits in 1..=32u8 {
|
||||
let per = 64 / usize::from(bits);
|
||||
for i in 0..4096 {
|
||||
let expected = (i / per, ((i % per) * usize::from(bits)) as u32);
|
||||
assert_eq!(locate(bits, i), expected, "{bits} bits, entry {i}");
|
||||
}
|
||||
}
|
||||
}
|
||||
fn rng(state: &mut u64) -> u64 {
|
||||
*state ^= *state << 13;
|
||||
*state ^= *state >> 7;
|
||||
|
||||
Reference in New Issue
Block a user