Merge pull request #508 from ludovic111/perf/mob-push-grid

Mobs: find the mobs to push through a grid, not a scan of every mob (−14 % tick time)
This commit is contained in:
Ludovic Marie
2026-10-16 20:00:42 +02:00
committed by GitHub
6 changed files with 181 additions and 6 deletions
+12 -5
View File
@@ -19,7 +19,7 @@ use horizon_client::{
},
mobs::{
Mob, MobData, MobEvent, MobTypes, MobWorld, PlayerView, Seen, Species, Who,
control::Operation, spawning::LocalDifficulty,
control::Operation, seen_grid::SeenGrid, spawning::LocalDifficulty,
},
world::World,
};
@@ -1011,6 +1011,9 @@ fn main() -> horizon_client::Result<()> {
// The MobWorld every step sees, over a snapshot of the mobs.
macro_rules! mob_world {
($snapshot:expr) => {
mob_world!($snapshot, None)
};
($snapshot:expr, $grid:expr) => {
MobWorld {
teams: Default::default(),
world: &world,
@@ -1021,6 +1024,7 @@ fn main() -> horizon_client::Result<()> {
data: &data,
player: player.clone(),
others: $snapshot,
others_grid: $grid,
daylight: (world.day_time.rem_euclid(24000)) < 12000,
game_time: world.game_time,
day_time: world.day_time,
@@ -1065,6 +1069,8 @@ fn main() -> horizon_client::Result<()> {
p.yaw = yaw;
}
let mut snapshot: Vec<Seen> = mobs.iter().filter(|m| !m.removed).map(seen).collect();
// The snapshot by position, kept as the game keeps it.
let mut grid = SeenGrid::new(snapshot.iter().map(|e| e.bounds));
let w = mob_world!(&snapshot);
let mut events = Vec::new();
let mut heart_events = Vec::new();
@@ -1130,7 +1136,7 @@ fn main() -> horizon_client::Result<()> {
if mobs[i].removed {
continue;
}
let w = mob_world!(&snapshot);
let w = mob_world!(&snapshot, Some(&grid));
let m = &mut mobs[i];
horizon_client::mobs::apply_mob_pushes(m, &mut pushes);
let from = events.len();
@@ -1246,9 +1252,10 @@ fn main() -> horizon_client::Result<()> {
}
// ServerLevel ticks entities one after another: the ones
// after this mob see it as it is now.
if let Some(e) = snapshot.iter_mut().find(|e| e.id == mobs[i].id) {
*e = seen(&mobs[i]);
e.alive &= !mobs[i].removed;
if let Some(k) = snapshot.iter().position(|e| e.id == mobs[i].id) {
snapshot[k] = seen(&mobs[i]);
snapshot[k].alive &= !mobs[i].removed;
grid.update(k, snapshot[k].bounds);
}
}
for m in &mut mobs {
@@ -385,6 +385,7 @@ impl Game<'_> {
data: &self.res.mob_data,
player: self.player_view(),
others,
others_grid: None,
daylight: self.dim != Dim::Nether && self.sky_darken() < 4,
game_time: self.world.game_time,
day_time: self.world.day_time,
@@ -528,8 +529,11 @@ impl Game<'_> {
let mut scratch = self.world.random.clone();
let mut pushes = Vec::new();
let mut keep = Vec::with_capacity(mobs.len());
let mut grid =
horizon_client::mobs::seen_grid::SeenGrid::new(snapshot.iter().map(|s| s.bounds));
for i in 0..mobs.len() {
let mut w = self.mob_world_base(&snapshot);
w.others_grid = Some(&grid);
let m = &mut mobs[i];
let seen_guest = if guests.is_empty() {
None
@@ -592,6 +596,7 @@ impl Game<'_> {
// after this mob see it as it is now.
snapshot[i] = seen(m, uuid);
snapshot[i].alive &= kept;
grid.update(i, snapshot[i].bounds);
horizon_client::mobs::apply_brain_events(&mut mobs, &mut events, from);
}
let mut flags = keep.into_iter();
+3
View File
@@ -53,6 +53,7 @@ pub mod raider;
pub mod ravager;
pub mod render;
pub mod riders;
pub mod seen_grid;
pub mod shulker;
pub mod sniffer;
pub mod spawn_rules;
@@ -1399,6 +1400,8 @@ pub struct MobWorld<'a> {
pub data: &'a MobData,
pub player: Option<PlayerView>,
pub others: &'a [Seen],
/// `others` by position, when the caller keeps one (the mob ticks).
pub others_grid: Option<&'a seen_grid::SeenGrid>,
pub daylight: bool,
pub game_time: i64,
pub day_time: i64,
+1 -1
View File
@@ -777,7 +777,7 @@ fn push_entities(m: &mut Mob, w: &MobWorld, events: &mut Vec<MobEvent>) {
{
targets.push((Who::Player, p.position));
}
for o in w.others {
for o in w.others_near(bounds) {
if o.id != m.id && o.alive && o.pushable && o.bounds.intersects(bounds) {
targets.push((Who::Mob(o.id), o.position));
}
+148
View File
@@ -0,0 +1,148 @@
//! The tick's mob snapshot indexed by where the boxes are, so a box query
//! (`LivingEntity.pushEntities`) reads the mobs near it instead of every
//! mob, and still meets them in snapshot order.
use crate::fast_hash::FxHashMap;
use horizon_core::geometry::{Aabb, Axis};
/// Width of a grid column, in blocks.
const CELL: f64 = 4.0;
/// Snapshot indices by the column holding each box's minimum corner.
#[derive(Default)]
pub struct SeenGrid {
cells: FxHashMap<(i32, i32), Vec<u32>>,
cell_of: Vec<(i32, i32)>,
/// The widest box (along x or z) the grid has held: no box reaches
/// further than this past its minimum corner.
reach: f64,
}
fn column(x: f64, z: f64) -> (i32, i32) {
((x / CELL).floor() as i32, (z / CELL).floor() as i32)
}
fn corner(b: Aabb) -> (i32, i32) {
column(b.min(Axis::X), b.min(Axis::Z))
}
fn width(b: Aabb) -> f64 {
(b.max(Axis::X) - b.min(Axis::X)).max(b.max(Axis::Z) - b.min(Axis::Z))
}
impl SeenGrid {
/// The boxes of the snapshot's entries, in order.
pub fn new(boxes: impl Iterator<Item = Aabb>) -> Self {
let mut grid = Self::default();
for (i, b) in boxes.enumerate() {
let at = corner(b);
grid.reach = grid.reach.max(width(b));
grid.cells.entry(at).or_default().push(i as u32);
grid.cell_of.push(at);
}
grid
}
/// Entry `i`'s box is now `b` (the mob moved or resized).
pub fn update(&mut self, i: usize, b: Aabb) {
self.reach = self.reach.max(width(b));
let at = corner(b);
let old = std::mem::replace(&mut self.cell_of[i], at);
if old == at {
return;
}
if let Some(list) = self.cells.get_mut(&old)
&& let Some(k) = list.iter().position(|&j| j == i as u32)
{
list.swap_remove(k);
}
self.cells.entry(at).or_default().push(i as u32);
}
/// The indices, ascending, of every entry whose box can meet `area`
/// (a superset: callers still test it). False when the area spans more
/// columns than the grid holds (or is not finite): scan them all.
pub fn candidates(&self, area: Aabb, out: &mut Vec<u32>) -> bool {
out.clear();
// A box meets `area` only if its minimum corner is below area.max
// and within `reach` (plus a margin for rounding) of area.min.
let margin = self.reach + 1.0;
let lo = column(area.min(Axis::X) - margin, area.min(Axis::Z) - margin);
let hi = column(area.max(Axis::X), area.max(Axis::Z));
let span = |a: i32, b: i32| i64::from(b) - i64::from(a) + 1;
let (nx, nz) = (span(lo.0, hi.0), span(lo.1, hi.1));
if nx < 1 || nz < 1 || nx.saturating_mul(nz) > self.cells.len() as i64 {
return false;
}
for x in lo.0..=hi.0 {
for z in lo.1..=hi.1 {
if let Some(list) = self.cells.get(&(x, z)) {
out.extend_from_slice(list);
}
}
}
out.sort_unstable();
true
}
}
#[cfg(test)]
mod tests {
use super::*;
use horizon_core::geometry::Vec3;
fn boxed(x: f64, y: f64, z: f64, w: f64) -> Aabb {
Aabb::new(Vec3::new(x, y, z), Vec3::new(x + w, y + 1.8, z + w))
}
#[test]
fn candidates_hold_every_meeting_box_in_order() {
// A small LCG keeps the test free of dependencies.
let mut seed = 0x2545_f491_4f6c_dd1du64;
let mut next = |range: f64| {
seed = seed.wrapping_mul(6_364_136_223_846_793_005).wrapping_add(1);
(seed >> 11) as f64 / (1u64 << 53) as f64 * range
};
let mut boxes: Vec<Aabb> = (0..300)
.map(|_| {
boxed(
next(64.0) - 32.0,
next(8.0),
next(64.0) - 32.0,
0.3 + next(1.2),
)
})
.collect();
let mut grid = SeenGrid::new(boxes.iter().copied());
let mut out = Vec::new();
let mut indexed = 0;
for round in 0..2000 {
// Move one box, sometimes far and wider, as mobs do between queries.
let i = round % boxes.len();
let w = if round % 97 == 0 { 3.9 } else { 0.6 };
boxes[i] = boxed(next(80.0) - 40.0, next(8.0), next(80.0) - 40.0, w);
grid.update(i, boxes[i]);
let area = boxed(next(70.0) - 35.0, next(8.0), next(70.0) - 35.0, next(2.0));
let expected: Vec<u32> = (0..boxes.len() as u32)
.filter(|&j| boxes[j as usize].intersects(area))
.collect();
if grid.candidates(area, &mut out) {
indexed += 1;
assert!(out.windows(2).all(|p| p[0] < p[1]), "ascending, no repeats");
let met: Vec<u32> = out
.iter()
.copied()
.filter(|&j| boxes[j as usize].intersects(area))
.collect();
assert_eq!(met, expected);
}
}
assert!(indexed > 1900, "the grid answered {indexed} of 2000");
}
#[test]
fn unbounded_areas_fall_back_to_a_full_scan() {
let grid = SeenGrid::new([boxed(0.0, 0.0, 0.0, 0.6)].into_iter());
let mut out = Vec::new();
let huge = Aabb::new(
Vec3::new(f64::NEG_INFINITY, 0.0, 0.0),
Vec3::new(f64::INFINITY, 1.0, 1.0),
);
assert!(!grid.candidates(huge, &mut out));
}
}
+12
View File
@@ -213,6 +213,18 @@ impl MobWorld<'_> {
}),
}
}
/// The other mobs whose boxes may meet `area` (callers test the boxes),
/// in snapshot order: through the grid when there is one, else all.
pub fn others_near(&self, area: Aabb) -> impl Iterator<Item = &Seen> {
let mut near = Vec::new();
let indexed = self
.others_grid
.is_some_and(|grid| grid.candidates(area, &mut near));
let all = (!indexed).then(|| self.others.iter());
near.into_iter()
.map(|i| &self.others[i as usize])
.chain(all.into_iter().flatten())
}
pub fn seen(&self, id: u32) -> Option<&Seen> {
self.others.iter().find(|s| s.id == id)
}