Cache random-tick section eligibility for unchanged chunks

This commit is contained in:
rdbtCVS
2026-10-15 10:37:46 +01:00
parent 89b58991bb
commit 0c161a0336
6 changed files with 577 additions and 13 deletions
@@ -0,0 +1,149 @@
//! Selection cost only (not block simulation). Run with prepared client resources.
//! cargo run --profile fast -p horizon-client --example random_tick_eligibility_bench -- RESOURCES
use horizon_client::{
blocks::BlockFacts,
chunk::{Chunk, MIN_Y, Section},
fast_hash::FxHashMap,
random_tick_sections::RandomTickSections,
};
use horizon_world::registry::BlockStateId;
use std::{hint::black_box, path::Path, sync::Arc, time::Instant};
fn select_baseline(chunks: &FxHashMap<(i32, i32), Arc<Chunk>>, facts: &BlockFacts) -> usize {
let mut ticking = Vec::new();
for (&(x, z), chunk) in chunks {
if x.abs() > 8 || z.abs() > 8 || !chunk.lit {
continue;
}
let sections: Vec<i32> = chunk
.sections
.iter()
.enumerate()
.filter(|(_, s)| {
s.blocks
.maybe_has(|id| facts.get(BlockStateId(id)).random_ticks)
})
.map(|(i, _)| MIN_Y + i as i32 * 16)
.collect();
ticking.push((x, z, sections));
}
black_box(ticking).iter().map(|(_, _, s)| s.len()).sum()
}
fn select_cached(
chunks: &FxHashMap<(i32, i32), Arc<Chunk>>,
facts: &BlockFacts,
cache: &mut RandomTickSections,
) -> usize {
let mut ticking = Vec::new();
for (&(x, z), chunk) in chunks {
if x.abs() > 8 || z.abs() > 8 || !chunk.lit {
continue;
}
ticking.push((x, z, cache.get((x, z), chunk, facts)));
}
black_box(ticking).iter().map(|(_, _, s)| s.len()).sum()
}
fn main() -> horizon_client::Result<()> {
let root = std::env::args()
.nth(1)
.ok_or("expected prepared client resource directory")?;
let start = Instant::now();
let registry = horizon_world::level::data::load_registry(Path::new(&root))?;
let facts = BlockFacts::load(Path::new(&root), &registry)?;
println!(
"resource_load_ms={:.3}",
start.elapsed().as_secs_f64() * 1000.0
);
let inactive: Vec<u16> = facts
.states
.iter()
.enumerate()
.filter(|(_, f)| !f.random_ticks)
.take(128)
.map(|(i, _)| i as u16)
.collect();
let active = registry.default_state("minecraft:grass_block").unwrap().0 as u16;
assert!(facts.get(BlockStateId(u32::from(active))).random_ticks);
for (name, palette) in [("sparse", 1), ("dense", 128)] {
let start = Instant::now();
let mut chunks = FxHashMap::default();
for z in -8..=8 {
for x in -8..=8 {
let mut chunk = Chunk::empty(x, z);
chunk.lit = true;
for (i, section) in chunk.sections.iter_mut().enumerate() {
let mut values = std::array::from_fn(|j| inactive[j % palette]);
if i % 3 == 0 {
values[4095] = active;
}
Arc::make_mut(section).blocks = Section::from_blocks(&values);
}
chunks.insert((x, z), Arc::new(chunk));
}
}
println!(
"{name} fixture_ms={:.3}",
start.elapsed().as_secs_f64() * 1000.0
);
for (workload, changed) in [
("steady", 0),
("cold", 0),
("reload", 0),
("edit_1pct", 3),
("edit_all", 289),
] {
let keys: Vec<_> = chunks.keys().copied().collect();
let mut baseline = Vec::new();
let mut candidate = Vec::new();
let mut cache = RandomTickSections::default();
for sample in 0..8 {
// Alternate order; first pair warms both paths and is discarded.
for cached in if sample % 2 == 0 {
[false, true]
} else {
[true, false]
} {
let start = Instant::now();
for tick in 0..300 {
if cached && workload == "cold" {
cache = RandomTickSections::default();
}
if cached && workload == "reload" {
cache.clear();
}
for key in keys.iter().take(changed) {
Arc::make_mut(chunks.get_mut(key).unwrap()).set(
0,
MIN_Y,
0,
inactive[tick % 2],
);
}
black_box(if cached {
select_cached(&chunks, &facts, &mut cache)
} else {
select_baseline(&chunks, &facts)
});
}
// Include teardown, and remove weak handles before baseline edits.
if cached {
cache = RandomTickSections::default();
}
if sample > 0 {
let us = start.elapsed().as_secs_f64() * 1e6 / 300.0;
if cached {
candidate.push(us);
} else {
baseline.push(us);
}
}
}
}
println!("{name} {workload} baseline_us={baseline:.3?} candidate_us={candidate:.3?}");
}
}
println!("chunk_wrapper_bytes={}", std::mem::size_of::<Chunk>());
Ok(())
}
+7 -13
View File
@@ -838,6 +838,7 @@ struct Game<'a> {
end: end::EndState,
block_ticks: Ticks,
tick_chunks: HashSet<(i32, i32)>,
random_tick_sections: horizon_client::random_tick_sections::RandomTickSections,
level_events: Vec<LevelEvent>,
falling: Vec<FallingBlock>,
paintings: Vec<horizon_client::entities::Painting>,
@@ -1554,6 +1555,7 @@ impl Game<'_> {
self.register_chunk_ticks(*k);
}
for k in self.tick_chunks.difference(&loaded) {
self.random_tick_sections.remove(k);
self.block_ticks
.remove_chunk(horizon_core::position::ChunkPos::new(k.0, k.1));
}
@@ -1577,19 +1579,10 @@ impl Game<'_> {
if (x - pcx).abs() > simulation || (z - pcz).abs() > simulation || !chunk.lit {
continue;
}
// LevelChunkSection.isRandomlyTicking: a randomly ticking block or
// fluid state (lava reports both through LiquidBlock). The palette
// stands in for vanilla's exact counts.
let sections: Vec<i32> = chunk
.sections
.iter()
.enumerate()
.filter(|(_, s)| {
s.blocks
.maybe_has(|id| facts.get(BlockStateId(id)).random_ticks)
})
.map(|(i, _)| MIN_Y + i as i32 * 16)
.collect();
// Capture eligibility before inside-block and scheduled updates, as
// before. Keep the world map's chunk order, including empty chunks
// (tick_chunk_random still draws for precipitation in those).
let sections = self.random_tick_sections.get((x, z), chunk, facts);
ticking.push((x, z, sections));
}
// Entity.checkInsideBlocks for the plates, tripwires and detector rails
@@ -9344,6 +9337,7 @@ pub fn run() -> Result<()> {
end: Default::default(),
block_ticks: Ticks::new(0),
tick_chunks: HashSet::new(),
random_tick_sections: Default::default(),
level_events: Vec::new(),
falling: Vec::new(),
paintings: Vec::new(),
+1
View File
@@ -123,6 +123,7 @@ impl Game<'_> {
visibility,
)?);
// Everything loaded: switch over.
self.random_tick_sections.clear();
self.options.resources = root.to_path_buf();
let context = std::sync::Arc::new(horizon_client::streaming::WorkerContext {
assets: assets.clone(),
+1
View File
@@ -117,6 +117,7 @@ pub mod post;
pub mod projectiles;
pub mod raid;
pub mod random_sequences;
pub mod random_tick_sections;
pub mod recipe_book;
pub mod recipe_book_screen;
pub mod reconnect;
@@ -0,0 +1,353 @@
//! Memoized palette eligibility for the game thread's random-tick snapshot.
use crate::{
blocks::BlockFacts,
chunk::{Chunk, MIN_Y, SECTIONS},
fast_hash::FxHashMap,
};
use horizon_world::registry::BlockStateId;
use std::sync::{Arc, Weak};
/// A value snapshot, in ascending section order, with no per-chunk allocation.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Sections(u32);
impl Iterator for Sections {
type Item = i32;
fn next(&mut self) -> Option<i32> {
if self.0 == 0 {
return None;
}
let index = self.0.trailing_zeros();
self.0 &= self.0 - 1;
Some(MIN_Y + index as i32 * 16)
}
fn size_hint(&self) -> (usize, Option<usize>) {
let len = self.0.count_ones() as usize;
(len, Some(len))
}
}
impl ExactSizeIterator for Sections {}
struct Cached {
chunk: Weak<Chunk>,
sections: Sections,
}
/// Resident chunks are immutable behind `Arc`; edits use `Arc::make_mut`.
/// A weak reference detects both edits and replacement, without retaining block
/// data or forcing a deep copy on the next edit. It also prevents address reuse.
/// Clear this cache whenever the gameplay facts change (including resource reload).
#[derive(Default)]
pub struct RandomTickSections {
chunks: FxHashMap<(i32, i32), Cached>,
}
impl RandomTickSections {
pub fn clear(&mut self) {
self.chunks.clear();
}
pub fn remove(&mut self, key: &(i32, i32)) {
self.chunks.remove(key);
}
pub fn get(&mut self, key: (i32, i32), chunk: &Arc<Chunk>, facts: &BlockFacts) -> Sections {
self.get_with(key, chunk, |id| facts.get(BlockStateId(id)).random_ticks)
}
fn get_with(
&mut self,
key: (i32, i32),
chunk: &Arc<Chunk>,
mut random: impl FnMut(u32) -> bool,
) -> Sections {
const {
assert!(SECTIONS <= u32::BITS as usize);
}
let cached = self.chunks.entry(key).or_insert_with(|| Cached {
chunk: Weak::new(),
sections: Sections::default(),
});
if cached.chunk.as_ptr() != Arc::as_ptr(chunk) {
let mut mask = 0;
for (i, section) in chunk.sections.iter().enumerate() {
// Preserve maybe_has, including unused entries and global palettes.
if section.blocks.maybe_has(&mut random) {
mask |= 1 << i;
}
}
cached.sections = Sections(mask);
cached.chunk = Arc::downgrade(chunk);
}
cached.sections
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::chunk::Section;
#[test]
fn edits_replacements_compaction_and_reload() {
let mut cache = RandomTickSections::default();
let mut chunk = Arc::new(Chunk::empty(0, 0));
assert_eq!(cache.get_with((0, 0), &chunk, |id| id == 1).len(), 0);
cache.get_with((0, 0), &chunk, |_| panic!("unchanged chunk rescanned"));
let worker = chunk.clone();
Arc::make_mut(&mut chunk).set(0, MIN_Y, 0, 1);
assert_eq!(
cache
.get_with((0, 0), &chunk, |id| id == 1)
.collect::<Vec<_>>(),
[MIN_Y]
);
assert_eq!(worker.get(0, MIN_Y, 0), 0);
// Even after the last ticking block is removed, its unused palette entry counts.
Arc::make_mut(&mut chunk).set(0, MIN_Y, 0, 0);
assert_eq!(cache.get_with((0, 0), &chunk, |id| id == 1).len(), 1);
Arc::make_mut(&mut Arc::make_mut(&mut chunk).sections[0]).compact();
assert_eq!(cache.get_with((0, 0), &chunk, |id| id == 1).len(), 0);
Arc::make_mut(&mut chunk).sections[SECTIONS - 1] = Arc::new(Section::default());
cache.clear(); // resource reload, with changed facts for the same state IDs
assert_eq!(cache.get_with((0, 0), &chunk, |id| id == 0).len(), SECTIONS);
cache.remove(&(0, 0));
assert!(cache.chunks.is_empty());
}
#[test]
fn replacement_at_same_coordinates_and_early_snapshot() {
let mut cache = RandomTickSections::default();
let mut chunk = Arc::new(Chunk::empty(-2, 3));
let before_scheduled = cache.get_with((-2, 3), &chunk, |id| id == 1);
Arc::make_mut(&mut chunk).set(0, MIN_Y + 16, 0, 1);
assert_eq!(before_scheduled.len(), 0);
assert_eq!(
cache
.get_with((-2, 3), &chunk, |id| id == 1)
.collect::<Vec<_>>(),
[MIN_Y + 16]
);
let weak = Arc::downgrade(&chunk);
drop(chunk);
assert!(weak.upgrade().is_none()); // cache does not retain unloaded block data
let replacement = Arc::new(Chunk::empty(-2, 3));
assert_eq!(cache.get_with((-2, 3), &replacement, |id| id == 1).len(), 0);
}
#[test]
fn global_palette_and_ascending_order() {
let mut chunk = Chunk::empty(0, 0);
for i in [0, 9, SECTIONS - 1] {
Arc::make_mut(&mut chunk.sections[i]).blocks =
Section::from_blocks(&std::array::from_fn(|j| (j % 300) as u16));
}
let chunk = Arc::new(chunk);
let mut cache = RandomTickSections::default();
// Global maybe_has considers the entire ID range, including absent states.
assert_eq!(
cache
.get_with((0, 0), &chunk, |id| id == 40000)
.collect::<Vec<_>>(),
[MIN_Y, MIN_Y + 9 * 16, MIN_Y + (SECTIONS as i32 - 1) * 16]
);
}
}
#[cfg(test)]
mod tick_parity {
use super::*;
use horizon_core::{
position::{BlockPos, ChunkPos},
random::{LegacyRandom, RandomSource},
};
use horizon_world::level::{Behaviours, LevelAccess, LevelEvent, Ticks, data};
use std::{cell::RefCell, path::PathBuf};
#[derive(Clone)]
struct Level {
chunks: FxHashMap<(i32, i32), Arc<Chunk>>,
random: LegacyRandom,
reads: RefCell<Vec<BlockPos>>,
events: Vec<LevelEvent>,
time: i64,
speed: i32,
}
impl LevelAccess for Level {
fn block_state(&self, pos: BlockPos) -> BlockStateId {
self.reads.borrow_mut().push(pos);
self.chunks
.get(&(pos.x >> 4, pos.z >> 4))
.map_or(BlockStateId(0), |c| c.block(pos.x, pos.y, pos.z))
}
fn set_raw(&mut self, pos: BlockPos, state: BlockStateId) -> Option<BlockStateId> {
let chunk = self.chunks.get_mut(&(pos.x >> 4, pos.z >> 4))?;
Some(BlockStateId(u32::from(Arc::make_mut(chunk).set(
pos.x,
pos.y,
pos.z,
state.0 as u16,
))))
}
fn is_loaded(&self, pos: BlockPos) -> bool {
self.chunks.contains_key(&(pos.x >> 4, pos.z >> 4))
}
fn random(&mut self) -> &mut LegacyRandom {
&mut self.random
}
fn game_time(&self) -> i64 {
self.time
}
fn game_rules(&self) -> horizon_world::level::GameRules {
horizon_world::level::GameRules {
random_tick_speed: self.speed,
..Default::default()
}
}
fn emit(&mut self, event: LevelEvent) {
self.events.push(event);
}
}
#[test]
fn actual_random_ticks_match_original_scan() {
let root = std::env::var_os("HORIZON_RESOURCES")
.map(PathBuf::from)
.unwrap_or_else(|| {
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../.local/horizon-client/26.3")
});
if !root.join("gameplay-facts.json").exists() {
eprintln!(
"skipping resource-backed random-tick parity: {}",
root.display()
);
return;
}
let registry = data::load_registry(&root).unwrap();
let mut facts = BlockFacts::load(&root, &registry).unwrap();
let behaviours = Behaviours::load(&registry, &root).unwrap();
let grass = registry.default_state("minecraft:grass_block").unwrap();
let chunk = |x| {
let mut c = Chunk::empty(x, 0);
c.lit = true;
// Every sampled block can run real behaviour and change to dirt.
for i in [4, 7] {
Arc::make_mut(&mut c.sections[i]).blocks =
crate::chunk::Section::from_blocks(&[grass.0 as u16; 4096]);
}
Arc::new(c)
};
let mut original = Level {
chunks: [
((-1, 0), chunk(-1)),
((0, 0), chunk(0)),
((1, 0), Arc::new(Chunk::empty(1, 0))),
]
.into_iter()
.collect(),
random: LegacyRandom::new(42),
reads: RefCell::default(),
events: Vec::new(),
time: 0,
speed: 3,
};
Arc::make_mut(original.chunks.get_mut(&(1, 0)).unwrap()).lit = true; // precipitation-only chunk
let mut cached = original.clone();
let mut cache = RandomTickSections::default();
let (mut a, mut b) = (Ticks::new(12345), Ticks::new(12345));
for x in -1..=1 {
a.add_empty_chunk(ChunkPos::new(x, 0)).unwrap();
b.add_empty_chunk(ChunkPos::new(x, 0)).unwrap();
}
for step in 0..128 {
for level in [&mut original, &mut cached] {
level.time = step;
level.speed = [0, 1, 3, 9][step as usize % 4];
level.reads.borrow_mut().clear();
level.events.clear();
match step {
12 => {
level.chunks.remove(&(-1, 0));
}
13 => {
level.chunks.insert((-1, 0), chunk(-1));
}
30 => {
Arc::make_mut(level.chunks.get_mut(&(0, 0)).unwrap()).sections[7] =
Arc::new(crate::chunk::Section::default());
}
40 | 41 => {
Arc::make_mut(level.chunks.get_mut(&(0, 0)).unwrap()).lit = step == 41;
}
_ => {}
}
}
if step == 12 {
cache.remove(&(-1, 0));
}
if step == 50 || step == 60 {
facts.states[grass.0 as usize].random_ticks = step == 60;
cache.clear();
}
let old: Vec<_> = original
.chunks
.iter()
.filter(|(_, c)| c.lit)
.map(|(&key, c)| {
let sections: Vec<_> = c
.sections
.iter()
.enumerate()
.filter(|(_, s)| {
s.blocks
.maybe_has(|id| facts.get(BlockStateId(id)).random_ticks)
})
.map(|(i, _)| MIN_Y + i as i32 * 16)
.collect();
(key, sections)
})
.collect();
let new: Vec<_> = cached
.chunks
.iter()
.filter(|(_, c)| c.lit)
.map(|(&key, c)| (key, cache.get(key, c, &facts)))
.collect();
assert_eq!(
old,
new.iter()
.map(|(k, s)| (*k, s.collect::<Vec<_>>()))
.collect::<Vec<_>>()
);
// Stand in for an inside-block/scheduled write AFTER eligibility capture.
if step == 20 {
for level in [&mut original, &mut cached] {
level.set_raw(BlockPos::new(0, MIN_Y + 16, 0), grass);
}
}
for ((x, z), sections) in old {
behaviours.tick_chunk_random(&mut original, &mut a, ChunkPos::new(x, z), sections);
}
for ((x, z), sections) in new {
behaviours.tick_chunk_random(&mut cached, &mut b, ChunkPos::new(x, z), sections);
}
assert_eq!(original.reads, cached.reads, "ordered reads at tick {step}");
assert_eq!(original.events, cached.events, "events at tick {step}");
assert_eq!(
a.random_tick_value, b.random_tick_value,
"position RNG at tick {step}"
);
assert_eq!(
original.random.next_long(),
cached.random.next_long(),
"level RNG at tick {step}"
);
}
let dirt = registry.default_state("minecraft:dirt").unwrap();
assert!(
original
.chunks
.values()
.any(|c| c.sections[4].blocks.maybe_has(|id| id == dirt.0))
);
for (key, c) in &original.chunks {
for (s, t) in c.sections.iter().zip(&cached.chunks[key].sections) {
assert_eq!(s.blocks, t.blocks);
}
}
}
}
+66
View File
@@ -0,0 +1,66 @@
# Random-tick eligibility benchmark
Run with the pinned prepared client resources:
```sh
flock /tmp/horizon-optimizations-20261015-heavy.lock env CARGO_BUILD_JOBS=2 \
CARGO_TARGET_DIR=/home/bob/Documents/horizon/target \
cargo run --profile fast -p horizon-client --example random_tick_eligibility_bench -- \
.local/horizon-client/26.3
```
The example keeps the original selection algorithm alongside the cache. Both run
in the same optimized binary, alternating measurement order. Each workload has
one discarded warmup pair and seven pairs of 300 ticks. It reports every sample,
resource loading and fixture construction separately. This measures eligibility
selection and writes, not whole-game tick latency or rendering.
There are 289 lit chunks (simulation distance eight), each with 24 sections.
Sparse sections have one non-ticking state; dense sections have 128 distinct
non-ticking states selected from the real prepared facts. Every third section
also contains grass at its final position. These are controlled fixtures, not a
claim about typical generated terrain.
Steady includes initial cache fill amortized over 300 ticks. Cold recreates the
cache each tick, including allocation and destruction; reload clears it each
tick while retaining capacity. Edit workloads alternate a block value in three
or all 289 chunks before every selection. Timing includes the additional
`Arc::make_mut` allocation caused by weak cache handles; the baseline drops those
handles before its turn. Both paths retain the original outer-vector allocation.
The initial scan-only baseline was measured before implementing the cache with
the same fast profile: sparse steady median 49.277 us and dense steady median
1487.781 us. Paired results below are the comparison used for the keep decision.
Measured on an AMD Ryzen 5 5600X (12 logical CPUs), Linux x86-64, rustc
1.99.0 (b940084d7), using `fast` (release optimization, LTO off, 16 codegen units).
All build/measurement commands held the shared heavy-work lock with two build
jobs. Resource loading took 1086 ms; fixture construction took 85 ms sparse and
375 ms dense, shared by both implementations.
Median microseconds per tick (seven paired samples):
| Palette | Workload | Original | Cached | Speedup |
| --- | --- | ---: | ---: | ---: |
| sparse | steady | 48.855 | 2.588 | 18.88x |
| sparse | cold | 50.319 | 39.110 | 1.29x |
| sparse | reload | 49.051 | 36.167 | 1.36x |
| sparse | edit_1pct | 49.110 | 2.850 | 17.23x |
| sparse | edit_all | 59.425 | 50.759 | 1.17x |
| dense | steady | 1311.924 | 4.397 | 298.37x |
| dense | cold | 1305.237 | 553.209 | 2.36x |
| dense | reload | 1292.879 | 542.848 | 2.38x |
| dense | edit_1pct | 1290.162 | 9.102 | 141.74x |
| dense | edit_all | 1303.115 | 561.606 | 2.32x |
Keep: steady selection improves repeatedly, and cold/refill/edit-heavy selection
also improves in this fixture. No whole-game speedup is inferred from this
microbenchmark. Real terrain's palette sizes and block-write frequency determine
the gain. Lighting and other chunk changes conservatively invalidate eligibility.
The cache adds a weak handle and mask per cached resident chunk: about 13 KiB of
hash-table storage for 289 entries on this platform, including capacity slack.
It retains no unloaded block data. A first edit after caching can allocate/move
the 176-byte chunk wrapper (plus Arc bookkeeping); it does not force a deep
copy. The edit rows include this cost. Unloads remove entries; resource reload
clears the cache. The original outer tick-vector allocation remains.