Random ticks: ask for the next ticking section, and read a section's blocks faster
tick_chunk_random asks for the lowest randomly ticking section from a height up, instead of asking about every section: a chunk costs one question per section that ticks, not 24. PalettedContainer::has tests whole words for the few matching palette indices (a field-wise zero test) instead of reading every entry, so a section read again after an edit costs a few hundred word operations. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -99,7 +99,7 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||
&mut level,
|
||||
&mut ticks,
|
||||
ChunkPos::new(0, 0),
|
||||
|_, min_y| cache.get((0, 0), &chunk, &facts).contains(min_y),
|
||||
|_, from| cache.get((0, 0), &chunk, &facts).from(from),
|
||||
);
|
||||
for pos in level.reads.borrow().iter() {
|
||||
let state = chunk.block(pos.x, pos.y, pos.z);
|
||||
|
||||
@@ -2427,7 +2427,7 @@ impl Game<'_> {
|
||||
let mut read = None;
|
||||
let mut sections = Default::default();
|
||||
let pos = horizon_core::position::ChunkPos::new(x, z);
|
||||
b.tick_chunk_random(level, ticks, pos, |level, min_y| {
|
||||
b.tick_chunk_random(level, ticks, pos, |level, from| {
|
||||
if read != Some(level.world.revision) {
|
||||
read = Some(level.world.revision);
|
||||
sections = level
|
||||
@@ -2438,7 +2438,7 @@ impl Game<'_> {
|
||||
level.random_tick_sections.get((x, z), chunk, level.facts)
|
||||
});
|
||||
}
|
||||
sections.contains(min_y)
|
||||
sections.from(from)
|
||||
});
|
||||
}
|
||||
b.run_block_events(level, ticks);
|
||||
|
||||
@@ -12,10 +12,10 @@ use std::sync::{Arc, Weak};
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||
pub struct Sections(u32);
|
||||
impl Sections {
|
||||
/// Whether the section starting at block `min_y` is in the set.
|
||||
pub fn contains(self, min_y: i32) -> bool {
|
||||
let index = (min_y - MIN_Y) >> 4;
|
||||
(0..SECTIONS as i32).contains(&index) && self.0 >> index & 1 != 0
|
||||
/// The minimum Y of the lowest section in the set from block `min_y` up.
|
||||
pub fn from(self, min_y: i32) -> Option<i32> {
|
||||
let index = ((min_y - MIN_Y) >> 4).max(0) as u32;
|
||||
Sections(self.0 & u32::MAX.checked_shl(index).unwrap_or(0)).next()
|
||||
}
|
||||
}
|
||||
impl Iterator for Sections {
|
||||
@@ -184,6 +184,27 @@ mod tests {
|
||||
.collect::<Vec<_>>(),
|
||||
[MIN_Y, MIN_Y + 9 * 16, MIN_Y + (SECTIONS as i32 - 1) * 16]
|
||||
);
|
||||
let sections = cache.get_with((0, 0), &chunk, |id| id == 299);
|
||||
let top = MIN_Y + (SECTIONS as i32 - 1) * 16;
|
||||
let from = |y| sections.from(y);
|
||||
assert_eq!(
|
||||
[
|
||||
from(MIN_Y - 16),
|
||||
from(MIN_Y),
|
||||
from(MIN_Y + 16),
|
||||
from(MIN_Y + 9 * 16)
|
||||
],
|
||||
[
|
||||
Some(MIN_Y),
|
||||
Some(MIN_Y),
|
||||
Some(MIN_Y + 9 * 16),
|
||||
Some(MIN_Y + 9 * 16)
|
||||
]
|
||||
);
|
||||
assert_eq!(
|
||||
[from(top), from(top + 16), from(top + 1000)],
|
||||
[Some(top), None, None]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -359,19 +380,21 @@ mod tick_parity {
|
||||
for (x, z) in lit(&original) {
|
||||
// Vanilla's rule, asked as each section is reached: any of its
|
||||
// 4,096 blocks ticks randomly.
|
||||
let reference = |level: &mut Level, min_y: i32| {
|
||||
let reference = |level: &mut Level, from: i32| {
|
||||
let chunk = &level.chunks[&(x, z)];
|
||||
(0..16).any(|y| {
|
||||
(0..16).any(|z| (0..16).any(|x| random(chunk.get(x, min_y + y, z))))
|
||||
})
|
||||
(from..MIN_Y + SECTIONS as i32 * 16)
|
||||
.step_by(16)
|
||||
.find(|&min_y| {
|
||||
(0..16).any(|y| {
|
||||
(0..16).any(|z| (0..16).any(|x| random(chunk.get(x, min_y + y, z))))
|
||||
})
|
||||
})
|
||||
};
|
||||
behaviours.tick_chunk_random(&mut original, &mut a, ChunkPos::new(x, z), reference);
|
||||
}
|
||||
for (x, z) in lit(&cached) {
|
||||
let memoized = |level: &mut Level, min_y| {
|
||||
cache
|
||||
.get((x, z), &level.chunks[&(x, z)], &facts)
|
||||
.contains(min_y)
|
||||
let memoized = |level: &mut Level, from| {
|
||||
cache.get((x, z), &level.chunks[&(x, z)], &facts).from(from)
|
||||
};
|
||||
behaviours.tick_chunk_random(&mut cached, &mut b, ChunkPos::new(x, z), memoized);
|
||||
}
|
||||
|
||||
@@ -677,37 +677,43 @@ impl PalettedContainer {
|
||||
/// Whether any entry satisfies `predicate`. Unlike [`Self::maybe_has`],
|
||||
/// palette entries no entry uses any more don't count.
|
||||
pub fn has(&self, mut predicate: impl FnMut(u32) -> bool) -> bool {
|
||||
let matching: Vec<bool> = match &self.palette {
|
||||
let list = match &self.palette {
|
||||
Palette::Single(value) => return predicate(*value),
|
||||
Palette::List(list) => list.iter().map(|&v| predicate(v)).collect(),
|
||||
Palette::Global => Vec::new(),
|
||||
Palette::List(list) => list,
|
||||
Palette::Global => {
|
||||
return (0..self.strategy.entries()).any(|i| predicate(self.raw(i) as u32));
|
||||
}
|
||||
};
|
||||
if matches!(self.palette, Palette::List(_)) {
|
||||
if !matching.contains(&true) {
|
||||
return false;
|
||||
}
|
||||
if !matching.contains(&false) {
|
||||
return true;
|
||||
}
|
||||
let matching: Vec<bool> = list.iter().map(|&v| predicate(v)).collect();
|
||||
let count = matching.iter().filter(|&&m| m).count();
|
||||
if count == 0 || count == list.len() {
|
||||
return count != 0;
|
||||
}
|
||||
let bits = usize::from(self.bits);
|
||||
let (bits, entries) = (usize::from(self.bits), self.strategy.entries());
|
||||
let per = 64 / bits;
|
||||
let m = mask(self.bits);
|
||||
let mut left = self.strategy.entries();
|
||||
for &word in &self.words {
|
||||
for slot in 0..per.min(left) {
|
||||
let id = ((word >> (slot * bits)) & m) as usize;
|
||||
let hit = match self.palette {
|
||||
Palette::Global => predicate(id as u32),
|
||||
_ => matching[id],
|
||||
};
|
||||
if hit {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
left = left.saturating_sub(per);
|
||||
// The words whose fields all hold entries; the last may be partial.
|
||||
let full = entries / per;
|
||||
if count <= 4 {
|
||||
// A field-wise zero test of each word XORed with the matching
|
||||
// index in every field: `(v - lo) & !v & hi` is non-zero exactly
|
||||
// when some field of `v` is zero.
|
||||
let lo = (0..per).fold(0u64, |a, f| a | 1 << (f * bits));
|
||||
let hi = lo << (bits - 1);
|
||||
let words = &self.words[..full];
|
||||
let found = matching
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(_, m)| **m)
|
||||
.any(|(i, _)| {
|
||||
let pattern = lo * i as u64;
|
||||
words.iter().any(|&w| {
|
||||
let v = w ^ pattern;
|
||||
v.wrapping_sub(lo) & !v & hi != 0
|
||||
})
|
||||
});
|
||||
return found || (full * per..entries).any(|i| matching[self.raw(i)]);
|
||||
}
|
||||
false
|
||||
(0..entries).any(|i| matching[self.raw(i)])
|
||||
}
|
||||
/// Heap bytes held by the palette and the storage.
|
||||
pub fn heap_bytes(&self) -> usize {
|
||||
@@ -816,6 +822,53 @@ mod tests {
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn has_matches_a_scan_at_every_width() {
|
||||
let mut state = 0x9E37_79B9_7F4A_7C15u64;
|
||||
for (strategy, distinct) in [
|
||||
(BLOCKS, 2u32),
|
||||
(BLOCKS, 3),
|
||||
(BLOCKS, 17),
|
||||
(BLOCKS, 33),
|
||||
(BLOCKS, 65),
|
||||
(BLOCKS, 129),
|
||||
(BIOMES, 2),
|
||||
(BIOMES, 3),
|
||||
(BIOMES, 5),
|
||||
] {
|
||||
let entries = strategy.entries();
|
||||
let mut c = PalettedContainer::new(strategy, 0);
|
||||
let mut plain = vec![0u32; entries];
|
||||
for i in 0..entries {
|
||||
let v = (rng(&mut state) % u64::from(distinct)) as u32;
|
||||
c.set(i, v);
|
||||
plain[i] = v;
|
||||
}
|
||||
// Take some values away again (stale entries), the last index too.
|
||||
for gone in [0, 1, distinct - 1] {
|
||||
for i in 0..entries {
|
||||
if plain[i] == gone {
|
||||
c.set(i, distinct);
|
||||
plain[i] = distinct;
|
||||
}
|
||||
}
|
||||
for m in [3, 5, 7] {
|
||||
let hit = |v: u32| v % m == gone % m;
|
||||
assert_eq!(c.has(hit), plain.iter().any(|&v| hit(v)));
|
||||
}
|
||||
for wanted in 0..=distinct {
|
||||
for other in [wanted, wanted + 1, wanted + 2, wanted + 3, wanted + 4] {
|
||||
let hit = |v: u32| v == wanted || v == other;
|
||||
assert_eq!(
|
||||
c.has(hit),
|
||||
plain.iter().any(|&v| hit(v)),
|
||||
"{distinct} values, {wanted}/{other}, without {gone}"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn has_ignores_stale_palette_entries() {
|
||||
let mut c = PalettedContainer::new(BLOCKS, 0);
|
||||
assert!(c.has(|v| v == 0) && !c.has(|v| v == 3));
|
||||
|
||||
@@ -69,7 +69,7 @@
|
||||
//! level.time += 1;
|
||||
//! behaviours.tick_scheduled(&mut level, &mut ticks);
|
||||
//! for chunk in ticking_chunks {
|
||||
//! behaviours.tick_chunk_random(&mut level, &mut ticks, chunk, |level, min_y| level.randomly_ticking(chunk, min_y));
|
||||
//! behaviours.tick_chunk_random(&mut level, &mut ticks, chunk, |level, from| level.next_ticking_section(chunk, from));
|
||||
//! }
|
||||
//! behaviours.run_block_events(&mut level, &mut ticks);
|
||||
//! /* tick entities */
|
||||
|
||||
@@ -1617,16 +1617,17 @@ impl Behaviours {
|
||||
}
|
||||
/// Random-tick one chunk (`ServerLevel.tickChunk` block part). Visits the
|
||||
/// sections from the bottom up and draws `randomTickSpeed` positions in
|
||||
/// each one `ticking(level, min_y)` reports randomly ticking
|
||||
/// (`LevelChunkSection.isRandomlyTicking`: it holds a randomly ticking
|
||||
/// block or fluid). Vanilla asks as it reaches each section, so the ticks
|
||||
/// of the sections below count.
|
||||
/// each randomly ticking one (`LevelChunkSection.isRandomlyTicking`: it
|
||||
/// holds a randomly ticking block or fluid). `next_ticking(level, min_y)`
|
||||
/// is the minimum Y of the lowest such section from `min_y` up, as the
|
||||
/// chunk is now: vanilla asks as it reaches each section, so the ticks of
|
||||
/// the sections below count.
|
||||
pub fn tick_chunk_random<L: LevelAccess + ?Sized>(
|
||||
&self,
|
||||
level: &mut L,
|
||||
ticks: &mut Ticks,
|
||||
chunk: ChunkPos,
|
||||
mut ticking: impl FnMut(&mut L, i32) -> bool,
|
||||
mut next_ticking: impl FnMut(&mut L, i32) -> Option<i32>,
|
||||
) {
|
||||
let speed = level.game_rules().random_tick_speed;
|
||||
if speed <= 0 {
|
||||
@@ -1641,14 +1642,13 @@ impl Behaviours {
|
||||
}
|
||||
}
|
||||
}
|
||||
for min_y in (level.min_y()..level.max_y()).step_by(16) {
|
||||
if !ticking(level, min_y) {
|
||||
continue;
|
||||
}
|
||||
let mut from = level.min_y();
|
||||
while let Some(min_y) = next_ticking(level, from).filter(|&y| y < level.max_y()) {
|
||||
for _ in 0..speed {
|
||||
let pos = ticks.block_random_pos(min_x, min_y, min_z, 15);
|
||||
self.sim(level, ticks).random_tick_at(pos);
|
||||
}
|
||||
from = min_y + 16;
|
||||
}
|
||||
}
|
||||
/// Random-tick a single position (block then fluid), as `tickChunk` does.
|
||||
|
||||
Reference in New Issue
Block a user