Merge pull request #509 from ludovic111/perf/poi-state-table

Villagers: each block state's POI type from a table built once (−11 % CPU at world load)
This commit is contained in:
Ludovic Marie
2026-10-16 20:00:49 +02:00
committed by GitHub
2 changed files with 22 additions and 12 deletions
+5
View File
@@ -50,6 +50,9 @@ pub struct MobData {
pub biome_tags: HashMap<String, std::collections::HashSet<String>>,
/// Entity type tags (`data/*/tags/entity_type`).
pub entity_tags: HashMap<String, std::collections::HashSet<String>>,
/// The villager POI type of every block state (`PoiTypes.forState`),
/// worked out the first time a villager looks for one.
pub pois: std::sync::OnceLock<Vec<Option<super::villager_brain::Poi>>>,
}
impl MobData {
pub fn load(root: &Path, facts: &BlockFacts) -> Self {
@@ -130,6 +133,7 @@ impl MobData {
animations,
biome_tags: HashMap::new(),
entity_tags: HashMap::new(),
pois: Default::default(),
}
}
/// Without the export, approximate path types from the gameplay facts.
@@ -184,6 +188,7 @@ impl MobData {
animations: HashMap::new(),
biome_tags: HashMap::new(),
entity_tags: HashMap::new(),
pois: Default::default(),
}
}
pub fn state(&self, id: horizon_world::registry::BlockStateId) -> StateData {
@@ -17,7 +17,6 @@ use horizon_core::{
geometry::Vec3,
random::{LegacyRandom, RandomSource},
};
use std::collections::HashMap;
/// `MemorySlot.NEVER_EXPIRE`.
pub(super) const NEVER: i64 = i64::MAX;
@@ -846,8 +845,20 @@ impl Poi {
pub fn poi_at(w: &MobWorld, x: i32, y: i32, z: i32) -> Option<Poi> {
poi_of_state(w, w.world.block(x, y, z))
}
/// The POI a block state is, if any (`PoiTypes.forState`).
/// The POI a block state is, if any (`PoiTypes.forState`), from the table
/// of every state built on first use.
fn poi_of_state(w: &MobWorld, state: horizon_world::registry::BlockStateId) -> Option<Poi> {
let table = w.data.pois.get_or_init(|| {
(0..w.registry.len())
.map(|i| state_poi(w, horizon_world::registry::BlockStateId(i as u32)))
.collect()
});
match table.get(state.0 as usize) {
Some(poi) => *poi,
None => state_poi(w, state),
}
}
fn state_poi(w: &MobWorld, state: horizon_world::registry::BlockStateId) -> Option<Poi> {
let name = &*w.facts.get(state).name;
if name == "minecraft:bell" {
return Some(Poi::Meeting);
@@ -929,16 +940,10 @@ fn pois_in_range(
let chunks = radius.div_euclid(16) + 1;
let y0 = (c[1] - radius).max(crate::chunk::MIN_Y);
let y1 = (c[1] + radius).min(crate::chunk::MIN_Y + crate::chunk::HEIGHT - 1);
// The POI each block state is, worked out once per search, and the
// sections whose palette holds none skipped whole, as PoiManager only
// visits sections with records: the same positions in the same order,
// without reading and naming every block of the sphere.
let mut kinds: HashMap<u32, Option<Poi>> = HashMap::new();
let mut kind = |state: u32| {
*kinds
.entry(state)
.or_insert_with(|| poi_of_state(w, horizon_world::registry::BlockStateId(state)))
};
// Sections whose palette holds no POI state are skipped whole, as
// PoiManager only visits sections with records: the same positions in
// the same order, without reading and naming every block of the sphere.
let kind = |state: u32| poi_of_state(w, horizon_world::registry::BlockStateId(state));
for qz in cz - chunks..=cz + chunks {
for qx in cx - chunks..=cx + chunks {
let Some(chunk) = w.world.chunks.get(&(qx, qz)) else {