From da314fa658e9b07b0e1c8d5552a126bbeb9c7bc2 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 21 Oct 2026 19:28:55 +0000 Subject: [PATCH 01/29] Server tick: cheaper view requeue and chunk ticket levels Two costs that grow with players on the server thread: - Hub::update_view rebuilt a player's chunk queue on every chunk crossing by scanning the queue (a Vec) once for each chunk of the view. It now checks a set. Same queue, same order (differential test against the old scan). 111 -> 16 us per crossing at view 10, 6.5 ms -> 0.13 ms at 32. - ChunkTickets rebuilt a hash map of every ticketed chunk whenever any player changed chunk, and every block entity, mob and ticking chunk hashed into it. Levels are now a grid over the tickets' bounding box, with the map kept for tickets spread too far apart. At 50 players: build 0.23 -> 0.08 ms, lookups about 2x faster (differential test over 200 random ticket layouts). Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01UxXpf7d4RgpespL2G2UsGE --- crates/horizon-client/src/lan/hub.rs | 112 ++++++++-- crates/horizon-client/src/ticket_levels.rs | 232 ++++++++++++++++++++- 2 files changed, 322 insertions(+), 22 deletions(-) diff --git a/crates/horizon-client/src/lan/hub.rs b/crates/horizon-client/src/lan/hub.rs index a961d57a4..76a9551fe 100644 --- a/crates/horizon-client/src/lan/hub.rs +++ b/crates/horizon-client/src/lan/hub.rs @@ -240,6 +240,30 @@ fn tracked(cx: i32, cz: i32, r: i32, x: i32, z: i32) -> bool { dx * dx + dz * dz < i64::from(r) * i64::from(r) } +/// The view's queue around a new centre: the queued chunks still tracked, then +/// the tracked ones neither sent nor queued, closest first (a stable sort, so +/// equally close chunks keep their order). +fn requeue( + queue: &mut Vec<(i32, i32)>, + sent: &crate::fast_hash::FxHashSet<(i32, i32)>, + (cx, cz): (i32, i32), + r: i32, +) { + queue.retain(|&(x, z)| tracked(cx, cz, r, x, z)); + // A walking player changes chunk every few ticks with hundreds of chunks + // queued: a set, not a scan of the queue for each of the view's chunks. + let mut queued: crate::fast_hash::FxHashSet<(i32, i32)> = queue.iter().copied().collect(); + for dz in -r - 2..=r + 2 { + for dx in -r - 2..=r + 2 { + let key = (cx + dx, cz + dz); + if tracked(cx, cz, r, key.0, key.1) && !sent.contains(&key) && queued.insert(key) { + queue.push(key); + } + } + } + queue.sort_by_key(|&(x, z)| i64::from(x - cx).pow(2) + i64::from(z - cz).pow(2)); +} + /// What a level's players were last told of its clock and weather. #[derive(Clone, Copy)] struct LevelSync { @@ -1288,20 +1312,7 @@ impl Hub { g.sent_chunks.remove(&(x, z)); g.send(&C::ForgetLevelChunk(play::ForgetLevelChunk { pos: [x, z] })); } - g.chunk_queue.retain(|&(x, z)| tracked(cx, cz, r, x, z)); - for dz in -r - 2..=r + 2 { - for dx in -r - 2..=r + 2 { - let key = (cx + dx, cz + dz); - if tracked(cx, cz, r, key.0, key.1) - && !g.sent_chunks.contains(&key) - && !g.chunk_queue.contains(&key) - { - g.chunk_queue.push(key); - } - } - } - g.chunk_queue - .sort_by_key(|&(x, z)| i64::from(x - cx).pow(2) + i64::from(z - cz).pow(2)); + requeue(&mut g.chunk_queue, &g.sent_chunks, (cx, cz), r); } /// PlayerChunkSender.sendNextChunks' choice: ready chunks, closest first, @@ -2457,3 +2468,76 @@ fn ticking_state(rate: f32, frozen: bool) -> C { w.bool(frozen); raw(ids::TICKING_STATE, w.data) } + +#[cfg(test)] +mod tests { + use super::*; + use crate::fast_hash::FxHashSet; + + /// `requeue` as it was, scanning the queue for each chunk of the view. + fn requeue_scan( + queue: &mut Vec<(i32, i32)>, + sent: &FxHashSet<(i32, i32)>, + (cx, cz): (i32, i32), + r: i32, + ) { + queue.retain(|&(x, z)| tracked(cx, cz, r, x, z)); + for dz in -r - 2..=r + 2 { + for dx in -r - 2..=r + 2 { + let key = (cx + dx, cz + dz); + if tracked(cx, cz, r, key.0, key.1) && !sent.contains(&key) && !queue.contains(&key) + { + queue.push(key); + } + } + } + queue.sort_by_key(|&(x, z)| i64::from(x - cx).pow(2) + i64::from(z - cz).pow(2)); + } + + /// A player walking a loop, sending a few chunks a tick: the queue + /// after every move is the one the scan built. + #[test] + fn requeue_matches_the_scan() { + for r in [2, 5, 10, 12, 32] { + let (mut a, mut b) = (Vec::new(), Vec::new()); + let mut sent = FxHashSet::default(); + for step in 0..60i32 { + let center = ((step % 17) - 8, (step * 3 % 11) - 5); + requeue(&mut a, &sent, center, r); + requeue_scan(&mut b, &sent, center, r); + assert_eq!(a, b, "radius {r}, step {step}"); + for key in a.drain(..a.len().min(7)) { + sent.insert(key); + } + b.drain(..b.len().min(7)); + sent.retain(|&(x, z)| tracked(center.0, center.1, r, x, z)); + } + } + } + + /// cargo test --release -p horizon-client --lib lan::hub::tests::requeue_cost -- --ignored --nocapture + #[test] + #[ignore] + fn requeue_cost() { + for r in [10, 16, 32] { + for (name, f) in [ + ("scan", requeue_scan as fn(&mut _, &_, _, _)), + ("set", requeue as fn(&mut _, &_, _, _)), + ] { + let sent = FxHashSet::default(); + let mut queue = Vec::new(); + let start = std::time::Instant::now(); + let moves = 200; + for step in 0..moves { + // Nothing sent yet (an explorer waiting on generation). + f(&mut queue, &sent, (step, 0), r); + } + let per = start.elapsed().as_secs_f64() * 1e6 / f64::from(moves); + println!( + "view {r}: {name} {per:.1} us per chunk crossed, {} queued", + queue.len() + ); + } + } + } +} diff --git a/crates/horizon-client/src/ticket_levels.rs b/crates/horizon-client/src/ticket_levels.rs index 216815de1..de2c427ad 100644 --- a/crates/horizon-client/src/ticket_levels.rs +++ b/crates/horizon-client/src/ticket_levels.rs @@ -45,13 +45,52 @@ pub struct Sources { /// block ticking or lower; `None` ticks everywhere (a game with no server and /// no player: the headless fixtures). #[derive(Clone, Default)] -pub struct Levels(Option>>); +pub struct Levels(Option>); + +/// The levels by chunk: a grid over the tickets' bounding box while it is +/// small (players near each other, the usual case), a map otherwise. Every +/// block entity, mob and ticking chunk asks each tick, so an index beats a +/// hash; and a player crossing into another chunk rebuilds it all. +enum Map { + Grid { + x0: i32, + z0: i32, + width: u32, + depth: u32, + cells: Box<[(u8, u8)]>, + }, + Sparse(FxHashMap<(i32, i32), (u8, u8)>), +} + +/// The largest grid, in chunks: 1 MiB of levels, a box 724 chunks wide. +const GRID_CELLS: u64 = 1 << 19; + +impl Map { + fn get(&self, x: i32, z: i32) -> (u8, u8) { + match self { + Self::Grid { + x0, + z0, + width, + depth, + cells, + } => { + let (dx, dz) = (x.wrapping_sub(*x0) as u32, z.wrapping_sub(*z0) as u32); + if dx < *width && dz < *depth { + cells[(dz * width + dx) as usize] + } else { + NONE + } + } + Self::Sparse(map) => map.get(&(x, z)).copied().unwrap_or(NONE), + } + } +} impl Levels { /// Chunk coordinates. fn get(&self, x: i32, z: i32) -> Option<(u8, u8)> { - let map = self.0.as_ref()?; - Some(map.get(&(x, z)).copied().unwrap_or(NONE)) + Some(self.0.as_ref()?.get(x, z)) } /// `DistanceManager.inEntityTickingRange`, in an entity-ticking chunk: /// where entities tick. @@ -112,19 +151,89 @@ impl ChunkTickets { } } -fn compute(s: &Sources) -> FxHashMap<(i32, i32), (u8, u8)> { +/// How far a player's tickets reach, and the simulation ticket's level. +fn reach(s: &Sources) -> (i32, i32, i32) { let view = s.view_distance.clamp(0, 32); let simulation = s.simulation_distance.clamp(0, 32); + // PLAYER_LOADING within the view distance, PLAYER_SIMULATION at the + // player's chunk (`getPlayerTicketLevel`: never below 0). + let player_sim = (i32::from(ENTITY_TICKING) - simulation).max(0); + let reach = (view + 1).max(i32::from(BLOCK_TICKING) - player_sim); + (view, player_sim, reach) +} + +fn compute(s: &Sources) -> Map { + let (view, player_sim, reach) = reach(s); + let boxes = s + .players + .iter() + .map(|&p| (p, reach)) + .chain(s.forced.iter().map(|&f| (f, 1))); + let Some((x0, z0, x1, z1)) = boxes.fold(None, |b, ((x, z), r)| { + let (lo_x, lo_z, hi_x, hi_z) = ( + i64::from(x) - i64::from(r), + i64::from(z) - i64::from(r), + i64::from(x) + i64::from(r), + i64::from(z) + i64::from(r), + ); + Some(match b { + None => (lo_x, lo_z, hi_x, hi_z), + Some((a, b, c, d)) => (lo_x.min(a), lo_z.min(b), hi_x.max(c), hi_z.max(d)), + }) + }) else { + return Map::Sparse(FxHashMap::default()); + }; + let (width, depth) = ((x1 - x0 + 1) as u64, (z1 - z0 + 1) as u64); + if width * depth > GRID_CELLS { + return Map::Sparse(compute_sparse(s)); + } + let (x0, z0, width, depth) = (x0 as i32, z0 as i32, width as u32, depth as u32); + let mut cells = vec![NONE; (width * depth) as usize].into_boxed_slice(); + let mut lower = |x: i32, z: i32, loading: i32, sim: i32| { + let i = ((z - z0) as u32 * width + (x - x0) as u32) as usize; + let e = &mut cells[i]; + e.0 = e.0.min(loading.min(i32::from(NONE.0)) as u8); + e.1 = e.1.min(sim.min(i32::from(NONE.1)) as u8); + }; + for &(px, pz) in &s.players { + for dz in -reach..=reach { + for dx in -reach..=reach { + let d = dx.abs().max(dz.abs()); + lower( + px + dx, + pz + dz, + i32::from(ENTITY_TICKING) + (d - view).max(0), + player_sim + d, + ); + } + } + } + for &(fx, fz) in &s.forced { + for dz in -1i32..=1 { + for dx in -1i32..=1 { + let level = i32::from(ENTITY_TICKING) + dx.abs().max(dz.abs()); + lower(fx + dx, fz + dz, level, level); + } + } + } + Map::Grid { + x0, + z0, + width, + depth, + cells, + } +} + +/// The levels as a map, for tickets spread too far apart for a grid. +fn compute_sparse(s: &Sources) -> FxHashMap<(i32, i32), (u8, u8)> { + let (view, player_sim, reach) = reach(s); let mut levels: FxHashMap<(i32, i32), (u8, u8)> = FxHashMap::default(); let mut lower = |key, loading: i32, sim: i32| { let e = levels.entry(key).or_insert(NONE); e.0 = e.0.min(loading.min(i32::from(NONE.0)) as u8); e.1 = e.1.min(sim.min(i32::from(NONE.1)) as u8); }; - // PLAYER_LOADING within the view distance, PLAYER_SIMULATION at the - // player's chunk (`getPlayerTicketLevel`: never below 0). - let player_sim = (i32::from(ENTITY_TICKING) - simulation).max(0); - let reach = (view + 1).max(i32::from(BLOCK_TICKING) - player_sim); for &(px, pz) in &s.players { for dz in -reach..=reach { for dx in -reach..=reach { @@ -229,4 +338,111 @@ mod tests { t.update(None); assert!(t.levels().0.is_none()); } + + /// The grid answers as the map does, everywhere around the tickets. + #[test] + fn grid_matches_the_map() { + let mut seed = 0x2545_f491_4f6c_dd1du64; + let mut next = |n: i32| { + seed ^= seed << 13; + seed ^= seed >> 7; + seed ^= seed << 17; + (seed % (2 * n as u64 + 1)) as i32 - n + }; + for case in 0..200 { + let spread = [3, 20, 80][case % 3]; + let players: Vec<_> = (0..1 + case % 12) + .map(|_| (next(spread), next(spread))) + .collect(); + let forced: Vec<_> = (0..case % 4) + .map(|_| (next(spread), next(spread))) + .collect(); + let s = Sources { + players, + forced, + view_distance: 2 + next(12).abs(), + simulation_distance: 2 + next(12).abs(), + }; + let Map::Grid { .. } = compute(&s) else { + panic!("case {case} fell back to the map"); + }; + let (grid, map) = (compute(&s), Map::Sparse(compute_sparse(&s))); + let r = spread + 50; + for z in -r..=r { + for x in -r..=r { + assert_eq!(grid.get(x, z), map.get(x, z), "case {case} at ({x}, {z})"); + } + } + } + } + + #[test] + fn far_apart_tickets_use_the_map() { + let l = levels( + &[(0, 0), (100_000, -100_000)], + &[(i32::MAX - 2, i32::MIN + 2)], + 10, + 10, + ); + assert!(matches!(**l.0.as_ref().unwrap(), Map::Sparse(_))); + assert!(l.entity_ticking(100_010, -100_010) && !l.block_ticking(50_000, 0)); + assert!(l.entity_ticking(i32::MAX - 2, i32::MIN + 2)); + } + + /// cargo test --release -p horizon-client --lib ticket_levels::tests::levels_cost -- --ignored --nocapture + #[test] + #[ignore] + fn levels_cost() { + for players in [1usize, 10, 25, 50] { + // The benchmark's bots: most near spawn, a fifth flying outwards. + let at: Vec<_> = (0..players) + .map(|i| { + let a = i as f64 * 2.399; + let d = if i % 5 == 0 { 30.0 } else { 3.0 }; + ((a.cos() * d) as i32, (a.sin() * d) as i32) + }) + .collect(); + let s = Sources { + players: at.clone(), + forced: Vec::new(), + view_distance: 10, + simulation_distance: 10, + }; + let rounds = 200; + let t = std::time::Instant::now(); + for _ in 0..rounds { + std::hint::black_box(compute_sparse(std::hint::black_box(&s))); + } + let map_build = t.elapsed().as_secs_f64() * 1e3 / rounds as f64; + let t = std::time::Instant::now(); + for _ in 0..rounds { + std::hint::black_box(compute(std::hint::black_box(&s))); + } + let grid_build = t.elapsed().as_secs_f64() * 1e3 / rounds as f64; + // A tick's questions: every chunk within 12 of every player. + let (map, grid) = (Map::Sparse(compute_sparse(&s)), compute(&s)); + let ask = |m: &Map| { + let t = std::time::Instant::now(); + let mut n = 0u32; + for _ in 0..rounds { + for &(px, pz) in &at { + for z in pz - 12..=pz + 12 { + for x in px - 12..=px + 12 { + n += + u32::from(std::hint::black_box(m.get(x, z)).0 <= BLOCK_TICKING); + } + } + } + } + std::hint::black_box(n); + t.elapsed().as_secs_f64() * 1e3 / rounds as f64 + }; + println!( + "{players} players: build {map_build:.3} -> {grid_build:.3} ms, {} lookups {:.3} -> {:.3} ms", + players * 625, + ask(&map), + ask(&grid) + ); + } + } } From 62874d8dc43d131c93b2800a767e68f37b0b4be6 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 22 Oct 2026 00:31:00 +0000 Subject: [PATCH 02/29] Furnaces: look up the cooking recipe in an index, without cloning it Every lit furnace, smoker and blast furnace called Recipes::cook each tick, which scanned all cooking recipes (string compares and hash lookups) and the furnace then cloned the recipe found, its ingredient's item set included. Recipes now keeps, built at the first lookup, the first recipe of each kind for each item, which is what the scan returned; the furnace borrows it. 1000 lookups over 320 recipes: 2.07 -> 0.08 ms Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01UxXpf7d4RgpespL2G2UsGE --- crates/horizon-client/src/containers.rs | 2 +- crates/horizon-client/src/crafting.rs | 96 +++++++++++++++++++++++++ 2 files changed, 97 insertions(+), 1 deletion(-) diff --git a/crates/horizon-client/src/containers.rs b/crates/horizon-client/src/containers.rs index 6a135799c..854332815 100644 --- a/crates/horizon-client/src/containers.rs +++ b/crates/horizon-client/src/containers.rs @@ -528,7 +528,7 @@ impl Furnace { let has_fuel = self.items[1].is_some(); if lit || has_fuel && has_ingredient { if let Some(ingredient) = self.items[0].clone() { - if let Some(recipe) = recipes.cook(self.kind.recipe_type(), &ingredient).cloned() { + if let Some(recipe) = recipes.cook(self.kind.recipe_type(), &ingredient) { if self.cooking_timer == 0 { self.cooking_total = total_time(recipe.time, self.speed); } diff --git a/crates/horizon-client/src/crafting.rs b/crates/horizon-client/src/crafting.rs index e96ca0067..d0b8906ee 100644 --- a/crates/horizon-client/src/crafting.rs +++ b/crates/horizon-client/src/crafting.rs @@ -1102,7 +1102,11 @@ pub struct Recipes { facts: HashMap, Facts>, /// The `#minecraft:curse` enchantments. curses: HashSet, + /// The first cooking recipe of each kind taking each item, built at the + /// first lookup (furnaces look theirs up every tick). + cook_index: std::sync::OnceLock, } +type CookIndex = HashMap>; /// `MinMaxBounds.Ints` as (min, max) with the given defaults. fn bounds(v: Option<&Value>, default: (i64, i64), open: (i64, i64)) -> (i64, i64) { match v { @@ -1421,6 +1425,7 @@ impl Recipes { meta, facts, curses, + cook_index: Default::default(), }) } /// Match a `size`x`size` grid (row-major) against the crafting recipes. @@ -1606,6 +1611,22 @@ impl Recipes { out } pub fn cook(&self, kind: &str, input: &ItemStack) -> Option<&Cooking> { + let index = self.cook_index.get_or_init(|| { + let mut index = CookIndex::new(); + for (i, c) in self.cooking.iter().enumerate() { + let of_kind = index.entry(c.kind.clone()).or_default(); + for item in &c.ingredient.0 { + of_kind.entry(item.clone()).or_insert(i); + } + } + index + }); + let i = *index.get(kind)?.get(&*input.item)?; + self.cooking.get(i) + } + /// `cook` by scanning every recipe, as it was before the index. + #[cfg(test)] + fn cook_scan(&self, kind: &str, input: &ItemStack) -> Option<&Cooking> { self.cooking .iter() .find(|c| c.kind == kind && c.ingredient.test(input)) @@ -1625,6 +1646,80 @@ mod tests { s.data_mut().banner_patterns = vec![BannerLayer::new("minecraft:cross", "red")]; s } + /// About vanilla's count of cooking recipes: four kinds, ingredients of + /// one to twelve items, overlapping (several recipes take some items). + fn cooking_book() -> Recipes { + let kinds = ["smelting", "blasting", "smoking", "campfire_cooking"]; + let cooking = (0..320usize) + .map(|i| Cooking { + id: format!("r{i}"), + category: "misc".into(), + group: String::new(), + kind: kinds[i % 4].into(), + ingredient: ingredient( + &(0..1 + i % 12) + .map(|k| format!("minecraft:item_{}", (i * 7 + k * 13) % 400)) + .collect::>() + .iter() + .map(String::as_str) + .collect::>(), + ), + result: ItemStack::new("minecraft:stone", 1), + experience: 0.1, + time: 200, + }) + .collect(); + Recipes { + crafting: Vec::new(), + cooking, + meta: HashMap::new(), + facts: HashMap::new(), + curses: HashSet::new(), + cook_index: Default::default(), + } + } + #[test] + fn cook_finds_the_first_recipe_the_scan_finds() { + let recipes = cooking_book(); + for kind in [ + "smelting", + "blasting", + "smoking", + "campfire_cooking", + "other", + ] { + for n in 0..420 { + let stack = ItemStack::new(&format!("minecraft:item_{n}"), 1); + let a = recipes.cook(kind, &stack).map(|c| &c.id); + let b = recipes.cook_scan(kind, &stack).map(|c| &c.id); + assert_eq!(a, b, "{kind} item_{n}"); + } + } + } + /// A furnace's recipe lookup, once per lit furnace per tick. + #[test] + #[ignore] + fn cook_cost() { + let recipes = cooking_book(); + let stacks: Vec = (0..1000) + .map(|n| ItemStack::new(&format!("minecraft:item_{}", n % 400), 1)) + .collect(); + let t = std::time::Instant::now(); + let a = stacks + .iter() + .filter(|s| recipes.cook_scan("smelting", s).cloned().is_some()) + .count(); + let before = t.elapsed().as_secs_f64() * 1e3; + recipes.cook("smelting", &stacks[0]); + let t = std::time::Instant::now(); + let b = stacks + .iter() + .filter(|s| recipes.cook("smelting", s).is_some()) + .count(); + let after = t.elapsed().as_secs_f64() * 1e3; + assert_eq!(a, b); + println!("1000 furnace lookups (scan and clone -> index): {before:.3} ms -> {after:.3} ms"); + } fn run(special: &Special, cells: &[ItemStack]) -> Option { let recipes = Recipes { crafting: Vec::new(), @@ -1632,6 +1727,7 @@ mod tests { meta: HashMap::new(), facts: HashMap::new(), curses: HashSet::new(), + cook_index: Default::default(), }; let ctx = Context { recipes: &recipes, From 6216b846691aab89cf46a105a66e871703670401 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 21 Oct 2026 23:54:13 +0000 Subject: [PATCH 03/29] Mob tick: build the mob world once a tick, index the snapshot tick_creature_entities called mob_world_base for every mob. That rebuilt the player view (its allies set scans every mob UUID) and, with any scoreboard team, an owned copy of every mob's team, each a clone of the team's player set: cubic in the mobs on a team. The base is now built once and cloned per mob, with the teams borrowed behind an Arc. 100 mobs, half on a team: 37.7 ms -> 0.06 ms per tick 500 mobs, half on a team: 4297 ms -> 0.31 ms per tick MobWorld::seen looked mobs up by scanning the snapshot; the mob ticks now pass an id index (first slot wins, as the scan did): 2000 lookups over 2000 mobs 1.09 -> 0.01 ms. The nearest-living-entity sensors, the look-at goal, the ravager's roar and MobWorld::nearby read candidates from the tick's position grid instead of every mob, in the same order: 2000 mobs 0.39 -> 0.11 ms per tick. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01UxXpf7d4RgpespL2G2UsGE --- crates/horizon-client/src/game/creatures.rs | 20 +- crates/horizon-client/src/game/scoreboard.rs | 8 +- crates/horizon-client/src/mobs.rs | 253 +++++++++++++++++- .../horizon-client/src/mobs/animal_brain.rs | 2 +- crates/horizon-client/src/mobs/goals.rs | 3 +- .../horizon-client/src/mobs/monster_brain.rs | 2 +- crates/horizon-client/src/mobs/ravager.rs | 2 +- .../horizon-client/src/mobs/villager_brain.rs | 2 +- .../horizon-client/src/mobs/warden_brain.rs | 2 +- crates/horizon-client/src/mobs/world.rs | 18 +- 10 files changed, 285 insertions(+), 27 deletions(-) diff --git a/crates/horizon-client/src/game/creatures.rs b/crates/horizon-client/src/game/creatures.rs index 113558e34..d538b5828 100644 --- a/crates/horizon-client/src/game/creatures.rs +++ b/crates/horizon-client/src/game/creatures.rs @@ -389,6 +389,7 @@ impl Game<'_> { player, others, others_grid: None, + others_index: None, daylight: self.dim != Dim::Nether && self.sky_darken() < 4, game_time: self.world.game_time, day_time: self.world.day_time, @@ -401,7 +402,7 @@ impl Game<'_> { end: self.dim == Dim::End, world_seed: self.level.seed, sea_level: if self.dim == Dim::Nether { 32 } else { 63 }, - teams: self.mob_teams(), + teams: std::sync::Arc::new(self.mob_teams()), items: Some(self.res.items()), difficulty: self.local_difficulty( self.player @@ -502,9 +503,22 @@ impl Game<'_> { let mut keep = Vec::with_capacity(mobs.len()); let mut grid = horizon_client::mobs::seen_grid::SeenGrid::new(snapshot.iter().map(|s| s.bounds)); + // All but the snapshot is the same for every mob of the tick, so + // build it once: the player view and the teams scan every UUID. + let base = self.mob_world_base(&[]); + // Each mob keeps its snapshot slot all tick; the first slot of an + // id answers, as a scan would. + let mut index = horizon_client::fast_hash::FxHashMap::::default(); + for (i, s) in snapshot.iter().enumerate() { + index.entry(s.id).or_insert(i as u32); + } for i in 0..mobs.len() { - let mut w = self.mob_world_base(&snapshot); - w.others_grid = Some(&grid); + let mut w = MobWorld { + others: &snapshot, + others_grid: Some(&grid), + others_index: Some(&index), + ..base.clone() + }; let m = &mut mobs[i]; w.nearest_player = super::natural_spawning::nearest_player( &players, diff --git a/crates/horizon-client/src/game/scoreboard.rs b/crates/horizon-client/src/game/scoreboard.rs index 99dae39a5..e01d6ed03 100644 --- a/crates/horizon-client/src/game/scoreboard.rs +++ b/crates/horizon-client/src/game/scoreboard.rs @@ -431,20 +431,18 @@ impl Game<'_> { self.score.board.players_team(self.acting_player_name()) } /// The teams of the player and of the mobs on one (empty without teams). - pub(super) fn mob_teams(&self) -> horizon_client::mobs::Teams { + pub(super) fn mob_teams(&self) -> horizon_client::mobs::Teams<'_> { let board = &self.score.board; if board.teams.is_empty() { return Default::default(); } horizon_client::mobs::Teams { - player: self.player_team().cloned(), + player: self.player_team(), mobs: self .persist .uuids .iter() - .filter_map(|(id, uuid)| { - Some((*id, board.players_team(&uuid_text(*uuid))?.clone())) - }) + .filter_map(|(id, uuid)| Some((*id, board.players_team(&uuid_text(*uuid))?))) .collect(), } } diff --git a/crates/horizon-client/src/mobs.rs b/crates/horizon-client/src/mobs.rs index c8155984c..10d5c19cb 100644 --- a/crates/horizon-client/src/mobs.rs +++ b/crates/horizon-client/src/mobs.rs @@ -1404,6 +1404,7 @@ pub struct PlayerView { /// (`EntitySelector.pushableBy` on the client). pub remote: bool, } +#[derive(Clone)] pub struct MobWorld<'a> { pub world: &'a World, pub registry: &'a horizon_world::registry::BlockRegistry, @@ -1418,6 +1419,8 @@ pub struct MobWorld<'a> { pub others: &'a [Seen], /// `others` by position, when the caller keeps one (the mob ticks). pub others_grid: Option<&'a seen_grid::SeenGrid>, + /// Where each id stands in `others`, when the caller keeps one. + pub others_index: Option<&'a crate::fast_hash::FxHashMap>, pub daylight: bool, pub game_time: i64, pub day_time: i64, @@ -1436,22 +1439,23 @@ pub struct MobWorld<'a> { pub world_seed: i64, /// `Level.getSeaLevel` (63 in the overworld, -63 in a flat world). pub sea_level: i32, - /// The scoreboard teams of the player and the mobs (collision rules). - pub teams: Teams, + /// The scoreboard teams of the player and the mobs (collision rules), + /// shared: the mob ticks build them once a tick, not once a mob. + pub teams: std::sync::Arc>, /// Item data (max stack sizes), when the host has it. pub items: Option<&'a crate::items::Items>, } /// `Entity.getTeam` of the player and of the mobs on a team. #[derive(Clone, Debug, Default)] -pub struct Teams { - pub player: Option, - pub mobs: HashMap, +pub struct Teams<'a> { + pub player: Option<&'a crate::scoreboard::Team>, + pub mobs: HashMap, } -impl Teams { - pub fn of(&self, who: Who) -> Option<&crate::scoreboard::Team> { +impl<'a> Teams<'a> { + pub fn of(&self, who: Who) -> Option<&'a crate::scoreboard::Team> { match who { - Who::Player => self.player.as_ref(), - Who::Mob(id) => self.mobs.get(&id), + Who::Player => self.player, + Who::Mob(id) => self.mobs.get(&id).copied(), } } } @@ -2132,3 +2136,234 @@ mod elytra_tests { assert!((wings.angles[1] - 0.08726646).abs() < 0.00001); } } +#[cfg(test)] +mod teams_tests { + use super::*; + use crate::save::uuid_text; + use crate::scoreboard::{Scoreboard, Team}; + + /// A board where every other mob is on the player's team, as with a + /// datapack that colours its mobs' names. + fn board(mobs: u32) -> (Scoreboard, HashMap) { + let mut board = Scoreboard::default(); + board.add_team("red"); + board.add_to_team("Steve", "red"); + let uuids: HashMap = (0..mobs) + .map(|id| (id, [id as i32, 7, -(id as i32), 11])) + .collect(); + for (id, uuid) in &uuids { + if id % 2 == 0 { + board.add_to_team(&uuid_text(*uuid), "red"); + } + } + (board, uuids) + } + /// `Game::mob_teams` as built for each mob before: owned clones of the teams. + fn owned(board: &Scoreboard, uuids: &HashMap) -> HashMap { + uuids + .iter() + .filter_map(|(id, u)| Some((*id, board.players_team(&uuid_text(*u))?.clone()))) + .collect() + } + fn borrowed<'a>(board: &'a Scoreboard, uuids: &HashMap) -> Teams<'a> { + Teams { + player: board.players_team("Steve"), + mobs: uuids + .iter() + .filter_map(|(id, u)| Some((*id, board.players_team(&uuid_text(*u))?))) + .collect(), + } + } + + #[test] + fn borrowed_teams_answer_as_the_clones() { + let (board, uuids) = board(40); + let teams = borrowed(&board, &uuids); + let old = owned(&board, &uuids); + for id in 0..41 { + assert_eq!(teams.of(Who::Mob(id)), old.get(&id), "mob {id}"); + } + assert_eq!(teams.of(Who::Player).map(|t| t.name.as_str()), Some("red")); + } + + /// One tick's worth of team building: once per mob (before) against once. + #[test] + #[ignore] + fn teams_cost() { + for mobs in [100u32, 250, 500] { + let (board, uuids) = board(mobs); + let t = std::time::Instant::now(); + for _ in 0..mobs { + std::hint::black_box(owned(&board, &uuids)); + } + let before = t.elapsed(); + let t = std::time::Instant::now(); + let reps = 20; + for _ in 0..reps { + std::hint::black_box(borrowed(&board, &uuids)); + } + let after = t.elapsed() / reps; + println!( + "{mobs} mobs, half on a team: per tick {:.3} ms -> {:.3} ms", + before.as_secs_f64() * 1e3, + after.as_secs_f64() * 1e3 + ); + } + } +} +#[cfg(test)] +mod sensor_scan_tests { + use super::*; + use horizon_core::geometry::Vec3; + use serde_json::json; + + fn kind() -> Arc { + Arc::new(MobType { + id: "minecraft:cow".into(), + species: Species::Cow, + width: 0.9, + height: 1.4, + eye_height: 1.3, + category: "creature".into(), + max_health: 10.0, + speed: 0.2, + flying_speed: 0.4, + attack_damage: 0.0, + attack_knockback: 0.0, + follow_range: 16.0, + step_height: 0.6, + armor: 0.0, + armor_toughness: 0.0, + knockback_resistance: 0.0, + jump_strength: 0.42, + attributes: Arc::default(), + gravity: 0.08, + safe_fall_distance: 3.0, + fall_damage_multiplier: 1.0, + water_efficiency: 0.0, + water_slowdown: 0.8, + air_speed: 0.02, + baby_scale: 0.5, + head_rot_speed: 10.0, + max_head_x_rot: 40.0, + max_head_y_rot: 75.0, + ambient_interval: 120, + flags: Flags { + animal: true, + ..Default::default() + }, + model: Arc::new(Part::parse("root", &json!({}))), + layers: HashMap::new(), + texture: String::new(), + brain: Brain::Passive, + anim: Anim::Rabbit, + sounds: [""; 3], + step_sound: None, + food: Arc::default(), + tempt: Arc::default(), + }) + } + /// `n` mobs in pens of 20 spread over a square of `side` blocks, as on a + /// server's farms; their snapshot and its grid. + fn herd(n: u32, side: f64) -> (Vec, seen_grid::SeenGrid) { + let kind = kind(); + let mut seed = 0x9e37_79b9_7f4a_7c15u64; + 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 pen = Vec3::ZERO; + let seen: Vec = (0..n) + .map(|id| { + if id % 20 == 0 { + pen = Vec3::new(next(side), 64.0, next(side)); + } + let at = Vec3::new(pen.x + next(8.0), 64.0, pen.z + next(8.0)); + Mob::new(id, kind.clone(), at, 0.0).seen(None) + }) + .collect(); + let grid = seen_grid::SeenGrid::new(seen.iter().map(|s| s.bounds)); + (seen, grid) + } + /// `NearestLivingEntitySensor` over every mob, as the brains ran it. + fn scan(seen: &[Seen], me: &Seen, area: Aabb) -> Vec { + seen.iter() + .filter(|o| o.id != me.id && o.alive && o.bounds.intersects(area)) + .map(|o| o.id) + .collect() + } + fn indexed(seen: &[Seen], grid: &seen_grid::SeenGrid, me: &Seen, area: Aabb) -> Vec { + let mut near = Vec::new(); + assert!(grid.candidates(area, &mut near)); + near.iter() + .map(|&i| &seen[i as usize]) + .filter(|o| o.id != me.id && o.alive && o.bounds.intersects(area)) + .map(|o| o.id) + .collect() + } + + #[test] + fn the_grid_finds_what_the_scan_finds() { + let (seen, grid) = herd(600, 160.0); + for me in &seen { + let area = me.bounds.inflate(Vec3::new(16.0, 16.0, 16.0)); + assert_eq!(indexed(&seen, &grid, me, area), scan(&seen, me, area)); + } + } + + /// `MobWorld::seen` once per mob (a target, a breeding partner...), by + /// scan as before and by the tick's id index. + #[test] + #[ignore] + fn lookup_cost() { + for n in [500u32, 1000, 2000] { + let (seen, _) = herd(n, 256.0); + let index: crate::fast_hash::FxHashMap = seen + .iter() + .enumerate() + .map(|(i, s)| (s.id, i as u32)) + .collect(); + let wanted: Vec = (0..n).map(|i| (i * 7919) % n).collect(); + let t = std::time::Instant::now(); + let a: usize = wanted + .iter() + .filter_map(|&id| seen.iter().find(|s| s.id == id)) + .map(|s| s.id as usize) + .sum(); + let before = t.elapsed().as_secs_f64() * 1e3; + let t = std::time::Instant::now(); + let b: usize = wanted + .iter() + .filter_map(|&id| index.get(&id).map(|&i| &seen[i as usize])) + .map(|s| s.id as usize) + .sum(); + let after = t.elapsed().as_secs_f64() * 1e3; + assert_eq!(a, b); + println!( + "{n} lookups over {n} mobs ({} B each): {before:.3} ms -> {after:.3} ms", + std::mem::size_of::() + ); + } + } + + /// One tick of sensing: a twentieth of the mobs fire their sensor. + #[test] + #[ignore] + fn sensor_cost() { + for (n, side) in [(500u32, 256.0), (1000, 384.0), (2000, 512.0)] { + let (seen, grid) = herd(n, side); + let time = |f: &dyn Fn(&Seen, Aabb) -> usize| { + let t = std::time::Instant::now(); + let mut found = 0; + for me in &seen { + found += f(me, me.bounds.inflate(Vec3::new(16.0, 16.0, 16.0))); + } + (t.elapsed().as_secs_f64() * 1e3 / 20.0, found) + }; + let (before, a) = time(&|me, area| scan(&seen, me, area).len()); + let (after, b) = time(&|me, area| indexed(&seen, &grid, me, area).len()); + assert_eq!(a, b); + println!("{n} mobs: per tick {before:.3} ms -> {after:.3} ms"); + } + } +} diff --git a/crates/horizon-client/src/mobs/animal_brain.rs b/crates/horizon-client/src/mobs/animal_brain.rs index 9920235a9..331870b4a 100644 --- a/crates/horizon-client/src/mobs/animal_brain.rs +++ b/crates/horizon-client/src/mobs/animal_brain.rs @@ -606,7 +606,7 @@ fn sense_living(m: &mut Mob, w: &MobWorld) { let r = m.attrs.follow_range; let area = m.bounds().inflate(Vec3::new(r, r, r)); let mut found: Vec<(Who, f64)> = Vec::new(); - for o in w.others { + for o in w.others_near(area) { if o.id != m.id && o.alive && o.bounds.intersects(area) { found.push((Who::Mob(o.id), m.distance_sqr(o.position))); } diff --git a/crates/horizon-client/src/mobs/goals.rs b/crates/horizon-client/src/mobs/goals.rs index 9419a23ba..38108f8bc 100644 --- a/crates/horizon-client/src/mobs/goals.rs +++ b/crates/horizon-client/src/mobs/goals.rs @@ -970,8 +970,7 @@ impl Goal for LookAtPlayer { let (id, only) = (m.id, self.only); let at = Vec3::new(m.position.x, m.eye_y(), m.position.z); let candidates: Vec<(u32, Vec3)> = - cx.w.others - .iter() + cx.w.others_near(area) .filter(|s| s.id != id && s.alive && s.bounds.intersects(area)) .filter(|s| only.is_none_or(|o| s.species == o)) .map(|s| (s.id, s.position)) diff --git a/crates/horizon-client/src/mobs/monster_brain.rs b/crates/horizon-client/src/mobs/monster_brain.rs index 238653dad..eeb2c42f2 100644 --- a/crates/horizon-client/src/mobs/monster_brain.rs +++ b/crates/horizon-client/src/mobs/monster_brain.rs @@ -299,7 +299,7 @@ fn sense_living(m: &mut Mob, w: &MobWorld) { let r = m.attrs.follow_range; let area = m.bounds().inflate(Vec3::new(r, r, r)); let mut found: Vec<(Who, f64)> = Vec::new(); - for o in w.others { + for o in w.others_near(area) { if o.id != m.id && o.alive && o.bounds.intersects(area) { found.push((Who::Mob(o.id), m.distance_sqr(o.position))); } diff --git a/crates/horizon-client/src/mobs/ravager.rs b/crates/horizon-client/src/mobs/ravager.rs index ef71b8f77..49bac4d9f 100644 --- a/crates/horizon-client/src/mobs/ravager.rs +++ b/crates/horizon-client/src/mobs/ravager.rs @@ -177,7 +177,7 @@ fn roar(m: &mut Mob, w: &MobWorld, events: &mut Vec) { velocity: fling(p.position), }); } - for s in w.others.iter() { + for s in w.others_near(area) { if s.id == m.id || !s.alive || s.species == Species::Ravager || !s.bounds.intersects(area) { continue; } diff --git a/crates/horizon-client/src/mobs/villager_brain.rs b/crates/horizon-client/src/mobs/villager_brain.rs index 5b8500c0d..d401ea6da 100644 --- a/crates/horizon-client/src/mobs/villager_brain.rs +++ b/crates/horizon-client/src/mobs/villager_brain.rs @@ -568,7 +568,7 @@ fn sense_living(m: &mut Mob, w: &MobWorld) { let r = m.attrs.follow_range; let area = m.bounds().inflate(Vec3::new(r, r, r)); let mut found: Vec<(Who, f64)> = Vec::new(); - for o in w.others { + for o in w.others_near(area) { if o.id != m.id && o.alive && o.bounds.intersects(area) { found.push((Who::Mob(o.id), m.distance_sqr(o.position))); } diff --git a/crates/horizon-client/src/mobs/warden_brain.rs b/crates/horizon-client/src/mobs/warden_brain.rs index 41353fc83..518347e77 100644 --- a/crates/horizon-client/src/mobs/warden_brain.rs +++ b/crates/horizon-client/src/mobs/warden_brain.rs @@ -492,7 +492,7 @@ fn sense_entities(m: &mut Mob, w: &MobWorld) { let r = m.attrs.follow_range; let area = m.bounds().inflate(Vec3::new(r, r, r)); let mut found: Vec<(Who, f64)> = Vec::new(); - for o in w.others { + for o in w.others_near(area) { if o.id != m.id && o.alive && o.bounds.intersects(area) { found.push((Who::Mob(o.id), m.distance_sqr(o.position))); } diff --git a/crates/horizon-client/src/mobs/world.rs b/crates/horizon-client/src/mobs/world.rs index cc4110dd7..79bbaa30c 100644 --- a/crates/horizon-client/src/mobs/world.rs +++ b/crates/horizon-client/src/mobs/world.rs @@ -226,7 +226,13 @@ impl MobWorld<'_> { .chain(all.into_iter().flatten()) } pub fn seen(&self, id: u32) -> Option<&Seen> { - self.others.iter().find(|s| s.id == id) + match self.others_index { + Some(index) => index + .get(&id) + .and_then(|&i| self.others.get(i as usize)) + .filter(|s| s.id == id), + None => self.others.iter().find(|s| s.id == id), + } } /// Living mobs of the given species within `range` of `from`, nearest first. pub fn nearby<'s>( @@ -235,9 +241,15 @@ impl MobWorld<'_> { range: f64, pred: impl Fn(&Seen) -> bool + 's, ) -> impl Iterator + 's { + // A position within `range` puts the box's bottom face in this area + // (one block wider, as the box test is strict). + let r = range + 1.0; + let area = Aabb::new( + Vec3::new(from.x - r, from.y - r, from.z - r), + Vec3::new(from.x + r, from.y + r, from.z + r), + ); let mut v: Vec<&Seen> = self - .others - .iter() + .others_near(area) .filter(|s| { s.alive && (s.position.x - from.x).abs() <= range From 67ca65bb3b003999c511a4bd729274acc0f6da38 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 21 Oct 2026 23:20:00 +0000 Subject: [PATCH 04/29] Mob tick: drop gone mobs' guest bindings with a set, not a scan per binding On a server every ticked mob is bound to its nearest player, and each tick the bindings of mobs that are gone were dropped by scanning every mob for each binding. Mob is 1.5 KB, so the scan misses the cache on every mob: with 1000 loaded mobs that was 0.44 ms a tick, with 2000 1.86 ms; a set of the live ids makes it 0.02 and 0.04 ms (stand-in structs of Mob's size). Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01UxXpf7d4RgpespL2G2UsGE --- crates/horizon-client/src/game/creatures.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/crates/horizon-client/src/game/creatures.rs b/crates/horizon-client/src/game/creatures.rs index 113558e34..053047b42 100644 --- a/crates/horizon-client/src/game/creatures.rs +++ b/crates/horizon-client/src/game/creatures.rs @@ -588,7 +588,11 @@ impl Game<'_> { if let Some(host) = self.lan.as_mut() && !guests.is_empty() { - bindings.retain(|id, _| mobs.iter().any(|m| m.id == *id)); + // A set: a scan of every mob for each binding is quadratic, and a + // server's loaded mobs run into the thousands. + let live: horizon_client::fast_hash::FxHashSet = + mobs.iter().map(|m| m.id).collect(); + bindings.retain(|id, _| live.contains(id)); host.mob_players = bindings; } self.mobs.append(&mut mobs); From 3b21a975bb04548d8f64a5adde7d4b24859332ce Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 22 Oct 2026 01:01:03 +0000 Subject: [PATCH 05/29] Mob goals: read nearby mobs from the tick's grid in four more places FollowParentGoal (every baby, every tick), the pufferfish's puff check, the enderman's block placing and the iron golem's DefendVillage scanned every mob. They now read candidates from the position grid around the area they test (one block wider where they test positions, as a box whose bottom face holds the position meets it), in snapshot order, and test exactly as before. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01UxXpf7d4RgpespL2G2UsGE --- crates/horizon-client/src/mobs/aquatic.rs | 2 +- crates/horizon-client/src/mobs/enderman.rs | 11 +++++------ crates/horizon-client/src/mobs/goals.rs | 13 +++++++++++-- crates/horizon-client/src/mobs/golem.rs | 5 ++++- 4 files changed, 21 insertions(+), 10 deletions(-) diff --git a/crates/horizon-client/src/mobs/aquatic.rs b/crates/horizon-client/src/mobs/aquatic.rs index 702e3740d..adb2cbe3a 100644 --- a/crates/horizon-client/src/mobs/aquatic.rs +++ b/crates/horizon-client/src/mobs/aquatic.rs @@ -438,7 +438,7 @@ impl Goal for PufferfishPuff { .as_ref() .is_some_and(|p| p.alive && !p.creative && area.intersects(p.bounds)); // TARGETING_CONDITIONS: not other fish (not_scary_for_pufferfish) and alive. - let mob = cx.w.others.iter().any(|s| { + let mob = cx.w.others_near(area).any(|s| { s.id != m.id && s.alive && !matches!( diff --git a/crates/horizon-client/src/mobs/enderman.rs b/crates/horizon-client/src/mobs/enderman.rs index 13e6c8a44..fcfba832e 100644 --- a/crates/horizon-client/src/mobs/enderman.rs +++ b/crates/horizon-client/src/mobs/enderman.rs @@ -391,12 +391,11 @@ impl Goal for LeaveBlock { let target = cx.w.world.block(x, y, z); let below = cx.w.world.block(x, y - 1, z); let bf = cx.w.facts.get(below); - let free = !cx.w.others.iter().any(|s| { - s.bounds.intersects(Aabb::new( - Vec3::new(f64::from(x), f64::from(y), f64::from(z)), - Vec3::new(f64::from(x + 1), f64::from(y + 1), f64::from(z + 1)), - )) - }); + let block = Aabb::new( + Vec3::new(f64::from(x), f64::from(y), f64::from(z)), + Vec3::new(f64::from(x + 1), f64::from(y + 1), f64::from(z + 1)), + ); + let free = !cx.w.others_near(block).any(|s| s.bounds.intersects(block)); if cx.w.facts.is_air(target) && !cx.w.facts.is_air(below) && &*bf.name != "minecraft:bedrock" diff --git a/crates/horizon-client/src/mobs/goals.rs b/crates/horizon-client/src/mobs/goals.rs index 38108f8bc..c12f9e7ee 100644 --- a/crates/horizon-client/src/mobs/goals.rs +++ b/crates/horizon-client/src/mobs/goals.rs @@ -1,7 +1,11 @@ //! Vanilla's `GoalSelector` (priorities, MOVE/LOOK/JUMP/TARGET control flags, //! interruption) and the shared goals of `net.minecraft.world.entity.ai.goal`. use super::{Mob, MobEvent, MobWorld, Seen, Species, Who, world::TargetInfo}; -use horizon_core::{geometry::Vec3, jmath, random::RandomSource}; +use horizon_core::{ + geometry::{Aabb, Vec3}, + jmath, + random::RandomSource, +}; use std::sync::Arc; pub const MOVE: u8 = 1; @@ -1901,7 +1905,12 @@ impl Goal for FollowParent { let species = m.species(); let pos = m.position; let mut best: Option<(u32, f64)> = None; - for s in cx.w.others.iter().filter(|s| { + // Every position in range puts the box's bottom face in this area. + let area = Aabb::new( + Vec3::new(pos.x - 9.0, pos.y - 5.0, pos.z - 9.0), + Vec3::new(pos.x + 9.0, pos.y + 5.0, pos.z + 9.0), + ); + for s in cx.w.others_near(area).filter(|s| { s.alive && s.species == species && !s.baby diff --git a/crates/horizon-client/src/mobs/golem.rs b/crates/horizon-client/src/mobs/golem.rs index d4d67660b..c41c34f11 100644 --- a/crates/horizon-client/src/mobs/golem.rs +++ b/crates/horizon-client/src/mobs/golem.rs @@ -68,7 +68,10 @@ impl Goal for DefendVillage { if !b.intersects(p.bounds) || m.position.distance_squared(p.position) > 64.0 * 64.0 { return false; } - let bad = cx.w.others.iter().any(|s| { + // A position inside `b` puts the box's bottom face there (one block + // wider, as the box test is strict). + let near = b.inflate(Vec3::new(1.0, 1.0, 1.0)); + let bad = cx.w.others_near(near).any(|s| { s.species == Species::Villager && s.alive && s.player_rep <= -100 From a893fa6125eacaa13ea9db58d2c8d0e278bee1f3 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 22 Oct 2026 00:54:15 +0000 Subject: [PATCH 06/29] Villagers: count the others' POI tickets once per search pois_in_range (AcquirePoi and the job, bed and meeting point searches) asked, for every candidate POI in range, how many tickets it had, and each answer scanned every other mob for villagers holding it: POIs times mobs per search. The others' tickets are now counted into a map at the first candidate and read from it; this villager's own memories are still checked per POI, as before. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01UxXpf7d4RgpespL2G2UsGE --- .../horizon-client/src/mobs/villager_brain.rs | 26 +++++++++++++------ 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/crates/horizon-client/src/mobs/villager_brain.rs b/crates/horizon-client/src/mobs/villager_brain.rs index 5b8500c0d..e981e4dda 100644 --- a/crates/horizon-client/src/mobs/villager_brain.rs +++ b/crates/horizon-client/src/mobs/villager_brain.rs @@ -898,8 +898,9 @@ fn poi_matches(m: &Mob, f: PoiFilter, p: Poi) -> bool { _ => false, } } -/// Tickets taken on a POI: by this villager's memories and by others'. -fn tickets(m: &Mob, w: &MobWorld, p: [i32; 3]) -> usize { +/// Tickets taken on a POI: by this villager's memories and by others' +/// (counted once per search by [`others_tickets`]). +fn tickets(m: &Mob, others: &crate::fast_hash::FxHashMap<[i32; 3], usize>, p: [i32; 3]) -> usize { let mut n = 0; if let Some(mm) = mem_ref(m) { for s in [ @@ -913,16 +914,22 @@ fn tickets(m: &Mob, w: &MobWorld, p: [i32; 3]) -> usize { } } } + n + others.get(&p).copied().unwrap_or(0) +} +/// The tickets the other living villagers' POIs take, by POI. +fn others_tickets(m: &Mob, w: &MobWorld) -> crate::fast_hash::FxHashMap<[i32; 3], usize> { + let mut taken = crate::fast_hash::FxHashMap::default(); for o in w.others { if o.id != m.id && o.alive && o.species == Species::Villager { - for q in [o.job, o.bed, o.potential_job, o.meeting] { - if q == Some(p) { - n += 1; - } + for q in [o.job, o.bed, o.potential_job, o.meeting] + .into_iter() + .flatten() + { + *taken.entry(q).or_insert(0) += 1; } } } - n + taken } /// The POIs of chunk `(qx, qz)` from `y0` to `y1` (within the world), in /// scan order: y, then z, then x. Only the indexed POI blocks of the sections @@ -962,6 +969,8 @@ fn pois_in_range( ) -> Vec<(Poi, [i32; 3])> { let c = block_of(m.position); let mut found = Vec::new(); + // Every candidate asks how taken it is: count the others' once. + let mut taken = None; let r2 = i64::from(radius) * i64::from(radius); let (cx, cz) = (c[0] >> 4, c[2] >> 4); let chunks = radius.div_euclid(16) + 1; @@ -974,7 +983,8 @@ fn pois_in_range( || (p[2] - c[2]).abs() > radius || dist_sqr(p, c) > r2 || !poi_matches(m, f, poi) - || tickets(m, w, p) >= poi.max_tickets() + || tickets(m, taken.get_or_insert_with(|| others_tickets(m, w)), p) + >= poi.max_tickets() { return; } From 3917f35e3e781954186d7c2c5be5fa739a333e5e Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 22 Oct 2026 00:07:18 +0000 Subject: [PATCH 07/29] Natural spawning: answer mob box queries from a grid after a few scans GameLevel's isUnobstructed, noCollision (living shulkers) and the spawners' entity count each scanned every mob, computing each 1.5 KB mob's box. A spawn pass holds the game immutably, so after eight queries it builds a grid of the mobs' boxes once and reads candidates from it; the first queries still scan, as the build costs a few scans. 100 box tests, 2000 mobs: 3.48 -> 0.31 ms (grid build included) Also gives the mob_ai_probe example the new others_index field. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01UxXpf7d4RgpespL2G2UsGE --- .../horizon-client/examples/mob_ai_probe.rs | 1 + .../src/game/natural_spawning.rs | 88 ++++++++++++++++--- crates/horizon-client/src/mobs.rs | 52 +++++++++++ 3 files changed, 127 insertions(+), 14 deletions(-) diff --git a/crates/horizon-client/examples/mob_ai_probe.rs b/crates/horizon-client/examples/mob_ai_probe.rs index 2a4969221..66d06aa84 100644 --- a/crates/horizon-client/examples/mob_ai_probe.rs +++ b/crates/horizon-client/examples/mob_ai_probe.rs @@ -1027,6 +1027,7 @@ fn main() -> horizon_client::Result<()> { nearest_player: player.as_ref().filter(|p| !p.spectator).map(|p| p.position), others: $snapshot, others_grid: $grid, + others_index: None, daylight: (world.day_time.rem_euclid(24000)) < 12000, game_time: world.game_time, day_time: world.day_time, diff --git a/crates/horizon-client/src/game/natural_spawning.rs b/crates/horizon-client/src/game/natural_spawning.rs index 0fab09bde..6e7ddf500 100644 --- a/crates/horizon-client/src/game/natural_spawning.rs +++ b/crates/horizon-client/src/game/natural_spawning.rs @@ -9,6 +9,7 @@ use horizon_client::{ mobs::{ Mob, MobWorld, Species, data::{self, MobData}, + seen_grid::SeenGrid, spawn_rules::{Dimension, SpawnLevel}, spawn_selection::{ self, Category, Counted, Heightmap, NaturalLevel, SpawnCost, SpawnPlayer, SpawnRequest, @@ -75,8 +76,72 @@ pub(super) struct GameLevel<'g, 'a> { pub(super) pending: std::cell::RefCell>, /// The types and boxes of what a spawner created this tick. pub(super) pending_ids: std::cell::RefCell>, + /// The game's mobs by box, built once a pass has asked more than + /// [`SCANNED_QUERIES`] box queries (the mobs do not change while it + /// holds the game); the first ones scan, as the build costs a few scans. + pub(super) boxes: std::cell::OnceCell, + pub(super) box_queries: std::cell::Cell, +} +const SCANNED_QUERIES: u32 = 8; +/// A mob not removed: its index in `Game::mobs`, box, alive, and whether +/// it is a living shulker. +type MobBox = (usize, Aabb, bool, bool); +fn mob_box(i: usize, m: &Mob) -> MobBox { + let alive = m.alive(); + ( + i, + m.bounds(), + alive, + alive && m.species() == Species::Shulker, + ) +} +/// The boxes of the game's mobs not removed, and a grid over them. +pub(super) struct MobBoxes { + entries: Vec, + grid: SeenGrid, +} +impl MobBoxes { + fn new(mobs: &[Mob]) -> Self { + let entries: Vec<_> = mobs + .iter() + .enumerate() + .filter(|(_, m)| !m.removed) + .map(|(i, m)| mob_box(i, m)) + .collect(); + let grid = SeenGrid::new(entries.iter().map(|e| e.1)); + Self { entries, grid } + } + /// The entries whose boxes meet `area` (and possibly a few more). + fn near(&self, area: Aabb) -> impl Iterator + '_ { + let mut near = Vec::new(); + let indexed = self.grid.candidates(area, &mut near); + let all = (!indexed).then(|| self.entries.iter()); + near.into_iter() + .map(|i| self.entries[i as usize]) + .chain(all.into_iter().flatten().copied()) + } } impl GameLevel<'_, '_> { + /// The mobs not removed whose boxes may meet `area` (callers test them). + fn mobs_near(&self, area: Aabb) -> Box + '_> { + let asked = self.box_queries.get() + 1; + self.box_queries.set(asked); + if asked <= SCANNED_QUERIES && self.boxes.get().is_none() { + return Box::new( + self.game + .mobs + .iter() + .enumerate() + .filter(|(_, m)| !m.removed) + .map(|(i, m)| mob_box(i, m)), + ); + } + Box::new( + self.boxes + .get_or_init(|| MobBoxes::new(&self.game.mobs)) + .near(area), + ) + } fn biome(&self, x: i32, y: i32, z: i32) -> Option<&BiomeInfo> { let chunk = self.game.world.chunks.get(&(x >> 4, z >> 4))?; Some(self.game.res.biomes().get(chunk.biome(x, y, z))) @@ -218,12 +283,9 @@ impl NaturalLevel for GameLevel<'_, '_> { // getEntityCollisions(null, box): living shulkers are solid. let grown = bounds.inflate(Vec3::new(1e-7, 1e-7, 1e-7)); no_collision_empty(&mut scene, bounds) - && !self.game.mobs.iter().any(|m| { - m.species() == Species::Shulker - && m.alive() - && !m.removed - && m.bounds().intersects(grown) - }) + && !self + .mobs_near(grown) + .any(|(_, b, _, shulker)| shulker && b.intersects(grown)) && !self .pending_ids .borrow() @@ -238,10 +300,8 @@ impl NaturalLevel for GameLevel<'_, '_> { !player && !self.pending.borrow().iter().any(|b| b.intersects(bounds)) && !self - .game - .mobs - .iter() - .any(|m| m.alive() && !m.removed && m.bounds().intersects(bounds)) + .mobs_near(bounds) + .any(|(_, b, alive, _)| alive && b.intersects(bounds)) } fn spawn_mobs(&self) -> bool { self.game.ui.rules.bool("spawn_mobs") @@ -283,10 +343,8 @@ impl horizon_client::mobs::spawner::SpawnerLevel for GameLevel<'_, '_> { } fn count_entities(&self, id: &str, bounds: Aabb) -> usize { let mobs = self - .game - .mobs - .iter() - .filter(|m| !m.removed && m.kind.id == id && m.bounds().intersects(bounds)) + .mobs_near(bounds) + .filter(|&(i, b, ..)| self.game.mobs[i].kind.id == id && b.intersects(bounds)) .count(); let pending = self .pending_ids @@ -305,6 +363,8 @@ impl<'a> Game<'a> { players: self.level_players(), pending: Default::default(), pending_ids: Default::default(), + boxes: Default::default(), + box_queries: Default::default(), } } /// Creates a mob the spawner accepted, finalized the game's way; the diff --git a/crates/horizon-client/src/mobs.rs b/crates/horizon-client/src/mobs.rs index 10d5c19cb..ecce6fede 100644 --- a/crates/horizon-client/src/mobs.rs +++ b/crates/horizon-client/src/mobs.rs @@ -2346,6 +2346,58 @@ mod sensor_scan_tests { } } + /// Natural spawning's `isUnobstructed` box test against every mob (real + /// `Mob`s), by scan and by a grid built once for the pass. + #[test] + #[ignore] + fn spawn_box_cost() { + let kind = kind(); + for n in [500u32, 1000, 2000] { + let (seen, _) = herd(n, 384.0); + let mobs: Vec = seen + .iter() + .map(|s| Mob::new(s.id, kind.clone(), s.position, 0.0)) + .collect(); + let mut seed = 7u64; + 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 queries: Vec = (0..100) + .map(|_| { + let (x, z) = (next(384.0), next(384.0)); + Aabb::new(Vec3::new(x, 64.0, z), Vec3::new(x + 0.9, 65.4, z + 0.9)) + }) + .collect(); + let t = std::time::Instant::now(); + let a: Vec = queries + .iter() + .map(|&q| { + mobs.iter() + .any(|m| m.alive() && !m.removed && m.bounds().intersects(q)) + }) + .collect(); + let before = t.elapsed().as_secs_f64() * 1e3; + let t = std::time::Instant::now(); + let boxes: Vec = mobs.iter().map(|m| m.bounds()).collect(); + let grid = seen_grid::SeenGrid::new(boxes.iter().copied()); + let build = t.elapsed().as_secs_f64() * 1e3; + let mut near = Vec::new(); + let b: Vec = queries + .iter() + .map(|&q| { + assert!(grid.candidates(q, &mut near)); + near.iter().any(|&i| boxes[i as usize].intersects(q)) + }) + .collect(); + let after = t.elapsed().as_secs_f64() * 1e3; + assert_eq!(a, b); + println!( + "{n} mobs, 100 spawn box tests: {before:.3} ms -> {after:.3} ms (grid build {build:.3} ms included)" + ); + } + } + /// One tick of sensing: a twentieth of the mobs fire their sensor. #[test] #[ignore] From 863a3091612f065e9118fc94c709249fa14ada68 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 22 Oct 2026 00:34:52 +0000 Subject: [PATCH 08/29] Allays and piglins: hoist per-tick lookups out of the per-mob loop tick_allays scanned every block entity of the level for each allay, every tick, to find a playing jukebox within 10 blocks. The playing jukeboxes are now collected once, at the first allay (no allay starts or stops one). piglin_pickups looked up the piglin_loved tag and hashed every item entity's id once per piglin. Each item's flag is now computed once and kept until an item is added (always pushed at the end under a new id) or removed. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01UxXpf7d4RgpespL2G2UsGE --- crates/horizon-client/src/game/allays.rs | 22 ++++++++++++++++------ crates/horizon-client/src/game/piglins.rs | 14 +++++++++++++- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/crates/horizon-client/src/game/allays.rs b/crates/horizon-client/src/game/allays.rs index 52dfa7e1c..bed835663 100644 --- a/crates/horizon-client/src/game/allays.rs +++ b/crates/horizon-client/src/game/allays.rs @@ -9,18 +9,28 @@ impl Game<'_> { pub(super) fn tick_allays(&mut self) { let griefing = self.ui.rules.bool("mob_griefing"); let player = self.player.as_ref().map(|p| p.position); + // The playing jukeboxes, found at the first allay: no allay starts + // or stops one, and the block entities run into the thousands. + let mut jukeboxes: Option> = None; for i in 0..self.mobs.len() { if self.mobs[i].species() != Species::Allay || !self.mobs[i].alive() { continue; } let pos = self.mobs[i].position; // Dancing: a playing jukebox within 10 blocks. - let dancing = self.world.block_entities.iter().any(|(k, e)| { - matches!(e, BlockEntity::Jukebox(j) if j.playing) - && (f64::from(k.0) + 0.5 - pos.x).powi(2) - + (f64::from(k.1) + 0.5 - pos.y).powi(2) - + (f64::from(k.2) + 0.5 - pos.z).powi(2) - <= 100.0 + let jukeboxes = jukeboxes.get_or_insert_with(|| { + self.world + .block_entities + .iter() + .filter(|(_, e)| matches!(e, BlockEntity::Jukebox(j) if j.playing)) + .map(|(k, _)| *k) + .collect() + }); + let dancing = jukeboxes.iter().any(|k| { + (f64::from(k.0) + 0.5 - pos.x).powi(2) + + (f64::from(k.1) + 0.5 - pos.y).powi(2) + + (f64::from(k.2) + 0.5 - pos.z).powi(2) + <= 100.0 }); self.mobs[i].extra.flag = dancing && !horizon_client::mobs::goals::should_panic(&self.mobs[i]); diff --git a/crates/horizon-client/src/game/piglins.rs b/crates/horizon-client/src/game/piglins.rs index 9df5311aa..6a1e2d6b7 100644 --- a/crates/horizon-client/src/game/piglins.rs +++ b/crates/horizon-client/src/game/piglins.rs @@ -34,6 +34,9 @@ impl Game<'_> { if !self.ui.rules.bool("mob_griefing") { return; } + // Whether each item is piglin-loved, kept until an item is added or + // removed (added ones are pushed at the end under a new id). + let mut loved: (Option<(usize, Option)>, Vec) = (None, Vec::new()); for i in 0..self.mobs.len() { let m = &self.mobs[i]; if m.species() != Species::Piglin @@ -46,11 +49,20 @@ impl Game<'_> { continue; } let pos = m.position; + let key = (self.dropped.len(), self.dropped.last().map(|e| e.id)); + if loved.0 != Some(key) { + loved.0 = Some(key); + loved.1 = self + .dropped + .iter() + .map(|e| self.piglin_loved(&e.stack.item)) + .collect(); + } let best = self .dropped .iter() .enumerate() - .filter(|(_, e)| self.piglin_loved(&e.stack.item)) + .filter(|&(j, _)| loved.1[j]) .map(|(j, e)| (j, e.position.distance_squared(pos))) .filter(|(_, d)| *d <= 81.0) .min_by(|a, b| a.1.total_cmp(&b.1)); From cb0bbe7d19e0123e11915244f906791786dd244d Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 22 Oct 2026 00:33:01 +0000 Subject: [PATCH 09/29] Sculk: skip the loaded-chunk diff while no chunk came or went sculk_sync_index collected every loaded chunk key into a set and diffed it against the known chunks every ten ticks, thousands of keys on a busy server. It now remembers the chunk map's membership revision and skips the diff while it is unchanged; a replaced chunk keeps its key and was not rescanned before either. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01UxXpf7d4RgpespL2G2UsGE --- crates/horizon-client/src/game/sculk.rs | 7 ++++++- crates/horizon-client/src/game/sculk_fixture.rs | 1 + 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/crates/horizon-client/src/game/sculk.rs b/crates/horizon-client/src/game/sculk.rs index 096562608..8667f305f 100644 --- a/crates/horizon-client/src/game/sculk.rs +++ b/crates/horizon-client/src/game/sculk.rs @@ -71,6 +71,8 @@ pub(super) struct SculkState { /// The sculk block entities in the order they were added. index: Index, pub known: HashSet<(i32, i32)>, + /// The chunk map's membership revision `known` was taken at. + pub known_revision: Option, // Keyed by mob ids the server gives out: the fast hasher is safe. steps: FxHashMap, damaged: FxHashMap, @@ -336,7 +338,10 @@ impl Game<'_> { /// the block writes recorded since the last tick. pub(super) fn sculk_sync_index(&mut self) { // Chunks that arrived or left (every half second is plenty). - if self.sculk.ticks.is_multiple_of(10) { + // Nothing to do while no chunk came or went. + let revision = self.world.chunks.membership_revision(); + if self.sculk.ticks.is_multiple_of(10) && self.sculk.known_revision != Some(revision) { + self.sculk.known_revision = Some(revision); let loaded: HashSet<(i32, i32)> = self.world.chunks.keys().copied().collect(); let arrived: Vec<(i32, i32)> = loaded.difference(&self.sculk.known).copied().collect(); for key in arrived { diff --git a/crates/horizon-client/src/game/sculk_fixture.rs b/crates/horizon-client/src/game/sculk_fixture.rs index 890e571b8..df376a7e3 100644 --- a/crates/horizon-client/src/game/sculk_fixture.rs +++ b/crates/horizon-client/src/game/sculk_fixture.rs @@ -433,6 +433,7 @@ impl Game<'_> { }); self.level_events.clear(); self.sculk.known = self.world.chunks.keys().copied().collect(); + self.sculk.known_revision = Some(self.world.chunks.membership_revision()); self.sculk_sync_index(); let mut names: HashMap = HashMap::new(); let mut sources: Vec = Vec::new(); From 186777110f6c7996889726d90d02a80f8f0cf9fa Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 22 Oct 2026 00:22:26 +0000 Subject: [PATCH 10/29] Hoppers: skip the item scan when no item reaches the hopper's column HopperBlockEntity.suckInItems scanned every item entity for each hopper that ticked without a container above. During the block-entity pass the game now keeps the set of block columns item boxes reach; a hopper whose column is not in it has nothing to suck, and the scan (same order, same item taken) runs only for the rest. Items do not move within the pass: removals leave a superset, and additions (pushed at the end with a new id) or removing the last item rebuild the set. It is dropped at the end of the pass. 500 hoppers, 200 items: 1.36 -> 0.05 ms per tick 2000 hoppers, 500 items: 13.52 -> 0.26 ms per tick 2000 hoppers, 2000 items: 60.6 -> 3.3 ms per tick Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01UxXpf7d4RgpespL2G2UsGE --- crates/horizon-client/src/game.rs | 3 + .../src/game/block_entity_ticks.rs | 3 + crates/horizon-client/src/game/machines.rs | 164 ++++++++++++++++++ 3 files changed, 170 insertions(+) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 56d37a3af..c87875e4d 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -1393,6 +1393,8 @@ struct Game<'a> { arrow_model: Option, tnts: Vec, hoppers: HashMap<(i32, i32, i32), machines::HopperState>, + /// Where item entities are, while the block entities tick (hoppers). + item_columns: Option, creative_screen: Creative, control: Option, frame_stats: horizon_client::control::FrameStats, @@ -11381,6 +11383,7 @@ impl<'a> Game<'a> { arrow_model: prepared.arrow_model, tnts: Vec::new(), hoppers: HashMap::new(), + item_columns: None, creative_screen: prepared.creative_screen, control: None, frame_stats: Default::default(), diff --git a/crates/horizon-client/src/game/block_entity_ticks.rs b/crates/horizon-client/src/game/block_entity_ticks.rs index bb1cadd15..094aed6ef 100644 --- a/crates/horizon-client/src/game/block_entity_ticks.rs +++ b/crates/horizon-client/src/game/block_entity_ticks.rs @@ -24,6 +24,7 @@ impl Game<'_> { let ticking = self.block_ticking_area(); let mut spawners = Vec::new(); let mut trial_spawners = Vec::new(); + self.item_columns = Some(Default::default()); for slot in self.world.block_entity_tickers() { let pos = slot.0; // Ticks before this one can remove it (`TickingBlockEntity.isRemoved`). @@ -56,6 +57,8 @@ impl Game<'_> { _ => {} } } + // Items move between passes: the columns hold for this one only. + self.item_columns = None; self.update_song_volumes(); self.scan_for_hearts(); // The client ticks (particles, sounds, the turning mob) are for a diff --git a/crates/horizon-client/src/game/machines.rs b/crates/horizon-client/src/game/machines.rs index c05c7295f..8ce1940c9 100644 --- a/crates/horizon-client/src/game/machines.rs +++ b/crates/horizon-client/src/game/machines.rs @@ -327,6 +327,46 @@ impl Containers<'_> { } } +/// The block columns any item entity's box reaches, so that a hopper with +/// no item over it skips the scan of every item. Kept for one block-entity +/// pass, in which items do not move: removing one leaves a superset, still +/// right, and adding one (always pushed at the end, under a new id) or +/// removing the last rebuilds the columns. +#[derive(Default)] +pub(super) struct ItemColumns { + /// The item count and last item id when last asked, if built. + seen: Option<(usize, Option)>, + cells: horizon_client::fast_hash::FxHashSet<(i32, i32)>, +} +impl ItemColumns { + /// Whether an item's box may reach into column (x, z). + fn may_reach( + &mut self, + items: &[horizon_client::entities::ItemEntity], + x: i32, + z: i32, + ) -> bool { + let (len, last) = (items.len(), items.last().map(|i| i.id)); + let stale = self + .seen + .is_none_or(|(seen_len, seen_last)| len > seen_len || last != seen_last); + self.seen = Some((len, last)); + if stale { + self.cells.clear(); + for item in items { + let b = item.bounds(); + let (lo, hi) = (b.min_position(), b.max_position()); + for cx in lo.x.floor() as i32..=hi.x.floor() as i32 { + for cz in lo.z.floor() as i32..=hi.z.floor() as i32 { + self.cells.insert((cx, cz)); + } + } + } + } + self.cells.contains(&(x, z)) + } +} + fn bone_meal() -> ItemStack { ItemStack { item: "minecraft:bone_meal".into(), @@ -648,6 +688,11 @@ impl Game<'_> { f64::from(pos.2) + 1.0, ], ); + if let Some(columns) = self.item_columns.as_mut() + && !columns.may_reach(&self.dropped, pos.0, pos.2) + { + return false; + } let mut i = 0; while i < self.dropped.len() { let b = self.dropped[i].bounds(); @@ -880,3 +925,122 @@ impl Game<'_> { true } } + +#[cfg(test)] +mod item_column_tests { + use super::ItemColumns; + use horizon_client::{entities::ItemEntity, items::ItemStack}; + use horizon_core::geometry::Vec3; + + fn item(id: u32, x: f64, y: f64, z: f64) -> ItemEntity { + let at = Vec3::new(x, y, z); + ItemEntity { + health: 5, + fire_ticks: 0, + id, + stack: ItemStack::new("minecraft:stone", 1), + thrower: None, + position: at, + previous: at, + velocity: Vec3::ZERO, + on_ground: true, + age: 0, + pickup_delay: 0, + bob_offset: 0.0, + ticks: 0, + } + } + /// `hopper_suck`'s test: some item box meets the hopper's SUCK_AABB. + fn scan(items: &[ItemEntity], (x, y, z): (i32, i32, i32)) -> bool { + let (min, max) = ( + Vec3::new(f64::from(x), f64::from(y) + 11.0 / 16.0, f64::from(z)), + Vec3::new(f64::from(x) + 1.0, f64::from(y) + 2.0, f64::from(z) + 1.0), + ); + items.iter().any(|i| { + let b = i.bounds(); + let (lo, hi) = (b.min_position(), b.max_position()); + lo.x < max.x + && hi.x > min.x + && lo.y < max.y + && hi.y > min.y + && lo.z < max.z + && hi.z > min.z + }) + } + + /// One tick of empty hoppers looking for items: each scans every item + /// (before) or asks the columns first (after, build included). + #[test] + #[ignore] + fn hopper_suck_cost() { + for (hoppers, count) in [(500i32, 200u32), (2000, 500), (2000, 2000)] { + let mut seed = 99u64; + 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 items: Vec = (0..count) + .map(|id| item(id, next(160.0), 64.0, next(160.0))) + .collect(); + let at: Vec<(i32, i32, i32)> = (0..hoppers) + .map(|_| (next(160.0) as i32, 63, next(160.0) as i32)) + .collect(); + let t = std::time::Instant::now(); + let a: Vec = at.iter().map(|&h| scan(&items, h)).collect(); + let before = t.elapsed().as_secs_f64() * 1e3; + let t = std::time::Instant::now(); + let mut columns = ItemColumns::default(); + let b: Vec = at + .iter() + .map(|&h| columns.may_reach(&items, h.0, h.2) && scan(&items, h)) + .collect(); + let after = t.elapsed().as_secs_f64() * 1e3; + assert_eq!(a, b); + println!("{hoppers} hoppers, {count} items: {before:.3} ms -> {after:.3} ms"); + } + } + + #[test] + fn no_hopper_with_an_item_over_it_is_skipped() { + let mut seed = 0x1234_5678_9abc_def1u64; + 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 items: Vec = Vec::new(); + let mut columns = ItemColumns::default(); + let mut next_id = 0; + let mut skipped = 0; + for round in 0..3000 { + // Items come and go as hoppers take them and crafters drop them. + match round % 5 { + 0 | 1 => { + next_id += 1; + // On edges too: a quarter of them sit on a block line. + let snap = |v: f64, edge: bool| if edge { v.floor() + 0.125 } else { v }; + let edge = round % 4 == 0; + items.push(item( + next_id, + snap(next(24.0) - 12.0, edge), + 64.0, + snap(next(24.0) - 12.0, edge), + )); + } + 2 if !items.is_empty() => { + let i = next(items.len() as f64) as usize; + items.remove(i); + } + _ => {} + } + let hopper = (next(26.0) as i32 - 13, 63, next(26.0) as i32 - 13); + let reach = columns.may_reach(&items, hopper.0, hopper.2); + let found = scan(&items, hopper); + assert!( + reach || !found, + "round {round}: skipped a hopper with an item over it" + ); + skipped += usize::from(!reach); + } + assert!(skipped > 1000, "skipped only {skipped}"); + } +} From 9019d708d0b49df09945213fe6e33246aa8b2dc0 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 21 Oct 2026 23:10:14 +0000 Subject: [PATCH 11/29] Streaming: answer "near a player?" from a grid of the players' squares On a server every player is an extra area, and streaming asks of every loaded and wanted column, several times a tick (unloading, dropping protos, finishing, decoration, the generation scan), whether one of those squares reaches it: a scan of every player's square per question. The squares are now one grid of distances, made again when a player changes chunk, and in_extra reads it. Differential test against the scan over 60 random layouts, every margin. At 50 players, 6081 columns, 3 questions each: 0.45 ms -> 0.04 ms, plus 0.02 ms to make the grid. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01UxXpf7d4RgpespL2G2UsGE --- crates/horizon-client/src/streaming.rs | 235 +++++++++++++++++++++++++ 1 file changed, 235 insertions(+) diff --git a/crates/horizon-client/src/streaming.rs b/crates/horizon-client/src/streaming.rs index 858d05d30..49d1a052b 100644 --- a/crates/horizon-client/src/streaming.rs +++ b/crates/horizon-client/src/streaming.rs @@ -276,6 +276,8 @@ pub struct Streaming { /// chunks finish out to `radius + 1` (Chebyshev) and generation reaches /// past that as around the player. They are generated and kept but not meshed. pub extra: Vec<((i32, i32), i32)>, + /// `extra`'s squares as a grid, made again when `extra` changes. + cover: Option, /// Mesh the sections for drawing (false on a server thread). pub mesh: bool, } @@ -391,6 +393,7 @@ impl GenerationScan { radius: input.ranges.radius, extra: input.ranges.extra, forced: input.ranges.forced, + cover: input.ranges.cover, }; self.remember(ranges, input.world); } else { @@ -562,6 +565,7 @@ impl Streaming { meshed_total: 0, remote: false, extra: Vec::new(), + cover: None, mesh: true, } } @@ -808,6 +812,7 @@ impl Streaming { if self.paused { return (uploads, Vec::new()); } + ExtraCover::refresh(&mut self.cover, &self.extra); self.relight_edits(world); let deferred = std::mem::take(&mut self.deferred); let received = std::iter::from_fn(|| self.results.try_recv().ok()); @@ -982,6 +987,7 @@ impl Streaming { radius: self.radius, extra: &self.extra, forced: &self.forced, + cover: ExtraCover::matching(&self.cover, &self.extra), }; let (protos, generating) = (&self.protos, &self.generating); self.decoration_order.retain(|x, z| { @@ -1109,6 +1115,7 @@ impl Streaming { radius: self.radius, extra: &self.extra, forced: &self.forced, + cover: ExtraCover::matching(&self.cover, &self.extra), }; self.waiting.retain(|&(x, sy, z)| { let back = ranges.in_view(x, z); @@ -1185,6 +1192,7 @@ impl Streaming { radius: self.radius, extra: &self.extra, forced: &self.forced, + cover: ExtraCover::matching(&self.cover, &self.extra), }, world, generating: &self.generating, @@ -1280,6 +1288,7 @@ impl Streaming { radius: self.radius, extra: &self.extra, forced: &self.forced, + cover: ExtraCover::matching(&self.cover, &self.extra), } } fn near_finished(&self, x: i32, z: i32, reach: i32) -> bool { @@ -1365,6 +1374,99 @@ struct Ranges<'a> { radius: i32, extra: &'a [((i32, i32), i32)], forced: &'a HashSet<(i32, i32)>, + /// `extra` as a grid, when one was made for it: `in_extra` without a + /// scan of every area. + cover: Option<&'a ExtraCover>, +} +/// For every column near the extra areas, how far it lies outside the +/// nearest one's square (Chebyshev distance less its radius; 0 inside). A server has an area per player, and streaming asks of every +/// loaded and wanted column whether one reaches it, several times a tick: +/// with fifty players, a scan of fifty squares per question. +struct ExtraCover { + extra: Vec<((i32, i32), i32)>, + x0: i32, + z0: i32, + width: u32, + depth: u32, + cells: Box<[i8]>, +} +impl ExtraCover { + /// The largest margin the grid answers for; beyond it (and outside the + /// grid) a column is further than that from every square. + const REACH: i32 = 4; + /// The largest grid, in columns (a mebibyte). + const CELLS: u64 = 1 << 20; + fn matching<'a>( + cover: &'a Option, + extra: &[((i32, i32), i32)], + ) -> Option<&'a ExtraCover> { + cover.as_ref().filter(|c| c.extra == extra) + } + /// Make the grid again if `extra` changed; none for one area or none + /// (a scan is as quick), or areas too far apart. + fn refresh(cover: &mut Option, extra: &[((i32, i32), i32)]) { + if cover.as_ref().is_some_and(|c| c.extra == extra) { + return; + } + *cover = Self::new(extra); + } + fn new(extra: &[((i32, i32), i32)]) -> Option { + if extra.len() < 2 || extra.iter().any(|&(_, r)| !(0..=64).contains(&r)) { + return None; + } + let k = i64::from(Self::REACH); + let (mut x0, mut z0, mut x1, mut z1) = (i64::MAX, i64::MAX, i64::MIN, i64::MIN); + for &((x, z), r) in extra { + let (x, z, r) = (i64::from(x), i64::from(z), i64::from(r) + k); + (x0, z0, x1, z1) = (x0.min(x - r), z0.min(z - r), x1.max(x + r), z1.max(z + r)); + } + let (width, depth) = ((x1 - x0 + 1) as u64, (z1 - z0 + 1) as u64); + if width * depth > Self::CELLS || x0 < i64::from(i32::MIN) || x1 > i64::from(i32::MAX) { + return None; + } + let (x0, z0, width, depth) = (x0 as i32, z0 as i32, width as u32, depth as u32); + let mut cells = vec![(Self::REACH + 1) as i8; (width * depth) as usize].into_boxed_slice(); + // Inside a square counts as 0: no margin asked for is below it. Each + // row of a square is a run of one value with a slope at either end. + let lower = |run: &mut [i8], v: i8| run.iter_mut().for_each(|c| *c = (*c).min(v)); + for &((cx, cz), r) in extra { + let reach = r + Self::REACH; + for dz in -reach..=reach { + let start = (cz + dz - z0) as usize * width as usize + (cx - x0) as usize; + let row = &mut cells[start - reach as usize..=start + reach as usize]; + let flat = dz.abs().max(r); + let centre = reach as usize; + lower( + &mut row[centre - flat as usize..=centre + flat as usize], + (flat - r) as i8, + ); + for d in flat + 1..=reach { + let v = (d - r) as i8; + lower(&mut row[centre - d as usize..=centre - d as usize], v); + lower(&mut row[centre + d as usize..=centre + d as usize], v); + } + } + } + Some(Self { + extra: extra.to_vec(), + x0, + z0, + width, + depth, + cells, + }) + } + /// Whether (x, z) is within `margin` of an area's square, for a + /// `margin` up to `REACH`. + fn within(&self, x: i32, z: i32, margin: i32) -> bool { + let (dx, dz) = ( + x.wrapping_sub(self.x0) as u32, + z.wrapping_sub(self.z0) as u32, + ); + dx < self.width + && dz < self.depth + && i32::from(self.cells[(dz * self.width + dx) as usize]) <= margin + } } impl Ranges<'_> { /// `distance`, or `rings` while the level loading screen is up. @@ -1441,6 +1543,11 @@ impl Ranges<'_> { /// Within `margin` of an extra area's square (Chebyshev distance, as /// vanilla's ticket levels spread). fn in_extra(&self, x: i32, z: i32, margin: i32) -> bool { + if let Some(cover) = self.cover + && (0..=ExtraCover::REACH).contains(&margin) + { + return cover.within(x, z, margin); + } self.extra .iter() .any(|&(c, radius)| (x - c.0).abs().max((z - c.1).abs()) <= radius + margin) @@ -1554,6 +1661,7 @@ mod tests { radius: self.radius, extra: &self.extra, forced: &self.forced, + cover: None, }, world: &self.world, generating: &self.generating, @@ -1866,6 +1974,7 @@ mod tests { radius, extra: &[], forced: &forced, + cover: None, }; let square = |x: i32, z: i32, n: i32| { (-n..=n).flat_map(move |dz| (-n..=n).map(move |dx| (x + dx, z + dz))) @@ -1920,6 +2029,7 @@ mod tests { radius: 12, extra: &[], forced: &forced, + cover: None, }; let at = |dx: i32, dz: i32| (2 + dx, -1 + dz); let key = |(x, z): (i32, i32), loading| r.priority(x, z, loading); @@ -1945,6 +2055,7 @@ mod tests { radius, extra: &[((radius * 2 + 9, 3), 4)], forced: &forced, + cover: None, }; let span = radius * 3 + 20; for x in -span..=span { @@ -1984,6 +2095,7 @@ mod tests { radius: 2, extra: &extra, forced: &forced, + cover: None, }; let level = |x: i32, z: i32| 31 + ((x - 40).abs().max((z + 9).abs()) - view).max(0); for x in 40 - view - 8..=40 + view + 8 { @@ -2021,3 +2133,126 @@ mod tests { } } } + +#[cfg(test)] +mod cover_tests { + use super::*; + + fn areas(seed: &mut u64, n: usize, spread: i32) -> Vec<((i32, i32), i32)> { + let mut next = |m: i32| { + *seed ^= *seed << 13; + *seed ^= *seed >> 7; + *seed ^= *seed << 17; + (*seed % (2 * m as u64 + 1)) as i32 - m + }; + (0..n) + .map(|_| ((next(spread), next(spread)), 2 + next(14).abs())) + .collect() + } + + /// Every question streaming asks gets the same answer with the grid. + #[test] + fn the_grid_answers_as_the_scan() { + let mut seed = 0x9E37_79B9_7F4A_7C15u64; + let forced: HashSet<(i32, i32)> = [(40, -40)].into(); + for case in 0..60 { + let extra = areas(&mut seed, 2 + case % 20, [4, 30, 120][case % 3]); + let cover = ExtraCover::new(&extra).expect("a grid"); + let ranges = |cover| Ranges { + center: (3, -2), + radius: 10, + extra: &extra, + forced: &forced, + cover, + }; + let (scan, grid) = (ranges(None), ranges(Some(&cover))); + let span = [4, 30, 120][case % 3] + 40; + for z in -span..=span { + for x in -span..=span { + for m in 0..=ExtraCover::REACH + 2 { + assert_eq!( + scan.in_extra(x, z, m), + grid.in_extra(x, z, m), + "case {case} ({x}, {z}) {m}" + ); + } + for d in [false, true] { + assert_eq!(scan.unloaded(x, z, d), grid.unloaded(x, z, d)); + assert_eq!(scan.requested(x, z, d), grid.requested(x, z, d)); + assert_eq!(scan.chunk_kept(x, z, d), grid.chunk_kept(x, z, d)); + } + } + } + } + } + + #[test] + fn a_stale_or_impossible_grid_is_not_used() { + let mut cover = None; + ExtraCover::refresh(&mut cover, &[((0, 0), 11), ((5, 5), 11)]); + assert!(ExtraCover::matching(&cover, &[((0, 0), 11), ((5, 5), 11)]).is_some()); + assert!(ExtraCover::matching(&cover, &[((0, 0), 11), ((5, 6), 11)]).is_none()); + // Too far apart for a mebibyte of grid, and one area alone: scans. + assert!(ExtraCover::new(&[((0, 0), 11), ((100_000, 0), 11)]).is_none()); + assert!(ExtraCover::new(&[((0, 0), 11)]).is_none()); + assert!(ExtraCover::new(&[((i32::MAX - 1, 0), 11), ((i32::MAX - 3, 0), 11)]).is_none()); + } + + /// cargo test --release -p horizon-client --lib cover_tests::cover_cost -- --ignored --nocapture + #[test] + #[ignore] + fn cover_cost() { + let forced = HashSet::new(); + for players in [10usize, 25, 50] { + // The benchmark's bots: most near spawn, a fifth flying outwards. + let extra: Vec<_> = (0..players) + .map(|i| { + let a = i as f64 * 2.399; + let d = if i % 5 == 0 { 30.0 } else { 3.0 }; + (((a.cos() * d) as i32, (a.sin() * d) as i32), 11) + }) + .collect(); + let cover = ExtraCover::new(&extra).unwrap(); + let ranges = |cover| Ranges { + center: (0, 0), + radius: 10, + extra: &extra, + forced: &forced, + cover, + }; + // The columns streaming asks about: everything kept, and the + // generation scan's ring past it. + let columns: Vec<(i32, i32)> = (-60..=60) + .flat_map(|z| (-60..=60).map(move |x| (x, z))) + .filter(|&(x, z)| { + ranges(None).requested(x, z, true) || !ranges(None).unloaded(x, z, true) + }) + .collect(); + let rounds = 100; + let time = |r: &Ranges| { + let t = std::time::Instant::now(); + let mut n = 0usize; + for _ in 0..rounds { + for &(x, z) in &columns { + n += usize::from(r.unloaded(x, z, true)); + n += usize::from(r.requested(x, z, true)); + n += usize::from(r.in_range(x, z, 1)); + } + } + std::hint::black_box(n); + t.elapsed().as_secs_f64() * 1e3 / f64::from(rounds) + }; + let t = std::time::Instant::now(); + for _ in 0..rounds { + std::hint::black_box(ExtraCover::new(std::hint::black_box(&extra))); + } + let build = t.elapsed().as_secs_f64() * 1e3 / f64::from(rounds); + println!( + "{players} players, {} columns, 3 questions each: scan {:.3} ms, grid {:.3} ms (+ {build:.3} ms to build)", + columns.len(), + time(&ranges(None)), + time(&ranges(Some(&cover))), + ); + } + } +} From dfe795bc090809090993c44c2a032f108efbf3f4 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 22 Oct 2026 02:42:12 +0000 Subject: [PATCH 12/29] Faster Perlin volumes and climate search, same results PerlinNoise::add_volume worked out each Y coordinate's cell, offset and fade once per X/Z column. Every column walks the same Y coordinates, so they are now computed once per volume. The arithmetic is unchanged, so the output is bit for bit the same. ClimateIndex kept its tree as nodes with a Vec of child ids each. The built tree is now renumbered breadth first into flat arrays, so a branch's children are one contiguous run. The search visits the same nodes in the same order, so ties resolve exactly as before. terrain_bench (seed 12345, 8x8 chunks, 1 thread, medians of 3 runs): terrain 14.5 -> 12.4 ms/chunk, biomes 1.20 -> 1.06 ms/chunk. Checked against vanilla 26.3 in-process: 2304 chunks on seeds 12345, -1 and 987654321987 identical, and the biome-source and biome-generation parity suites (2.1M lines) match. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01M3pNrSHA2ifNrH7SAMHgSW --- crates/horizon-core/src/noise/perlin.rs | 58 ++++++++++++----- .../src/worldgen/climate/index.rs | 65 ++++++++++++------- 2 files changed, 84 insertions(+), 39 deletions(-) diff --git a/crates/horizon-core/src/noise/perlin.rs b/crates/horizon-core/src/noise/perlin.rs index 725373eff..92fa2a880 100644 --- a/crates/horizon-core/src/noise/perlin.rs +++ b/crates/horizon-core/src/noise/perlin.rs @@ -164,6 +164,31 @@ impl PerlinNoise { return Err(DensityError::BufferTooShort); } let [size_x, size_y, size_z] = volume.sizes(); + // Every X/Z column walks the same Y coordinates, so their cells, offsets + // and fades are computed once per volume instead of once per column. + let mut stack = [YSample::default(); STACK_Y]; + let mut heap = Vec::new(); + let ys = if size_y <= STACK_Y { + &mut stack[..size_y] + } else { + heap.resize(size_y, YSample::default()); + &mut heap[..] + }; + for (y, sample) in ys.iter_mut().enumerate() { + let input_y = f64::from(volume.coordinate(1, y)) * y_scale; + let shifted_y = wrap(input_y) + self.table.offsets[1]; + let cell = floor_i32(shifted_y); + let dy = shifted_y - f64::from(cell); + *sample = YSample { + cell, + fade: smoothstep(dy as f32), + offset: match fudge_y_scale { + Some(scale) => (dy - compute_fudge_y(input_y, dy, scale)) as f32, + None => dy as f32, + }, + }; + } + let ys = &*ys; let mut index = 0; for z in 0..size_z { let shifted_z = @@ -182,37 +207,40 @@ impl PerlinNoise { let mut previous_y = i32::MIN; let mut xz_dots = [0.0_f32; 8]; let mut y_gradients = [0.0_f32; 8]; - for y in 0..size_y { - let input_y = f64::from(volume.coordinate(1, y)) * y_scale; - let shifted_y = wrap(input_y) + self.table.offsets[1]; - let cell_y = floor_i32(shifted_y); - let dy = shifted_y - f64::from(cell_y); - let fade_y = smoothstep(dy as f32); - if previous_y != cell_y { - let gradients = self.gradients_at(p, q, cell_y, cell_z); + for (sample, out) in ys.iter().zip(&mut buffer[index..index + size_y]) { + if previous_y != sample.cell { + let gradients = self.gradients_at(p, q, sample.cell, cell_z); for i in 0..8 { xz_dots[i] = gradients[i][0] as f32 * (dx - (i & 1) as f32) + gradients[i][2] as f32 * (dz - ((i >> 2) & 1) as f32); y_gradients[i] = gradients[i][1] as f32; } - previous_y = cell_y; + previous_y = sample.cell; } - let dy = match fudge_y_scale { - Some(scale) => (dy - compute_fudge_y(input_y, dy, scale)) as f32, - None => dy as f32, - }; + let dy = sample.offset; let values = std::array::from_fn(|i| { xz_dots[i] + y_gradients[i] * (dy - ((i >> 1) & 1) as f32) }); - buffer[index] += amplitude * lerp3([fade_x, fade_y, fade_z], values); - index += 1; + *out += amplitude * lerp3([fade_x, sample.fade, fade_z], values); } + index += size_y; } } Ok(()) } } +/// Y columns up to this height keep their per-Y samples on the stack. +const STACK_Y: usize = 64; + +#[derive(Clone, Copy, Default)] +struct YSample { + cell: i32, + fade: f32, + /// The Y offset inside the cell used for the corner values, after any fudge. + offset: f32, +} + #[derive(Clone, Debug)] pub struct SmearedPerlinNoise { base: PerlinNoise, diff --git a/crates/horizon-world/src/worldgen/climate/index.rs b/crates/horizon-world/src/worldgen/climate/index.rs index 3e9b50f51..6a602ed92 100644 --- a/crates/horizon-world/src/worldgen/climate/index.rs +++ b/crates/horizon-world/src/worldgen/climate/index.rs @@ -202,8 +202,11 @@ fn build( #[derive(Debug)] pub struct ClimateIndex { entries: Vec<(ParameterPoint, T)>, - nodes: Vec, - root: usize, + /// The built tree renumbered breadth first, root at 0, so each branch's + /// children are one contiguous run and a search walks memory in order. + spaces: Vec<[Parameter; 7]>, + /// Per node: its first child and child count, or its entry and 0 for a leaf. + links: Vec<(u32, u32)>, } impl ClimateIndex { pub fn new(entries: Vec<(ParameterPoint, T)>) -> Result { @@ -232,10 +235,25 @@ impl ClimateIndex { .collect(); let children = (0..nodes.len()).collect(); let root = build(&mut nodes, children, branching)?; + let mut order = vec![root]; + let mut links = Vec::with_capacity(nodes.len()); + let mut next = 0; + while next < order.len() { + let node = &nodes[order[next]]; + next += 1; + links.push(match &node.children { + Children::Leaf(entry) => (*entry as u32, 0), + Children::Branch(children) => { + let first = order.len() as u32; + order.extend_from_slice(children); + (first, children.len() as u32) + } + }); + } Ok(Self { entries, - nodes, - root, + spaces: order.iter().map(|id| nodes[*id].space).collect(), + links, }) } pub fn entries(&self) -> &[(ParameterPoint, T)] { @@ -263,18 +281,18 @@ impl ClimateIndex { } /// Preorder leaf entry indices expose stable tree layout for comparison. pub fn leaf_order(&self) -> Vec { - fn walk(nodes: &[Node], id: usize, out: &mut Vec) { - match &nodes[id].children { - Children::Leaf(entry) => out.push(*entry), - Children::Branch(children) => { - for child in children { - walk(nodes, *child, out); + fn walk(links: &[(u32, u32)], id: usize, out: &mut Vec) { + match links[id] { + (entry, 0) => out.push(entry as usize), + (first, count) => { + for child in first..first + count { + walk(links, child as usize, out); } } } } let mut result = Vec::with_capacity(self.entries.len()); - walk(&self.nodes, self.root, &mut result); + walk(&self.links, 0, &mut result); result } fn search( @@ -283,19 +301,20 @@ impl ClimateIndex { values: &[i64; 7], previous: Option, ) -> Result { - let Children::Branch(children) = &self.nodes[id].children else { + let (first, count) = self.links[id]; + if count == 0 { return Ok(id); - }; + } let mut best = previous; - let mut fitness = previous.map_or(i64::MAX, |id| distance(&self.nodes[id].space, values)); - for child in children { - let lower = distance(&self.nodes[*child].space, values); + let mut fitness = previous.map_or(i64::MAX, |id| distance(&self.spaces[id], values)); + for child in first as usize..(first + count) as usize { + let lower = distance(&self.spaces[child], values); if fitness > lower { - let result = self.search(*child, values, best)?; - let next = if result == *child { + let result = self.search(child, values, best)?; + let next = if result == child { lower } else { - distance(&self.nodes[result].space, values) + distance(&self.spaces[result], values) }; if fitness > next { fitness = next; @@ -313,13 +332,11 @@ pub struct ClimateSearch<'a, T> { } impl<'a, T> ClimateSearch<'a, T> { pub fn find(&mut self, target: TargetPoint) -> Result<&'a T, IndexError> { - let result = self - .index - .search(self.index.root, &target.parameters(), self.last)?; + let result = self.index.search(0, &target.parameters(), self.last)?; self.last = Some(result); - let Children::Leaf(entry) = self.index.nodes[result].children else { + let (entry, 0) = self.index.links[result] else { unreachable!("search returns a leaf"); }; - Ok(&self.index.entries[entry].1) + Ok(&self.index.entries[entry as usize].1) } } From bc9e5afacce9cea888472491a83f57660c224dcf Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 22 Oct 2026 06:26:11 +0000 Subject: [PATCH 13/29] Faster feature decoration: region grid, ore column fast path, lazy biome cache TerrainRegion indexes its chunks through a grid over their bounding box. OreFeature asks the world whether any column under its blob reaches the floor heightmap in one call, and tracks tested positions in a bitset. BiomeTemperature no longer preallocates its map and queue. Same output. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01M3pNrSHA2ifNrH7SAMHgSW --- .../src/worldgen/biome_temperature.rs | 6 +- .../src/worldgen/feature_region.rs | 76 +++++++++++++------ .../src/worldgen/generator_decoration.rs | 37 +++++++++ .../horizon-world/src/worldgen/ore_feature.rs | 33 ++++++-- .../src/worldgen/placement/context.rs | 12 +++ 5 files changed, 133 insertions(+), 31 deletions(-) diff --git a/crates/horizon-world/src/worldgen/biome_temperature.rs b/crates/horizon-world/src/worldgen/biome_temperature.rs index c7e22c985..7111214f4 100644 --- a/crates/horizon-world/src/worldgen/biome_temperature.rs +++ b/crates/horizon-world/src/worldgen/biome_temperature.rs @@ -123,10 +123,12 @@ pub struct BiomeTemperature { } impl BiomeTemperature { pub fn new(settings: BiomeTemperatureSettings) -> Self { + // Empty until used: a decoration region makes one per biome, and + // most biomes never ask for a temperature. Self { settings, - values: HashMap::with_capacity(1024), - order: VecDeque::with_capacity(1024), + values: HashMap::new(), + order: VecDeque::new(), } } pub fn settings(&self) -> BiomeTemperatureSettings { diff --git a/crates/horizon-world/src/worldgen/feature_region.rs b/crates/horizon-world/src/worldgen/feature_region.rs index 5ab361985..91de61905 100644 --- a/crates/horizon-world/src/worldgen/feature_region.rs +++ b/crates/horizon-world/src/worldgen/feature_region.rs @@ -58,7 +58,13 @@ impl From for PlacementQueryError { } } pub struct TerrainRegion<'r, B: TerrainBlockStates> { - chunks: BTreeMap<(i32, i32), TerrainChunk<'r, B>>, + /// Ordered by X then Z. + chunks: Vec>, + /// Each chunk's index in `chunks` over the chunks' bounding box, row by + /// row from `origin` (`u32::MAX`: none): every block access looks one up. + grid: Vec, + origin: (i32, i32), + columns: i32, min_y: i32, height: i32, generation: WorldGenerationContext, @@ -83,15 +89,32 @@ impl<'r, B: TerrainBlockStates> TerrainRegion<'r, B> { }) { return Err(RegionError::InvalidChunks); } - let mut stored = BTreeMap::new(); - for chunk in chunks { + let mut chunks = chunks; + chunks.sort_by_key(|c| (c.position().x, c.position().z)); + let (xs, zs) = ( + chunks.iter().map(|c| c.position().x), + chunks.iter().map(|c| c.position().z), + ); + let origin = (xs.clone().min().unwrap(), zs.clone().min().unwrap()); + let columns = i64::from(xs.max().unwrap()) - i64::from(origin.0) + 1; + let rows = i64::from(zs.max().unwrap()) - i64::from(origin.1) + 1; + if columns * rows > 1 << 20 { + return Err(RegionError::InvalidChunks); + } + let mut grid = vec![u32::MAX; (columns * rows) as usize]; + for (i, chunk) in chunks.iter().enumerate() { let p = chunk.position(); - if stored.insert((p.x, p.z), chunk).is_some() { + let cell = (p.z - origin.1) as usize * columns as usize + (p.x - origin.0) as usize; + if grid[cell] != u32::MAX { return Err(RegionError::InvalidChunks); } + grid[cell] = i as u32; } Ok(Self { - chunks: stored, + chunks, + grid, + origin, + columns: columns as i32, sea_level: generator.sea_level, game_time: 0, next_tick_order: 0, @@ -114,10 +137,7 @@ impl<'r, B: TerrainBlockStates> TerrainRegion<'r, B> { mut self, bindings: &super::block_feature::FeatureBlockBindings<'r>, ) -> Result { - if !std::ptr::eq( - self.chunks.first_key_value().unwrap().1.registry(), - bindings.registry(), - ) { + if !std::ptr::eq(self.chunks[0].registry(), bindings.registry()) { return Err(RegionError::InvalidBindings); } self.block_queries = Some(RegionBlockQueries { @@ -186,20 +206,35 @@ impl<'r, B: TerrainBlockStates> TerrainRegion<'r, B> { } /// Release the resident chunks, ordered by X then Z; match them by position. pub fn into_chunks(self) -> Vec> { - self.chunks.into_values().collect() + self.chunks } pub fn chunks(&self) -> impl Iterator> { - self.chunks.values() + self.chunks.iter() + } + fn index(&self, x: i32, z: i32) -> Option { + let (dx, dz) = (x.wrapping_sub(self.origin.0), z.wrapping_sub(self.origin.1)); + if dx < 0 || dz < 0 || dx >= self.columns { + return None; + } + match *self + .grid + .get(dz as usize * self.columns as usize + dx as usize)? + { + u32::MAX => None, + i => Some(i as usize), + } } pub fn chunk(&self, x: i32, z: i32) -> Result<&TerrainChunk<'r, B>, RegionError> { - self.chunks - .get(&(x, z)) - .ok_or(RegionError::MissingChunk([x, z])) + match self.index(x, z) { + Some(i) => Ok(&self.chunks[i]), + None => Err(RegionError::MissingChunk([x, z])), + } } pub fn chunk_mut(&mut self, x: i32, z: i32) -> Result<&mut TerrainChunk<'r, B>, RegionError> { - self.chunks - .get_mut(&(x, z)) - .ok_or(RegionError::MissingChunk([x, z])) + match self.index(x, z) { + Some(i) => Ok(&mut self.chunks[i]), + None => Err(RegionError::MissingChunk([x, z])), + } } pub fn block(&self, [x, y, z]: [i32; 3]) -> Result { Ok(self.chunk(x >> 4, z >> 4)?.block(x, y, z)) @@ -302,12 +337,7 @@ impl PlacementContext for TerrainRegion<'_, B> { block: BlockStateId, delay: i32, ) -> Result<(), PlacementQueryError> { - let registry = self - .chunks - .first_key_value() - .expect("nonempty region") - .1 - .registry(); + let registry = self.chunks[0].registry(); let state = registry .state(block) .ok_or(RegionError::InvalidTickState(block))?; diff --git a/crates/horizon-world/src/worldgen/generator_decoration.rs b/crates/horizon-world/src/worldgen/generator_decoration.rs index bc5dbda70..a16baa506 100644 --- a/crates/horizon-world/src/worldgen/generator_decoration.rs +++ b/crates/horizon-world/src/worldgen/generator_decoration.rs @@ -1116,4 +1116,41 @@ impl PlacementContext for World<'_> { fn height(&mut self, kind: HeightmapType, x: i32, z: i32) -> Result { self.region.height(kind, x, z) } + /// OreFeature's search for a column under its blob, without a trait call + /// per column. Defers to `height` when a heightmap still has to be primed. + fn any_height_at_least( + &mut self, + kind: HeightmapType, + [x0, z0]: [i32; 2], + size: i32, + y: i32, + ) -> Result, PlacementQueryError> { + let (x1, z1) = (x0.wrapping_add(size), z0.wrapping_add(size)); + if size < 0 || x1 < x0 || z1 < z0 { + return Ok(None); + } + for cx in x0 >> 4..=x1 >> 4 { + for cz in z0 >> 4..=z1 >> 4 { + if !self + .region + .chunk(cx, cz) + .is_ok_and(|c| c.heightmap(kind).is_some()) + { + return Ok(None); + } + } + } + for x in x0..=x1 { + for z in z0..=z1 { + let map = self.region.chunk(x >> 4, z >> 4)?.heightmap(kind).unwrap(); + let floor = map + .first_available(x & 15, z & 15) + .map_err(|e| RegionError::from(super::terrain::TerrainError::from(e)))?; + if y <= floor { + return Ok(Some(true)); + } + } + } + Ok(Some(false)) + } } diff --git a/crates/horizon-world/src/worldgen/ore_feature.rs b/crates/horizon-world/src/worldgen/ore_feature.rs index 9d723ae40..d69874d76 100644 --- a/crates/horizon-world/src/worldgen/ore_feature.rs +++ b/crates/horizon-world/src/worldgen/ore_feature.rs @@ -180,6 +180,27 @@ impl OreFeature { let z_start = origin[2].wrapping_sub(spread_ceil).wrapping_sub(max_radius); let size_xz = 2 * (spread_ceil + max_radius); let size_y = 2 * (2 + max_radius); + if let Some(found) = world + .any_height_at_least( + HeightmapType::OceanFloorWg, + [x_start, z_start], + size_xz, + y_start, + ) + .map_err(FeatureExecutionError::Query)? + { + return match found { + true => self.do_place( + world, + random, + [x0, x1, z0, z1, y0, y1], + [x_start, y_start, z_start], + size_xz, + size_y, + ), + false => Ok(false), + }; + } for x in 0..=size_xz { for z in 0..=size_xz { let (px, pz) = (x_start.wrapping_add(x), z_start.wrapping_add(z)); @@ -243,7 +264,8 @@ impl OreFeature { } } } - let mut tested = vec![false; (size_xz as usize) * (size_y as usize) * (size_xz as usize)]; + let tested_len = (size_xz as usize) * (size_y as usize) * (size_xz as usize); + let mut tested = vec![0u64; tested_len.div_ceil(64)]; let mut placed = 0; for [xx, yy, zz, r] in data { if r < 0.0 { @@ -280,16 +302,15 @@ impl OreFeature { * i64::from(size_xz) * i64::from(size_y); // The sphere bounds keep indices inside the native BitSet. - let Some(bit) = usize::try_from(index) - .ok() - .and_then(|index| tested.get_mut(index)) + let Some(index) = usize::try_from(index).ok().filter(|&i| i < tested_len) else { return Err(FeatureExecutionError::WorkLimit); }; - if *bit { + let (word, bit) = (&mut tested[index / 64], 1u64 << (index % 64)); + if *word & bit != 0 { continue; } - *bit = true; + *word |= bit; let p = [x, y, z]; if !world .ensure_can_write(p) diff --git a/crates/horizon-world/src/worldgen/placement/context.rs b/crates/horizon-world/src/worldgen/placement/context.rs index 8d2a1dab7..73b9afd67 100644 --- a/crates/horizon-world/src/worldgen/placement/context.rs +++ b/crates/horizon-world/src/worldgen/placement/context.rs @@ -344,6 +344,18 @@ pub trait PlacementContext { /// World minimum, which can differ from the generator's minimum. fn min_y(&mut self) -> Result; fn height(&mut self, kind: HeightmapType, x: i32, z: i32) -> Result; + /// Whether any column of the square `x..=x + size`, `z..=z + size` has + /// `height(kind)` at least `y`, read faster than column by column; `None` + /// when the world can't (callers then ask `height` for each column). + fn any_height_at_least( + &mut self, + _kind: HeightmapType, + _corner: [i32; 2], + _size: i32, + _y: i32, + ) -> Result, PlacementQueryError> { + Ok(None) + } } impl PlacementContext for WorldGenerationContext { fn max_y(&mut self) -> Result { From 5bad717213c5119dd1d0bbfbf134ec44f6c9a6f2 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 22 Oct 2026 08:43:40 +0000 Subject: [PATCH 14/29] Perlin noise without libm floor or int-to-float conversions, same values floor_i32 is now Java's Mth.floor (truncate, step down for negative fractions): f64::floor is a libm call on baseline x86-64, made for every Perlin sample. Perlin's gradient table is stored as floats (every component is -1, 0 or 1, exact either way), so the corner dot products skip three conversions each. About 10% fewer instructions generating terrain; values unchanged. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01M3pNrSHA2ifNrH7SAMHgSW --- crates/horizon-core/src/geometry/mod.rs | 34 ++++++++++++++++++++++++- crates/horizon-core/src/noise/mod.rs | 24 ++++++++++++++--- crates/horizon-core/src/noise/perlin.rs | 14 +++++----- 3 files changed, 60 insertions(+), 12 deletions(-) diff --git a/crates/horizon-core/src/geometry/mod.rs b/crates/horizon-core/src/geometry/mod.rs index 950403ee8..9b581c044 100644 --- a/crates/horizon-core/src/geometry/mod.rs +++ b/crates/horizon-core/src/geometry/mod.rs @@ -602,8 +602,16 @@ pub(crate) fn clamp(value: f64, min: f64, max: f64) -> f64 { java_min(value, max) } } +/// `Mth.floor`: truncate, then step down for negative fractions. A truncating +/// conversion and a compare, where `f64::floor` is a libm call on baseline x86-64. +#[inline] pub(crate) fn floor_i32(value: f64) -> i32 { - value.floor() as i32 + let truncated = value as i32; + if value < f64::from(truncated) { + truncated.wrapping_sub(1) + } else { + truncated + } } fn lerp(amount: f64, start: f64, end: f64) -> f64 { start + amount * (end - start) @@ -635,6 +643,30 @@ fn next_down(value: f64) -> f64 { #[cfg(test)] mod tests { use super::*; + + #[test] + fn floor_i32_is_mths_floor() { + for v in [ + 0.0, + -0.0, + 0.5, + -0.5, + 1.0, + -1.0, + 2.999, + -2.999, + 1e-300, + -1e-300, + 16_777_215.5, + -16_777_215.5, + ] { + assert_eq!(floor_i32(v), v.floor() as i32, "{v}"); + } + assert_eq!(floor_i32(f64::NAN), 0); + assert_eq!(floor_i32(3e9), i32::MAX); + // Java's (int) cast saturates, then the step down wraps. + assert_eq!(floor_i32(-3e9), i32::MAX); + } #[test] fn ray_faces_and_half_open_bounds() { let b = Aabb::block(BlockPos::new(0, 0, 0)); diff --git a/crates/horizon-core/src/noise/mod.rs b/crates/horizon-core/src/noise/mod.rs index cb255fbe5..e715b10f0 100644 --- a/crates/horizon-core/src/noise/mod.rs +++ b/crates/horizon-core/src/noise/mod.rs @@ -61,8 +61,8 @@ impl GradientTable { i32::from(self.permutations[(index & 255) as usize]) } - fn gradient(&self, index: i32) -> [i32; 3] { - GRADIENTS[(self.permute(index) & 15) as usize] + fn gradient(&self, index: i32) -> [f32; 3] { + PERLIN_GRADIENTS[(self.permute(index) & 15) as usize] } } @@ -87,8 +87,24 @@ const GRADIENTS: [[i32; 3]; 16] = [ [0, -1, -1], ]; -fn dot(gradient: [i32; 3], point: [f32; 3]) -> f32 { - gradient[0] as f32 * point[0] + gradient[1] as f32 * point[1] + gradient[2] as f32 * point[2] +/// [`GRADIENTS`] as floats, so a dot product needs no conversions: every +/// component is -1, 0 or 1, which a float holds exactly. +const PERLIN_GRADIENTS: [[f32; 3]; 16] = { + let mut out = [[0.0; 3]; 16]; + let mut i = 0; + while i < 16 { + out[i] = [ + GRADIENTS[i][0] as f32, + GRADIENTS[i][1] as f32, + GRADIENTS[i][2] as f32, + ]; + i += 1; + } + out +}; + +fn dot(gradient: [f32; 3], point: [f32; 3]) -> f32 { + gradient[0] * point[0] + gradient[1] * point[1] + gradient[2] * point[2] } /// Vanilla's periodic coordinate reduction with the normal Far Lands flag off. diff --git a/crates/horizon-core/src/noise/perlin.rs b/crates/horizon-core/src/noise/perlin.rs index 725373eff..bdf5f792b 100644 --- a/crates/horizon-core/src/noise/perlin.rs +++ b/crates/horizon-core/src/noise/perlin.rs @@ -39,7 +39,7 @@ impl PerlinNoise { ) } - fn gradients(&self, cell: [i32; 3]) -> [[i32; 3]; 8] { + fn gradients(&self, cell: [i32; 3]) -> [[f32; 3]; 8] { self.gradients_at( self.table.permute(cell[0]), self.table.permute(cell[0].wrapping_add(1)), @@ -52,7 +52,7 @@ impl PerlinNoise { /// resolved. `add_volume` reuses one X column across every Y, so hoisting /// the two X permutes out of that loop avoids repeating them per Y. #[inline] - fn gradients_at(&self, p: i32, q: i32, cell_y: i32, cell_z: i32) -> [[i32; 3]; 8] { + fn gradients_at(&self, p: i32, q: i32, cell_y: i32, cell_z: i32) -> [[f32; 3]; 8] { let lower = self.table.permute(p.wrapping_add(cell_y)); let upper = self.table.permute(p.wrapping_add(cell_y).wrapping_add(1)); let lower_x = self.table.permute(q.wrapping_add(cell_y)); @@ -66,7 +66,7 @@ impl PerlinNoise { }) } - fn corner_values(gradients: [[i32; 3]; 8], point: [f32; 3]) -> [f32; 8] { + fn corner_values(gradients: [[f32; 3]; 8], point: [f32; 3]) -> [f32; 8] { std::array::from_fn(|i| { dot( gradients[i], @@ -99,7 +99,7 @@ impl PerlinNoise { let values = Self::corner_values(gradients, point); let fade = point.map(smoothstep); let gradient = - std::array::from_fn::<_, 3, _>(|axis| lerp3(fade, gradients.map(|g| g[axis] as f32))); + std::array::from_fn::<_, 3, _>(|axis| lerp3(fade, gradients.map(|g| g[axis]))); let changes = [ lerp2( fade[1], @@ -191,9 +191,9 @@ impl PerlinNoise { if previous_y != cell_y { let gradients = self.gradients_at(p, q, cell_y, cell_z); for i in 0..8 { - xz_dots[i] = gradients[i][0] as f32 * (dx - (i & 1) as f32) - + gradients[i][2] as f32 * (dz - ((i >> 2) & 1) as f32); - y_gradients[i] = gradients[i][1] as f32; + xz_dots[i] = gradients[i][0] * (dx - (i & 1) as f32) + + gradients[i][2] * (dz - ((i >> 2) & 1) as f32); + y_gradients[i] = gradients[i][1]; } previous_y = cell_y; } From 86e08072e44651634a581e20a689ceaf34e46601 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Thu, 22 Oct 2026 15:14:26 +0200 Subject: [PATCH 15/29] streaming: bound ExtraCover's z range too ExtraCover::new checked only x0/x1 against the i32 range, so two areas near i32::MAX or i32::MIN in z built a grid whose row arithmetic (cz + dz) overflows: a panic in debug builds, wrong cells in release. Check z the same way, and test both ends. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/streaming.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/crates/horizon-client/src/streaming.rs b/crates/horizon-client/src/streaming.rs index 49d1a052b..356614cb3 100644 --- a/crates/horizon-client/src/streaming.rs +++ b/crates/horizon-client/src/streaming.rs @@ -1421,7 +1421,8 @@ impl ExtraCover { (x0, z0, x1, z1) = (x0.min(x - r), z0.min(z - r), x1.max(x + r), z1.max(z + r)); } let (width, depth) = ((x1 - x0 + 1) as u64, (z1 - z0 + 1) as u64); - if width * depth > Self::CELLS || x0 < i64::from(i32::MIN) || x1 > i64::from(i32::MAX) { + let fits = |lo: i64, hi: i64| lo >= i64::from(i32::MIN) && hi <= i64::from(i32::MAX); + if width * depth > Self::CELLS || !fits(x0, x1) || !fits(z0, z1) { return None; } let (x0, z0, width, depth) = (x0 as i32, z0 as i32, width as u32, depth as u32); @@ -2196,6 +2197,8 @@ mod cover_tests { assert!(ExtraCover::new(&[((0, 0), 11), ((100_000, 0), 11)]).is_none()); assert!(ExtraCover::new(&[((0, 0), 11)]).is_none()); assert!(ExtraCover::new(&[((i32::MAX - 1, 0), 11), ((i32::MAX - 3, 0), 11)]).is_none()); + assert!(ExtraCover::new(&[((0, i32::MAX - 1), 11), ((0, i32::MAX - 3), 11)]).is_none()); + assert!(ExtraCover::new(&[((0, i32::MIN + 1), 11), ((0, i32::MIN + 3), 11)]).is_none()); } /// cargo test --release -p horizon-client --lib cover_tests::cover_cost -- --ignored --nocapture From a49cf5502d187daae072a039468a156226d90533 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Thu, 22 Oct 2026 15:37:37 +0200 Subject: [PATCH 16/29] geometry: floor_i32 saturates below i32::MIN, as (int) Math.floor Vanilla 26.3 Mth.floor(double) is (int) Math.floor(v), so a value below i32::MIN floors to i32::MIN. The truncate-and-step version wrapped to i32::MAX there (block_collision_scan parity failed at the chunk of x = -2147483648.8: Rust 134217727, vanilla -134217728). Step down with saturating_sub; the fast path is unchanged, and the test now checks the ends of the i32 range, infinities and f64::MIN/MAX against f64::floor as i32. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-core/src/geometry/mod.rs | 26 +++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/crates/horizon-core/src/geometry/mod.rs b/crates/horizon-core/src/geometry/mod.rs index 9b581c044..3081f66be 100644 --- a/crates/horizon-core/src/geometry/mod.rs +++ b/crates/horizon-core/src/geometry/mod.rs @@ -602,13 +602,15 @@ pub(crate) fn clamp(value: f64, min: f64, max: f64) -> f64 { java_min(value, max) } } -/// `Mth.floor`: truncate, then step down for negative fractions. A truncating -/// conversion and a compare, where `f64::floor` is a libm call on baseline x86-64. +/// `Mth.floor`, which is `(int) Math.floor(v)`: truncate, then step down for +/// negative fractions. A truncating conversion and a compare, where +/// `f64::floor` is a libm call on baseline x86-64. Both casts saturate (NaN is +/// 0), so the step down saturates too: below `i32::MIN` stays `i32::MIN`. #[inline] pub(crate) fn floor_i32(value: f64) -> i32 { let truncated = value as i32; if value < f64::from(truncated) { - truncated.wrapping_sub(1) + truncated.saturating_sub(1) } else { truncated } @@ -659,13 +661,25 @@ mod tests { -1e-300, 16_777_215.5, -16_777_215.5, + // Past the i32 range `(int) Math.floor` saturates (vanilla 26.3's + // BlockCollisions and BlockPos.containing rely on it). + 2_147_483_647.5, + 2_147_483_648.0, + -2_147_483_648.0, + -2_147_483_648.5, + -2_147_483_649.0, + 3e9, + -3e9, + f64::MAX, + f64::MIN, + f64::INFINITY, + f64::NEG_INFINITY, ] { assert_eq!(floor_i32(v), v.floor() as i32, "{v}"); } assert_eq!(floor_i32(f64::NAN), 0); - assert_eq!(floor_i32(3e9), i32::MAX); - // Java's (int) cast saturates, then the step down wraps. - assert_eq!(floor_i32(-3e9), i32::MAX); + assert_eq!(floor_i32(-3e9), i32::MIN); + assert_eq!(floor_i32(-2_147_483_648.5), i32::MIN); } #[test] fn ray_faces_and_half_open_bounds() { From ac60b337aa4374456664ed54c06f58e9bf9d2a56 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Thu, 22 Oct 2026 15:40:07 +0200 Subject: [PATCH 17/29] worldgen: chunks far apart still make a region (map past the grid) TerrainRegion::new refused a region whose chunks bounding box passed 2^20 columns, where it took any set of distinct chunks before: the bulk_section parity fixture puts a chunk at x = 4194304 beside the origin and failed with InvalidChunks. Keep the grid for the usual small box and fall back to a map past it; both refuse a position twice. The lookup now subtracts in i64, so no coordinate can wrap into the grid. Co-Authored-By: Claude Opus 5.5 --- .../src/worldgen/feature_region.rs | 120 ++++++++++++------ crates/horizon-world/tests/feature_region.rs | 43 +++++++ 2 files changed, 124 insertions(+), 39 deletions(-) diff --git a/crates/horizon-world/src/worldgen/feature_region.rs b/crates/horizon-world/src/worldgen/feature_region.rs index 91de61905..b4e8c07b0 100644 --- a/crates/horizon-world/src/worldgen/feature_region.rs +++ b/crates/horizon-world/src/worldgen/feature_region.rs @@ -22,7 +22,7 @@ use horizon_core::{ }, }; use std::{ - collections::{BTreeMap, BTreeSet}, + collections::{BTreeMap, BTreeSet, HashMap}, sync::Arc, }; @@ -60,11 +60,8 @@ impl From for PlacementQueryError { pub struct TerrainRegion<'r, B: TerrainBlockStates> { /// Ordered by X then Z. chunks: Vec>, - /// Each chunk's index in `chunks` over the chunks' bounding box, row by - /// row from `origin` (`u32::MAX`: none): every block access looks one up. - grid: Vec, - origin: (i32, i32), - columns: i32, + /// Each chunk's index in `chunks`: every block access looks one up. + index: ChunkIndex, min_y: i32, height: i32, generation: WorldGenerationContext, @@ -74,6 +71,77 @@ pub struct TerrainRegion<'r, B: TerrainBlockStates> { block_queries: Option, world_seed: Option, } +/// Where each chunk of a region sits in its list: a grid over the chunks' +/// bounding box while it is small (the usual square of neighbours), else a +/// map (chunks spread far apart). +enum ChunkIndex { + Grid { + origin: (i32, i32), + columns: i64, + rows: i64, + /// Row by row from `origin`; `u32::MAX` where no chunk is. + cells: Vec, + }, + Sparse(HashMap<(i32, i32), u32>), +} +impl ChunkIndex { + /// The largest grid, in chunk columns. + const GRID_CELLS: i64 = 1 << 20; + /// `None` when two chunks share a position. + fn new(positions: &[(i32, i32)]) -> Option { + let x0 = positions.iter().map(|p| p.0).min()?; + let z0 = positions.iter().map(|p| p.1).min()?; + let columns = i64::from(positions.iter().map(|p| p.0).max()?) - i64::from(x0) + 1; + let rows = i64::from(positions.iter().map(|p| p.1).max()?) - i64::from(z0) + 1; + if columns * rows > Self::GRID_CELLS { + let mut map = HashMap::with_capacity(positions.len()); + for (i, &p) in positions.iter().enumerate() { + if map.insert(p, i as u32).is_some() { + return None; + } + } + return Some(Self::Sparse(map)); + } + let mut cells = vec![u32::MAX; (columns * rows) as usize]; + for (i, &(x, z)) in positions.iter().enumerate() { + let cell = (i64::from(z) - i64::from(z0)) * columns + (i64::from(x) - i64::from(x0)); + let cell = &mut cells[cell as usize]; + if *cell != u32::MAX { + return None; + } + *cell = i as u32; + } + Some(Self::Grid { + origin: (x0, z0), + columns, + rows, + cells, + }) + } + fn get(&self, x: i32, z: i32) -> Option { + match self { + Self::Grid { + origin, + columns, + rows, + cells, + } => { + let (dx, dz) = ( + i64::from(x) - i64::from(origin.0), + i64::from(z) - i64::from(origin.1), + ); + if !(0..*columns).contains(&dx) || !(0..*rows).contains(&dz) { + return None; + } + match cells[(dz * columns + dx) as usize] { + u32::MAX => None, + i => Some(i as usize), + } + } + Self::Sparse(map) => map.get(&(x, z)).map(|&i| i as usize), + } + } +} impl<'r, B: TerrainBlockStates> TerrainRegion<'r, B> { pub fn new( chunks: Vec>, @@ -91,30 +159,14 @@ impl<'r, B: TerrainBlockStates> TerrainRegion<'r, B> { } let mut chunks = chunks; chunks.sort_by_key(|c| (c.position().x, c.position().z)); - let (xs, zs) = ( - chunks.iter().map(|c| c.position().x), - chunks.iter().map(|c| c.position().z), - ); - let origin = (xs.clone().min().unwrap(), zs.clone().min().unwrap()); - let columns = i64::from(xs.max().unwrap()) - i64::from(origin.0) + 1; - let rows = i64::from(zs.max().unwrap()) - i64::from(origin.1) + 1; - if columns * rows > 1 << 20 { - return Err(RegionError::InvalidChunks); - } - let mut grid = vec![u32::MAX; (columns * rows) as usize]; - for (i, chunk) in chunks.iter().enumerate() { - let p = chunk.position(); - let cell = (p.z - origin.1) as usize * columns as usize + (p.x - origin.0) as usize; - if grid[cell] != u32::MAX { - return Err(RegionError::InvalidChunks); - } - grid[cell] = i as u32; - } + let positions: Vec<(i32, i32)> = chunks + .iter() + .map(|c| (c.position().x, c.position().z)) + .collect(); + let index = ChunkIndex::new(&positions).ok_or(RegionError::InvalidChunks)?; Ok(Self { chunks, - grid, - origin, - columns: columns as i32, + index, sea_level: generator.sea_level, game_time: 0, next_tick_order: 0, @@ -212,17 +264,7 @@ impl<'r, B: TerrainBlockStates> TerrainRegion<'r, B> { self.chunks.iter() } fn index(&self, x: i32, z: i32) -> Option { - let (dx, dz) = (x.wrapping_sub(self.origin.0), z.wrapping_sub(self.origin.1)); - if dx < 0 || dz < 0 || dx >= self.columns { - return None; - } - match *self - .grid - .get(dz as usize * self.columns as usize + dx as usize)? - { - u32::MAX => None, - i => Some(i as usize), - } + self.index.get(x, z) } pub fn chunk(&self, x: i32, z: i32) -> Result<&TerrainChunk<'r, B>, RegionError> { match self.index(x, z) { diff --git a/crates/horizon-world/tests/feature_region.rs b/crates/horizon-world/tests/feature_region.rs index c484393bf..7ff34da6f 100644 --- a/crates/horizon-world/tests/feature_region.rs +++ b/crates/horizon-world/tests/feature_region.rs @@ -385,3 +385,46 @@ fn retained_upgrade_status_allows_old_biome_reads() { } } } +#[test] +fn spread_out_regions_route_like_dense_ones() { + // Chunks too far apart for a grid over their bounding box (fixtures put + // one at the far edge of the world beside the origin's neighbours). + let registry = registry(); + let context = WorldGenerationContext::new(0, 16, 8); + let chunk = + |x, z| TerrainChunk::new(ChunkPos::new(x, z), 0, 16, ®istry, Properties).unwrap(); + for far in [4_194_304, -4_194_304] { + let mut region = TerrainRegion::new( + vec![chunk(0, 0), chunk(far, 0), chunk(1, -1), chunk(-1, 1)], + context, + ) + .unwrap(); + let x = far * 16 + 3; + region + .set_block_worldgen([x, 7, 5], BlockStateId(2)) + .unwrap(); + assert_eq!(region.block([x, 7, 5]).unwrap(), BlockStateId(2)); + assert_eq!(region.block([3, 7, 5]).unwrap(), BlockStateId(0)); + assert_eq!(region.block([16, 7, -1]).unwrap(), BlockStateId(0)); + assert!(matches!( + region.block([x + 16, 7, 5]), + Err(RegionError::MissingChunk([_, 0])) + )); + assert!(matches!( + region.block([0, 7, 16]), + Err(RegionError::MissingChunk([0, 1])) + )); + let order: Vec<_> = region + .into_chunks() + .iter() + .map(|c| (c.position().x, c.position().z)) + .collect(); + let mut sorted = order.clone(); + sorted.sort(); + assert_eq!(order, sorted, "into_chunks is ordered by X then Z"); + // A position twice is refused either way. + assert!( + TerrainRegion::new(vec![chunk(0, 0), chunk(far, 0), chunk(far, 0)], context).is_err() + ); + } +} From e01296f99b31b4085190557fdbacb9aa4ec96de4 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 22 Oct 2026 13:47:37 +0000 Subject: [PATCH 18/29] Keep the pointer locked on macOS when the game captures it winit's macOS set_cursor_position re-associates the mouse with the pointer (CGAssociateMouseAndMouseCursorPosition(true)), so warping to the window centre right after the Locked grab released it again and the pointer moved freely in first person. Warp before grabbing on macOS; Wayland still warps after, since it moves a pointer only while locked. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_0188LkUPntcZdfMF3wAqR2tK --- crates/horizon-client/src/game.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index f706e9348..1162be7e9 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -4485,10 +4485,17 @@ impl Game<'_> { }; if capture { self.mouse.set_ignore_first_move(); + // macOS warps by re-associating the mouse with the pointer, + // which undoes a Locked grab and leaves the pointer free: warp + // first there. Wayland moves a pointer only while it is locked, + // so everywhere else warp after grabbing. + #[cfg(target_os = "macos")] + warp(window); self.captured = window .set_cursor_grab(CursorGrabMode::Locked) .or_else(|_| window.set_cursor_grab(CursorGrabMode::Confined)) .is_ok(); + #[cfg(not(target_os = "macos"))] if self.captured && self.focused { warp(window); } From 4e46f2a57a804fe81afc825b6fdfc9d3eef2df25 Mon Sep 17 00:00:00 2001 From: rdbtCVS <87152661+rdbtCVS@users.noreply.github.com> Date: Thu, 22 Oct 2026 14:48:08 +0100 Subject: [PATCH 19/29] bench: profile chunk generation and saved chunk loading --- crates/horizon-client/examples/load_bench.rs | 153 ++++++++++++++++++ .../horizon-world/examples/chunkgen_bench.rs | 38 ++++- docs/chunk-performance-campaign.md | 100 ++++++++++++ 3 files changed, 290 insertions(+), 1 deletion(-) create mode 100644 crates/horizon-client/examples/load_bench.rs create mode 100644 docs/chunk-performance-campaign.md diff --git a/crates/horizon-client/examples/load_bench.rs b/crates/horizon-client/examples/load_bench.rs new file mode 100644 index 000000000..5c30a71ff --- /dev/null +++ b/crates/horizon-client/examples/load_bench.rs @@ -0,0 +1,153 @@ +//! Read-only saved chunk benchmark. All timings exclude resource/corpus setup. +//! +//! load_bench RESOURCES DIMENSION_DIR [--rounds N] [--limit N] [--checksum] +//! Add --profile-worker when a sampling collector cannot sample the main thread. +//! Reports warm filesystem SaveDir::load (read, inflate, NBT, decode and light), +//! plus isolated inflate/NBT and native decode. No files in the save are changed. +use horizon_client::{ + assets::Assets, + biome::Biomes, + blocks::BlockFacts, + save::{SaveDir, SaveTables, decode_with}, +}; +use horizon_world::{nbt::Limits, region::RegionFile}; +use sha2::{Digest, Sha256}; +use std::{hint::black_box, path::PathBuf, sync::Arc, time::Instant}; + +type Result = std::result::Result>; + +fn main() -> Result<()> { + if std::env::args().any(|a| a == "--profile-worker") { + std::thread::spawn(|| run().map_err(|e| e.to_string())) + .join() + .expect("benchmark worker") + .map_err(Into::into) + } else { + run() + } +} + +fn run() -> Result<()> { + let args: Vec = std::env::args().skip(1).collect(); + let usage = "load_bench RESOURCES DIMENSION_DIR [--rounds N] [--limit N] [--checksum]"; + let root = PathBuf::from(args.first().ok_or(usage)?); + let dimension = PathBuf::from(args.get(1).ok_or(usage)?); + let flag = |name: &str, default: usize| -> Result { + Ok(match args.iter().position(|a| a == name) { + Some(i) => args.get(i + 1).ok_or(usage)?.parse()?, + None => default, + }) + }; + let rounds = flag("--rounds", 5)?; + let limit = flag("--limit", 256)?; + if rounds == 0 || limit == 0 { + return Err("rounds and limit must be positive".into()); + } + let assets = Assets::load(&root)?; + let facts = BlockFacts::load(&root, assets.registry)?; + let biomes = Biomes::load(&root)?; + let tables = Arc::new(SaveTables::new(&assets).with_block_entities(&facts)); + let source = SaveDir::new(dimension.clone()); + let _ = source.tables.set(tables.clone()); + let mut paths: Vec<_> = std::fs::read_dir(dimension.join("region"))? + .map(|e| e.map(|e| e.path())) + .collect::>()?; + paths.retain(|p| p.extension().is_some_and(|e| e == "mca")); + paths.sort(); + let mut corpus = Vec::new(); + 'regions: for path in paths { + let mut region = RegionFile::open(path)?; + let mut positions: Vec<_> = region.chunks().map(|(pos, _)| pos).collect(); + positions.sort_by_key(|p| (p.x, p.z)); + for pos in positions { + let record = region.read(pos)?.ok_or("missing record")?; + let tag = record.nbt(Limits::default())?; + if decode_with(&tag, assets.registry, &biomes, Some(&tables)).is_none() { + continue; + } + corpus.push((pos, record, tag)); + if corpus.len() == limit { + break 'regions; + } + } + } + if corpus.is_empty() { + return Err("no full chunks in corpus".into()); + } + let mut records_hash = Sha256::new(); + for (pos, record, _) in &corpus { + records_hash.update(pos.x.to_le_bytes()); + records_hash.update(pos.z.to_le_bytes()); + records_hash.update((record.compressed.len() as u64).to_le_bytes()); + records_hash.update(&record.compressed); + } + println!( + "{}", + serde_json::json!({"corpus": dimension, "chunks": corpus.len(), "rounds": rounds, + "compressed_records_sha256": format!("{:x}", records_hash.finalize()), + "compressed_bytes": corpus.iter().map(|(_, r, _)| r.compressed.len()).sum::()}) + ); + // Prime SaveDir's region handles and the filesystem cache before measurement. + for (pos, _, _) in &corpus { + black_box( + source + .load(pos.x, pos.z, assets.registry, &biomes) + .ok_or("load failed")?, + ); + } + for round in 0..rounds { + let started = Instant::now(); + for (pos, _, _) in &corpus { + black_box( + source + .load(pos.x, pos.z, assets.registry, &biomes) + .ok_or("load failed")?, + ); + } + let load_s = started.elapsed().as_secs_f64(); + let started = Instant::now(); + for (_, record, _) in &corpus { + black_box(record.nbt(Limits::default())?); + } + let nbt_s = started.elapsed().as_secs_f64(); + let started = Instant::now(); + for (_, _, tag) in &corpus { + black_box( + decode_with(tag, assets.registry, &biomes, Some(&tables)).ok_or("decode failed")?, + ); + } + let decode_s = started.elapsed().as_secs_f64(); + println!( + "{}", + serde_json::json!({"round": round, "load_s": load_s, + "inflate_nbt_s": nbt_s, "decode_s": decode_s}) + ); + } + if args.iter().any(|a| a == "--checksum") { + let mut hash = Sha256::new(); + let mut light_loaded = 0; + for (pos, _, _) in &corpus { + let chunk = source + .load(pos.x, pos.z, assets.registry, &biomes) + .ok_or("load failed")?; + hash.update(pos.x.to_le_bytes()); + hash.update(pos.z.to_le_bytes()); + light_loaded += usize::from(chunk.light_loaded); + for section in &chunk.sections { + for i in 0..4096 { + hash.update(section.get(i).to_le_bytes()); + hash.update([section.light(i)]); + } + for i in 0..64 { + hash.update(section.biomes.get(i).to_le_bytes()); + } + } + } + println!( + "{}", + serde_json::json!({"blocks_biomes_light_sha256": format!("{:x}", hash.finalize()), + "light_loaded": light_loaded}) + ); + } + Ok(()) +} diff --git a/crates/horizon-world/examples/chunkgen_bench.rs b/crates/horizon-world/examples/chunkgen_bench.rs index 4992515c5..23360b637 100644 --- a/crates/horizon-world/examples/chunkgen_bench.rs +++ b/crates/horizon-world/examples/chunkgen_bench.rs @@ -3,6 +3,7 @@ //! chunkgen_bench [--seed S] [--size N] [--threads T] //! [--center CX CZ] [--verify ] [--no-decorate] //! [--dimension overworld|the_nether|the_end] +//! [--warmup] [--json] [--no-map] use horizon_core::position::ChunkPos; use horizon_world::{ DATA_VERSION, @@ -275,6 +276,7 @@ fn main() -> Result<(), Error> { let (mut seed, mut size, mut threads, mut center) = (0i64, 16i32, 2usize, (0, 0)); let mut parity = None; let mut decorate = true; + let (mut warmup, mut json, mut map) = (false, false, true); let mut dimension = String::from("minecraft:overworld"); let mut starts: Option<(i32, i32, i32, i32, PathBuf)> = None; while let Some(arg) = args.next() { @@ -286,6 +288,9 @@ fn main() -> Result<(), Error> { "--center" => center = (next().parse()?, next().parse()?), "--verify" => parity = Some(PathBuf::from(next())), "--no-decorate" => decorate = false, + "--warmup" => warmup = true, + "--json" => json = true, + "--no-map" => map = false, "--dimension" => { let d = next(); dimension = if d.contains(':') { @@ -326,6 +331,16 @@ fn main() -> Result<(), Error> { dump_starts(&generator, seed, [x0, z0, x1, z1], &out)?; return Ok(()); } + if warmup { + let mut worker = generator.worker(); + for z in -4..=4 { + for x in -4..=4 { + std::hint::black_box( + worker.generate_proto(ChunkPos::new(center.0 + 64 + x, center.1 + 64 + z))?, + ); + } + } + } let (x0, z0) = (center.0 - size / 2, center.1 - size / 2); let count = (size * size) as usize; let next = AtomicUsize::new(0); @@ -353,6 +368,7 @@ fn main() -> Result<(), Error> { handles.into_iter().try_for_each(|h| h.join().unwrap()) })?; let elapsed = start.elapsed(); + let cpu_elapsed = cpu_seconds() - cpu; println!( "generated {count} proto chunks with {threads} threads in {elapsed:.2?}: {:.1} chunks/s ({:.1} ms/chunk/thread)", count as f64 / elapsed.as_secs_f64(), @@ -360,7 +376,7 @@ fn main() -> Result<(), Error> { ); println!( " cpu time {:.1} ms/chunk", - (cpu_seconds() - cpu) * 1000. / count as f64 + cpu_elapsed * 1000. / count as f64 ); let timings = timings.into_inner().unwrap(); let per_chunk = |f: fn(&StageTimings) -> std::time::Duration| { @@ -379,6 +395,16 @@ fn main() -> Result<(), Error> { per_chunk(|t| t.surface), per_chunk(|t| t.carvers) ); + if json { + println!( + "BENCH {}", + serde_json::json!({"phase": "proto", "chunks": count, + "threads": threads, "wall_s": elapsed.as_secs_f64(), "cpu_s": cpu_elapsed, + "biomes_ms_chunk": per_chunk(|t| t.biomes), "noise_ms_chunk": per_chunk(|t| t.noise), + "structures_ms_chunk": per_chunk(|t| t.structures), + "surface_ms_chunk": per_chunk(|t| t.surface), "carvers_ms_chunk": per_chunk(|t| t.carvers)}) + ); + } let mut chunks: Vec> = slots .into_iter() .map(|slot| slot.into_inner().unwrap()) @@ -441,6 +467,13 @@ fn main() -> Result<(), Error> { } } let elapsed = start.elapsed(); + if json { + println!( + "BENCH {}", + serde_json::json!({"phase": "decorate", "chunks": decorated, + "wall_s": elapsed.as_secs_f64()}) + ); + } let totals = totals.into_inner().unwrap(); println!( "decorated {decorated} chunks in {elapsed:.2?}: {:.1} chunks/s; feature roots placed {}, unsupported {}, failed {}", @@ -492,6 +525,9 @@ fn main() -> Result<(), Error> { } } } + if !map { + return Ok(()); + } let mut legend = BTreeMap::>::new(); let step = (size * 16 / 96).max(1); for bz in (0..size * 16).step_by(step as usize) { diff --git a/docs/chunk-performance-campaign.md b/docs/chunk-performance-campaign.md new file mode 100644 index 000000000..99645041e --- /dev/null +++ b/docs/chunk-performance-campaign.md @@ -0,0 +1,100 @@ +# Chunk generation and loading performance campaign + +Profiling baseline: `41055168afcd8687c7efe525545de896f17f4478` (2026-10-22). No production optimization is included in this profiling step. Measurements identify work to investigate; they do not establish a candidate speedup or complete Minecraft world parity. + +## Reproduction and artifacts + +Machine: AMD Ryzen 5 5600X, 6 cores/12 logical CPUs, 32 GiB RAM, Linux 7.2.11-zen1-1-zen x86_64; Rust/Cargo 1.99.0, OpenJDK 25.0.4.1. CPU boost remained enabled, governor `powersave`; normal desktop background activity was not isolated. Builds, timed workloads and sampling runs were sequential. Release uses `opt-level=3`, thin LTO, one codegen unit; client benchmarks disable audio/controllers and need no window/GPU. Do not compare these numbers directly with earlier `fast` profile reports. + +Private/generated evidence is outside Git at `/srv/bob-data/horizon-chunk-campaign-41055168/`: `baseline-raw.json`, `baseline-summary.json`, `run_baseline.py`, individual logs, preserved executables in `baseline/`, `binary-sha256.txt`, `fixture-sha256.txt`, the `*-profile.er` sampling experiments, and `*-functions.txt` tables. The matrix binaries are preserved separately from subsequent `*-profile` binaries. The latter add a structure timing field or optional worker execution for profiling; production sources are identical. Prepared resources: `/srv/bob-data/horizon/.local/horizon-client/26.3`. Pinned jars and source artifacts: `/srv/bob-data/horizon/.local/minecraft/26.3`. + +```sh +CARGO_BUILD_JOBS=6 CARGO_TARGET_DIR=/srv/bob-data/horizon-chunk-campaign-target cargo build --locked --release -p horizon-world --example chunkgen_bench -p horizon-client --no-default-features --example chunk_bench --example streaming_bench --example save_bench --example load_bench +``` + +`chunkgen_bench` now accepts `--warmup --json --no-map`. Warmup generates/discards an offset 9×9 region at center+(64,64), with the same seeded generator and a separate worker, before creating fresh measured workers. This removes lazy ring initialization from the steady workload without generating target chunks. Resource loading, generator construction, warmup, diagnostic output and map rendering remain outside the measured generation brackets. `BENCH` JSON reports exact elapsed seconds, process CPU accounting and cumulative stage wall brackets. Worker stage brackets are not exclusive CPU shares; elapsed/count has no thread multiplier. Non-warmed runs should be measured separately when changing startup behavior. + +`load_bench RESOURCES DIMENSION_DIR --rounds 5 --limit 256 --checksum` is read-only. It selects full chunks in sorted region/coordinate order, primes region handles and filesystem cache, then separately times `SaveDir::load` (region read, decompression, NBT, resident decode and saved light), decompression/NBT of retained compressed records, and native decode of retained tags. Allocation and destruction are inside each timed loop. Setup and checksums are outside. Stage times come from separate loops and must not be summed as an exact breakdown. It emits compressed-record SHA256 provenance and an optional complete blocks/biomes/light checksum; this checksum does not cover all saved metadata. + +## Baseline observations + +The primary workload uses seed 12345, center (0,0), size 16: 256 proto chunks and 196 interior decorations. One-worker/four-worker results have five processes each; Nether/End have three each. Values are medians. + +| Workload | Proto wall | Decoration wall | Combined ms/input chunk | +| --- | ---: | ---: | ---: | +| Overworld, 1 worker | 2.224094 s | 0.440144 s | 10.407179 | +| Overworld, 4 workers | 0.732982 s | 0.131088 s | 3.375274 | +| Nether, 1 worker | 0.981417 s | 0.084589 s | 4.164085 | +| End, 1 worker | 0.213254 s | 0.015784 s | 0.894681 | + +The one-worker Overworld brackets are approximately noise 4.20, surface 2.55, biomes 0.40 and carvers 0.34 ms/input chunk. A separate sampled size-32 run reports structures 0.395 ms/input chunk; its larger site mix differs from size 16. + +Resident `chunk_bench --seed 12345 --size 4`, three processes: finish 0.4 ms/chunk (rounded display), light median 1.43 ms/column, mesh 109.5 µs/section (each process reports its best of three mesh sweeps), 29.8 KiB resident storage/chunk. Before qualifying a finish/light/mesh change, emit exact timings on both revisions; existing rounded/best-of-three output is exploratory evidence only. + +`streaming_bench --threads 4 --radius 4 --seed 12345 --fly 4 --seconds 3 --hz 120`, three processes: fill median 1.21 s, 81 chunks generated and 504 sections meshed; median reported update p99 0.041 ms. Flight generates 121 chunks, with median update p99 0.207 ms and no sampled frame with an unlit column within two chunks of the player. This short fixed-duration flight is a responsiveness/throughput check, not a complete long-travel stress test. + +Saved loads use 256 full chunks, three independent processes, five measured rounds/process; table entries are the median of process medians for the whole 256-chunk batch. + +| Corpus | Complete load | Inflate + NBT | Native decode | +| --- | ---: | ---: | ---: | +| Normal vanilla 26.3, seed 12345 | 55.740 ms | 22.068 ms | 27.211 ms | +| Flat vanilla 26.3 locator fixture | 12.733 ms | 4.505 ms | 4.884 ms | + +The normal fixture was generated by the pinned local vanilla server, `level-type=minecraft:normal`, seed 12345, forced block bounds -128,-128 through 127,127, followed by `save-all flush` and clean stop. It contains 400 full chunks; the sorted limit selects 256. Files remain at `vanilla-normal/world/dimensions/minecraft/overworld` under the artifact root. Flat source is `/srv/bob-data/horizon/.local/locator-bar-parity/generate/world/dimensions/minecraft/overworld`. Normal compressed-record SHA256: `ff9bf9e822e334e117e3d6ba5c26a88b7698456ec6d83bdbe4355ad0af1d6f55`; blocks/biomes/light: `661dd26eed78b051afeca11d8ba69222162b5da72e5d1572a12ce2a3b3a4f764`. All three processes agree, and all 256 selected chunks have saved light. Flat checksums are in the raw logs. These are warm storage measurements; cold physical I/O and a remote multiplayer chunk stream are not measured. + +`save_bench` also read the 400 normal chunks and checked written compressed records byte-for-byte: median encode 139 ms, compression 141 ms, parallel SaveDir encode/compress/write 54 ms, five rounds. This checks existing save output consistency, not vanilla read/write parity. + +## CPU samples and ranked investigations + +`perf`, Valgrind and samply are unavailable; GNU gprofng 2.47 is installed. Collection uses `gprofng collect app -p hi -o OUTPUT.er BINARY ARGS`, nominally 997 µs samples. On this machine the collector warns that the initial thread's timer is cleared (`997 -> 0`). Initial-thread samples are therefore unusable. The measured terrain worker (thread 4) records 9.204 sampled CPU seconds against 9.22 process CPU seconds in its generation bracket; decoration worker threads record 3.098 seconds against 3.126 wall seconds. Their worker-only tables are retained as `terrain-worker-functions.txt` and `decoration-worker-functions.txt`. Startup/main-thread percentages must not be inferred from these samples. Streaming worker samples independently reproduce the same generation hotspots. + +The initial loader experiment captured only 0.108 sampled seconds against 10.899 process CPU seconds and is rejected. Repeating the identical code with optional `load_bench --profile-worker --rounds 100` captures 11.323 sampled seconds against 11.347 process CPU seconds; its table is `load-worker-profile-functions.txt`. In this mixed complete-load/NBT/native-decode workload, `PalettedContainer::from_packed` is 16.12% exclusive / 16.62% inclusive, zlib `inflate_fast_help_avx2` 15.71% exclusive, saved `load_light` 5.80% exclusive / 7.38% inclusive, and NBT `Reader::payload` 4.08% exclusive / 15.06% inclusive. Native `decode_column` is 46.97% inclusive. Formatting (`format_inner`) is 6.52% inclusive, including property-map Debug formatting at 4.79%; these nested shares cannot be added. These are shares of all three benchmark loops, not just the complete-load loop. Palette reconstruction/validation is the strongest loading lead; replacing the formatted per-chunk state-cache key is a smaller plausible lead requiring the full-load 5% gate. + +For the size-32 Overworld terrain worker, ranked opportunities outside the excluded PRs are: + +1. Aquifer `compute_substance`: 13.00% exclusive / 15.83% inclusive CPU. Investigate repeated nearest-cell/grid work while retaining exact distance ties, arithmetic, random draws and fluid scheduling. +2. Material `CompiledRule::apply`: 8.52% exclusive / 14.58% inclusive. Check the already-merged surface work before changing anything; inspect fresh evidence for remaining dispatch/data access, preserving condition order and random consumption. +3. `BlockSection::set` 6.09% and `get` 2.79%; decoration additionally spends 9.95% in `get`. Investigate construction/access patterns rather than changing vanilla palette semantics. Shared storage changes need broad callers and palette-width boundary validation. +4. Beardifier `sample_unchecked`: 4.78% exclusive. Recent bulk-density work is already in baseline; only a distinct measured bottleneck should proceed. +5. Decoration sculk `spread_all`: 8.82% exclusive / 22.85% inclusive of decoration-worker CPU at this larger site. Strongly site-dependent; require a matched sculk corpus before claiming a whole-workload win. + +Open/overlapping work excluded from optimization: #1208 Perlin floor/int conversion and float gradients; #1206 Perlin volume/climate lookup; #1207 feature-region grid, ore column/visited handling, lazy biome temperature; #1201 player proximity/ticket/requeue grids; #1192 worker priority; #1191 content/assets loading. Existing merged sampler reuse, neighbor generation, carver cache, surface, packed lighting and bulk density changes are part of this baseline. These exclusions cover the semantic mechanism, not merely the file paths. + +## Candidate acceptance protocol + +For each independent candidate, preserve identical benchmark sources, resource/corpus hashes, build flags and baseline/candidate executables. Do not compare against an open PR's branch. Run no builds, parity jobs or profilers concurrently with timed qualification. Use one discarded baseline/candidate process pair, then at least five measured pairs, alternating A/B and B/A order. Keep every observation; if external contention invalidates a batch, retain it and restart the entire batch with an explanation. + +Primary generation command: `chunkgen_bench RESOURCES --seed 12345 --size 16 --threads 1 --warmup --json --no-map`, repeated at four workers. Add seed -1 at center (32,-32), Nether and End, with five pairs for any affected claim. Report proto, decoration and complete proto+decoration wall time; input and decorated chunk counts must match. Retain a non-warmed check for startup regressions and streaming fill/flight for player-visible effects. + +Primary loading command: `load_bench RESOURCES NORMAL_DIMENSION --rounds 5 --limit 256 --checksum`, repeated with the flat fixture. Each process's median complete `load_s` is one observation; internal rounds are not independent processes. Report native decode and inflate/NBT alongside complete load. Use the same command without profiler-worker mode on both revisions for qualification. + +Open a PR only after the affected complete workload improves by at least 5% in the paired repeated measurements, the improvement is stable across the measured pairs, and correctness gates pass. Report median reduction and range of paired reductions; add pairs if noise makes the 5% threshold ambiguous. A 5% isolated-stage win without a 5% complete affected-workload win does not qualify. Check related workloads for material regression. Explain corpus-specific results instead of claiming universal speedups. + +Correctness gates must include exact baseline/candidate generated blocks, quart biomes, heightmaps, postprocessing, structures, block entities and metadata for affected cases, preserving RNG and iteration behavior, plus the pinned vanilla oracle. Use existing `tools/test_chunk_generation_parity.py` (full world stage comparisons), relevant noise/density/aquifer/material/feature/palette harnesses, and `tools/test_light_parity.py` if lighting changes. Cover negative/far coordinates, several seeds, all affected dimensions and both worker counts. `tools/parity.py --repeat 3 ...` checks repeated oracle/probe agreement; reused Java oracle output is suitable for Rust-only comparisons, with a fresh relevant oracle run before final qualification. + +Loading changes additionally require existing save unit tests and comprehensive baseline/candidate semantic output checks, including heightmaps, block entities, ticks, structures and preserved unknown NBT. The benchmark's block/biome/light checksum is only one gate. Use `tools/test_save_roundtrip_parity.py` for the full vanilla → Horizon → vanilla path when relevant. Prepared Java 25/pinned jars/resources make these checks feasible here; full roundtrip requires the repository's virtual-display/runtime setup and has not been run in this profiling step. No complete world-parity pass is claimed by this report. Run `python3 tools/pr_ready.py --base origin/main` and repository-required checks before any PR. + +## Agent 2: saved palette validation + +Candidate: list palettes in `PalettedContainer::from_packed` keep the saved words directly, so avoid allocating/filling the discarded unpacked-index vector. Reuse index validation, scanning words and their occupied slots in entry order; a palette filling the bit capacity cannot contain an invalid index. Global reencoding remains unchanged. The first invalid index, exact storage-length errors, unused high padding, palette order and resident words are preserved. Pinned 26.3 `PalettedContainer.unpack` bytecode was inspected first: matching list configurations retain `SimpleBitStorage` directly; global configurations reencode. No palette-related open PR overlap was found on 2026-10-22. + +Evidence: `/srv/bob-data/horizon-chunk-campaign-41055168/palette/`, `palette-pairs.py`, `palette-pilot.log`, `palette-qualification.log`, `palette-candidate.patch`, `palette-java-bytecode.txt`, and `palette-*.log`. Binary hashes and commands are recorded in `palette/binaries.json` and `*-raw.json`. The preserved baseline `load_bench-profile` is used without worker/profiler flags because its source exactly matches the candidate benchmark source, including the optional worker branch. Identical release flags, resources and compressed records were used. No builds, tests or profilers overlapped qualification. + +Three pilot pairs showed 9.77–10.28% complete normal-load reductions after a discarded pair. Qualification used five alternating process pairs per corpus, each after a discarded pair, with five measured rounds per process. Values below are medians of process medians for each 256-chunk batch; reductions are medians/ranges of the paired reductions. + +| Corpus / workload | Baseline | Candidate | Paired reduction (range) | +| --- | ---: | ---: | ---: | +| Normal complete load | 55.459 ms | 49.770 ms | 10.257% (9.656–10.573%) | +| Normal native decode | 26.263 ms | 19.902 ms | 24.188% (22.323–25.987%) | +| Normal inflate + NBT | 22.260 ms | 22.086 ms | 0.782% (-1.174–1.680%) | +| Flat complete load | 12.663 ms | 12.027 ms | 5.023% (2.609–5.617%) | +| Flat native decode | 5.170 ms | 4.220 ms | 13.908% (5.850–28.399%) | +| Flat inflate + NBT | 4.537 ms | 4.548 ms | -0.159% (-1.852–0.563%) | + +Normal clears the complete-workload 5% gate in every measured pair. Flat shows no observed regression, but its variation does not support a stable at-least-5% claim. This is a warm saved-load improvement on these fixtures, not a generation or cold-I/O claim. + +Correctness: all timed baseline/candidate compressed-record and block/biome/light hashes match. Core palette tests pass (7 passed; 2 existing ignored). A differential reference test covers all saved widths through 13, single/list/global modes, duplicate values, full-capacity palettes, absent/wrong storage, dirty word/final padding, and first-invalid-index errors; successful cases compare strategy, bits, palette and words exactly. A fresh pinned Java palette oracle passes 266 scenarios and 861,864 edits with identical hashes. Existing `save::tests` pass (19/19), including malformed packed palettes, stored light, block-entity roundtrips, file upgrades and level-file roundtrip. + +The temporary `load_snapshot.rs` harness and true baseline/candidate binaries are archived under `palette/`; the harness was removed from the working tree. It hashes resident palette/word representations and light values, canonical complete reencoded NBT (including heightmaps/block entities), saved root NBT/block-entity sources/order/ticks, and loaded flags, names, postprocessing, entities and structure references. Every full chunk was checked: 400 normal and 289 flat, plus a synthetic nested unknown NBT key and nonempty block/fluid ticks for each corpus. All 691 per-case baseline/candidate outputs match byte-for-byte. `palette/correctness.json` records hashes; `palette/sources.json` authenticates both core sources and the harness. Exact candidate source was restored after the baseline comparison. + +Both baseline and candidate also ran `save_parity` against the pinned vanilla-generated worlds: 324 interior normal chunks have zero block, biome, heightmap or other metadata differences, with the same preexisting 7/63,700,992 recomputed light-value differences in both revisions. All checks on 225 interior flat chunks are identical to vanilla, including all 44,236,800 light values. This establishes no added discrepancy in these comparisons; it is not a clean normal-world lighting parity pass. The full server/client `test_save_roundtrip_parity.py` simulation was not run; the candidate changes only loading/validation, and the read-only full-output comparison covers unchanged save output. No generation parity claim or PR is included in this agent's result. From 5d82b4c999d182db825741b2998b26d7bac90167 Mon Sep 17 00:00:00 2001 From: rdbtCVS <87152661+rdbtCVS@users.noreply.github.com> Date: Thu, 22 Oct 2026 14:48:08 +0100 Subject: [PATCH 20/29] perf: load list palettes without unpacking discarded indices --- crates/horizon-core/src/paletted.rs | 150 +++++++++++++++++++++++++--- 1 file changed, 134 insertions(+), 16 deletions(-) diff --git a/crates/horizon-core/src/paletted.rs b/crates/horizon-core/src/paletted.rs index 3d56bb987..0730cf5e4 100644 --- a/crates/horizon-core/src/paletted.rs +++ b/crates/horizon-core/src/paletted.rs @@ -577,14 +577,21 @@ impl PalettedContainer { if list.len() == 1 << self.bits { return Ok(()); } - for i in 0..self.strategy.entries() { - let index = read(&self.words, self.bits, i); - if index as usize >= list.len() { - return Err(PaletteError::BadIndex { - index, - palette: list.len(), - }); + let per = 64 / usize::from(self.bits); + let mut remaining = self.strategy.entries(); + let mask = mask(self.bits); + for &word in &self.words { + let count = remaining.min(per); + for slot in 0..count { + let index = ((word >> (slot * usize::from(self.bits))) & mask) as u32; + if index as usize >= list.len() { + return Err(PaletteError::BadIndex { + index, + palette: list.len(), + }); + } } + remaining -= count; } Ok(()) } @@ -644,6 +651,18 @@ impl PalettedContainer { actual: data.len(), }); } + if let Config::List(bits) = config { + // List palettes already use the saved indices as resident storage. + // Validate them without allocating an unpacked copy that is unused. + let container = Self { + strategy, + bits, + words: data.to_vec(), + palette: Palette::List(palette), + }; + container.check_indices()?; + return Ok(container); + } let mut ids = Vec::with_capacity(entries); for i in 0..entries { let index = read(data, disc_bits, i); @@ -655,15 +674,7 @@ impl PalettedContainer { } ids.push(index); } - Ok(match config { - Config::List(bits) => Self { - strategy, - bits, - words: data.to_vec(), - palette: Palette::List(palette), - }, - _ => Self::build(strategy, palette, &ids), - }) + Ok(Self::build(strategy, palette, &ids)) } /// Whether any palette entry satisfies `predicate` (`maybeHas`): entries /// no longer used by any position still count. @@ -796,6 +807,113 @@ mod tests { assert!(PalettedContainer::from_wire(BLOCKS, 0, vec![9], vec![]).is_ok()); } #[test] + fn saved_validation_matches_entrywise_reference() { + // Keep the previous loader as an independent entry-at-a-time reference: + // errors, resident palette order and unused storage bits must all agree. + fn reference( + strategy: Strategy, + palette: Vec, + data: Option<&[u64]>, + ) -> Result { + if palette.is_empty() { + return Err(PaletteError::BadPalette { len: 0 }); + } + let config = strategy.config(ceil_log2(palette.len())); + let entries = strategy.entries(); + let bits = match config { + Config::Single => return Ok(PalettedContainer::new(strategy, palette[0])), + Config::List(bits) => bits, + Config::Global => ceil_log2(palette.len()), + }; + let expected = storage_len(bits, entries); + let data = data.ok_or(PaletteError::BadStorage { + expected, + actual: 0, + })?; + if data.len() != expected { + return Err(PaletteError::BadStorage { + expected, + actual: data.len(), + }); + } + let mut ids = Vec::with_capacity(entries); + for i in 0..entries { + let index = read(data, bits, i); + if index as usize >= palette.len() { + return Err(PaletteError::BadIndex { + index, + palette: palette.len(), + }); + } + ids.push(index); + } + Ok(match config { + Config::List(bits) => PalettedContainer { + strategy, + bits, + palette: Palette::List(palette), + words: data.to_vec(), + }, + _ => PalettedContainer::build(strategy, palette, &ids), + }) + } + fn compare(strategy: Strategy, palette: &[u32], data: Option<&[u64]>) { + let expected = reference(strategy, palette.to_vec(), data); + let actual = PalettedContainer::from_packed(strategy, palette.to_vec(), data); + match (actual, expected) { + (Ok(a), Ok(b)) => { + assert_eq!(a.strategy, b.strategy); + assert_eq!(a.bits, b.bits); + assert_eq!(a.palette, b.palette); + assert_eq!(a.words, b.words); + } + (Err(a), Err(b)) => assert_eq!(a, b), + (a, b) => panic!("loader disagreement: {a:?} vs {b:?}"), + } + } + for strategy in [BLOCKS, BIOMES] { + for len in [ + 0, 1, 2, 3, 4, 5, 8, 9, 15, 16, 17, 31, 32, 33, 63, 64, 65, 127, 128, 129, 255, + 256, 257, 511, 512, 513, 1023, 1024, 1025, 2047, 2048, 2049, 4096, 4097, + ] { + let palette: Vec = (0..len).map(|i| (i as u32 * 7) % 127).collect(); + compare(strategy, &palette, None); + compare(strategy, &palette, Some(&[])); + if len <= 1 { + compare(strategy, &palette, Some(&[u64::MAX])); + continue; + } + let bits = match strategy.config(ceil_log2(len)) { + Config::List(bits) => bits, + _ => ceil_log2(len), + }; + let entries = strategy.entries(); + let mut words = pack_words(bits, entries, (0..entries).map(|i| (i % len) as u32)); + compare(strategy, &palette, Some(&words)); + compare(strategy, &palette, Some(&words[..words.len() - 1])); + // Nonzero padding in every word and in the final partial word + // is legal and must be preserved by resident list palettes. + let per = 64 / usize::from(bits); + for (word, positions) in words.iter_mut().zip((0..entries).step_by(per)) { + let used = (entries - positions).min(per) * usize::from(bits); + if used < 64 { + *word |= u64::MAX << used; + } + } + compare(strategy, &palette, Some(&words)); + if len < 1usize << bits { + for i in [0, per - 1, per, entries / 2, entries - 1] { + let mut invalid = words.clone(); + write(&mut invalid, bits, i, len as u32); + // A second invalid entry verifies first-error ordering. + write(&mut invalid, bits, entries - 1, mask(bits) as u32); + compare(strategy, &palette, Some(&invalid)); + } + } + } + } + } + #[test] fn division_magic_is_exact() { for bits in 1..=32u8 { let per = 64 / usize::from(bits); From 753c071c3776229928508b5d7d6451644f4c8ec1 Mon Sep 17 00:00:00 2001 From: rdbtCVS <87152661+rdbtCVS@users.noreply.github.com> Date: Thu, 22 Oct 2026 15:09:08 +0100 Subject: [PATCH 21/29] docs: record early-stopped chunk optimization attempts --- docs/chunk-performance-campaign.md | 42 ++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/docs/chunk-performance-campaign.md b/docs/chunk-performance-campaign.md index 99645041e..5c9d15001 100644 --- a/docs/chunk-performance-campaign.md +++ b/docs/chunk-performance-campaign.md @@ -98,3 +98,45 @@ Correctness: all timed baseline/candidate compressed-record and block/biome/ligh The temporary `load_snapshot.rs` harness and true baseline/candidate binaries are archived under `palette/`; the harness was removed from the working tree. It hashes resident palette/word representations and light values, canonical complete reencoded NBT (including heightmaps/block entities), saved root NBT/block-entity sources/order/ticks, and loaded flags, names, postprocessing, entities and structure references. Every full chunk was checked: 400 normal and 289 flat, plus a synthetic nested unknown NBT key and nonempty block/fluid ticks for each corpus. All 691 per-case baseline/candidate outputs match byte-for-byte. `palette/correctness.json` records hashes; `palette/sources.json` authenticates both core sources and the harness. Exact candidate source was restored after the baseline comparison. Both baseline and candidate also ran `save_parity` against the pinned vanilla-generated worlds: 324 interior normal chunks have zero block, biome, heightmap or other metadata differences, with the same preexisting 7/63,700,992 recomputed light-value differences in both revisions. All checks on 225 interior flat chunks are identical to vanilla, including all 44,236,800 light values. This establishes no added discrepancy in these comparisons; it is not a clean normal-world lighting parity pass. The full server/client `test_save_roundtrip_parity.py` simulation was not run; the candidate changes only loading/validation, and the read-only full-output comparison covers unchanged save output. No generation parity claim or PR is included in this agent's result. + + +## Round 3: aquifer neighborhood pilot — rejected + +Inspected the pinned server 26.3 `Aquifer.NoiseBasedAquifer.computeSubstance` bytecode and current open PRs; no aquifer optimization overlap was found. Tested a single-current-grid cache of the 12 decoded candidate positions/indices, populated in vanilla cell order only after successful checked accesses, plus explicit four-slot nearest insertion preserving later-cell distance ties. The candidate touched only `worldgen/aquifer/noise_based.rs`; no noise evaluation or terrain traversal was changed. + +Evidence is retained in `/srv/bob-data/horizon-chunk-campaign-41055168/aquifer/`: baseline source, candidate patch, preserved candidate executable, binary hashes, build log, pilot script, all process logs, `pilot-raw.json` and `summary.json`. Pinned Java bytecode is in `aquifer-java-bytecode.txt` at the parent artifact root. The benchmark source SHA256 is `f404feb7ec526f12735d11b15af36254fd585f39b975cdbdc71905d87f10f8cc`, identical to the preserved `chunkgen_bench-profile` baseline source. Both executables use the established release flags and resources. No builds, tests or profilers ran during timing. + +One discarded process pair followed by three alternating pairs used the primary seed-12345, center-(0,0), size-16, one-worker warmed command. Every process generated 256 proto chunks and decorated 196 chunks. Complete proto+decoration paired reductions were **3.864%, 4.326%, 3.828%** (median **3.864%**); baseline/candidate complete-time medians were **2.678451 s / 2.568094 s**. This does not clear the 5% complete-workload gate. The pilot had already completed when the parent requested early cancellation. The production candidate was reverted; no qualification, four-worker/second-site matrix, parity tests, generated snapshots or PR were pursued. No correctness claim is made for the rejected candidate. This result applies to this mechanism and corpus, not an upper bound on all aquifer optimization. + +Next lead: remaining material-rule dispatch/data access (8.52% exclusive terrain-worker CPU), after checking the already-merged surface changes. Preserve rule evaluation order and random consumption; reject early if its complete-workload pilot remains below 5%. + + +## Round 4: remaining material-rule dispatch — early no-go + +Inspected merged #801 (`Specialize constant surface material-rule branches`), `docs/surface-generation-benchmark.md`, the current compiled rule/condition/density implementation, and the pinned 26.3 material resources and Java `ConditionRule`, `SequenceRule`, `OreVeinRule`, and `BlockRule` bytecode. The open-PR list was checked on 2026-10-22; no additional material-rule dispatch PR was found, and the campaign's excluded mechanisms were left untouched. Inspection evidence is retained under `/srv/bob-data/horizon-chunk-campaign-41055168/surface-feasibility/`: `pinned-material-bytecode.txt` and `inspection.json` (source commit, pinned server-game SHA256, and static expanded rule counts). + +The existing implementation already removes constant predicates and singleton/empty sequences after compiling all children, shares eager density roots, and uses unit-step material-volume indexing. The 8.52% exclusive terrain-worker sample share for `CompiledRule::apply` includes ore-vein execution and inlined work, so it cannot be treated as removable dispatch overhead. Vanilla's top-level Overworld sequence includes both ore-vein rules before the surface predicate and underground rules. Condition/block fusion or flattening nested sequences would remove only part of this work; a broad replacement evaluator would be a speculative refactor without evidence for the required complete-workload gain. The raw expanded Overworld resource tree contains 53 sequences, 149 conditions, 110 blocks, two ore-vein rules and two band rules; these static counts precede constant specialization and are not runtime frequencies. + +Decision: **early no-go**, accepted by the parent before implementation. No production source or tests were changed, no build/timing/parity jobs were run, and no candidate or PR was created. This is a feasibility rejection, not a measured performance result or an upper bound on future material optimizations. The rejected aquifer executable in the shared target directory was not used. Earlier campaign evidence and the uncommitted Round 3 report were preserved. + + +## Round 5: section access and construction — early no-go + +Inspected `BlockSection` get/set/construction, `PackedStorage` addressing, the existing ignored section microbenchmark and differential tests, and merged commits `939eabe6`, `56a251eb` and `5c55e7d8`. Checked the open-PR list on 2026-10-22; no section-storage optimization overlap was found. Pinned 26.3 `LevelChunkSection`, `PalettedContainer` and `SimpleBitStorage` bytecode was inspected before considering changes. Evidence is retained in `/srv/bob-data/horizon-chunk-campaign-41055168/section-feasibility/`: `pinned-section-bytecode.txt`, `baseline-access-assembly.txt` and `inspection.json` (source/pinned-jar/baseline hashes and the bounded open-PR catalog). + +The current implementation already reads the old packed index directly, resolves the incoming state with one multiplicative-hash map probe, reuses freed palette slots, and skips repeated storage validation after the section checks. The ignored microbenchmark compares against older pre-optimization implementations; its historical speedups are already in the campaign baseline and cannot qualify a new candidate. Uniform construction still allocates palette/count/map storage, but construction is not identified as a substantial exclusive hotspot in the retained profile. + +The preserved baseline executable confirms runtime division in packed-index addressing; pinned Java uses reciprocal multiplication in `SimpleBitStorage.cellIndex`. This is a possible future storage investigation, but the division is only part of the sampled access cost, alongside palette/word loads, incoming-state lookup and count/free-slot handling. Terrain samples assign 6.09% exclusive CPU to `set` and 2.79% to `get`, while the separate decoration sample assigns 9.95% to `get`; these site-specific shares are not a measured removable fraction or a complete-workload speedup prediction. There is no evidence here that a small addressing or construction change can remove enough complete proto-plus-decoration work to meet the 5% gate. A dense representation or bulk terrain writer would need broader semantic and workload evidence and was not attempted. + +Decision: **early no-go**, accepted by the parent before implementation. No production source or tests were changed, no build/timing/parity jobs were run, and no candidate or PR was created. No new correctness or performance result is claimed. The rejected aquifer executable in the shared target directory was not used; assembly inspection used the preserved genuine baseline. Earlier campaign evidence and uncommitted Round 3/4 reports were preserved. + + +## Round 6: saved block-state cache keys — rejected + +Inspected the loader's formatted per-chunk state-cache keys, `BlockState::from_nbt`, registry lookup/default fallback, and pinned 26.3 `StateHolder.codec`/`BlockState` bytecode. Checked the open-PR list on 2026-10-22; no saved-state-cache optimization overlap was found. The candidate derived `Hash` for `BlockState` and replaced the formatted string key with the parsed structural state. Invalid parse results returned air directly; the existing registry/default-property resolution was retained. No palette or lighting code changed. + +Evidence is retained under `/srv/bob-data/horizon-chunk-campaign-41055168/state-key/`: candidate patch and sources, preserved baseline source copies, pinned bytecode, bounded open-PR catalog, build log, paired runner, raw process observations/logs, executable hashes and summary. The baseline was the preserved **accepted palette candidate**, so these results are incremental to commit `5d82b4c9`, with none of its earlier gain counted again. Both binaries used identical `load_bench` source (SHA256 `b6f5042731d7c9d5622758043f7c1e125a3f7d098c57ee0501012474c2df7325`), release/thin-LTO/one-codegen-unit flags, resources and normal corpus. No builds, tests or profilers overlapped timing. + +One discarded process pair followed by two alternating measured pairs used `--rounds 5 --limit 256 --checksum`. Complete normal-load paired reductions were **3.257% and 2.412%** (median **2.835%**); baseline/candidate process-median medians were **49.725 / 48.316 ms** per 256 chunks. Native decode improved **5.917%** (20.266 / 19.067 ms); inflate/NBT varied by **-0.844%**. Compressed-record and block/biome/light checksums matched in every process. Those checksums are limited benchmark checks, not comprehensive semantic or vanilla parity evidence. + +Decision: **early no-go** at the incremental 5% complete-load gate. Both production files were restored exactly to their pre-candidate contents; only this report remains. No flat/long qualification, comprehensive snapshot comparison, fresh parity suite, tests, commit or PR were pursued for this rejected candidate. The shared Cargo target contains the rejected executable; future work must use the preserved accepted binary or rebuild restored sources. From 71edc9d41ce51c555f4c80b630a6e7c3f533d1b8 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 22 Oct 2026 13:18:54 +0000 Subject: [PATCH 22/29] Prepare with Minecraft's own Java 25 when no JDK is installed On a Mac without a JDK, preparing failed with "Java 25 is needed ... /usr/bin/javac -version printed no version: Unable to locate a Java Runtime": /usr/bin/javac is macOS's placeholder. Mojang's Java runtimes are whole JDKs, javac included (java-runtime-epsilon is 25.0.1 on every platform), and the official launcher has already downloaded one for anyone who played 26.3. - horizon-prepare: java::find also tries Minecraft's runtimes (/runtime///, jre.bundle on macOS) and the launcher's pick (Options::java_home), and names the macOS placeholder for what it is. - launcher: when no JDK is found, downloads the runtime the version JSON names (javaVersion.component) from Mojang into its own game folder, every file checked against its sha1, executables and links as the official launcher lays them out. A version's own preparer binary or Python script gets it through JAVA_HOME and the PATH. Checked for real on Linux: the download is 147 files, its javac 25.0.1 compiles and runs a class with --release 25. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01AxbYUJt4ShJFCdyLNCxF21 --- crates/horizon-prepare/src/java.rs | 96 ++++++++++++++++++++++-- crates/horizon-prepare/src/lib.rs | 9 ++- crates/horizon-prepare/src/main.rs | 1 + launcher/src/backend.rs | 97 +++++++++++++++++++----- launcher/src/mojang.rs | 116 +++++++++++++++++++++++++++++ launcher/src/mojang_tests.rs | 96 ++++++++++++++++++++++++ website/docs/install.md | 2 +- website/docs/troubleshooting.md | 2 +- 8 files changed, 390 insertions(+), 29 deletions(-) diff --git a/crates/horizon-prepare/src/java.rs b/crates/horizon-prepare/src/java.rs index d54ef0949..a159fb055 100644 --- a/crates/horizon-prepare/src/java.rs +++ b/crates/horizon-prepare/src/java.rs @@ -41,11 +41,19 @@ fn javac_major(javac: &Path) -> Result { .skip_while(|w| *w != "javac") .nth(1) .ok_or_else(|| { - format!( - "{} -version printed no version: {}", - javac.display(), - text.trim() - ) + // macOS ships /usr/bin/javac as a placeholder that only says no Java is installed. + if text.contains("Unable to locate a Java Runtime") { + format!( + "{} is macOS's placeholder (no Java installed)", + javac.display() + ) + } else { + format!( + "{} -version printed no version: {}", + javac.display(), + text.trim() + ) + } })?; let mut parts = version.split(['.', '-', '+', '_']); let first: u32 = parts.next().and_then(|p| p.parse().ok()).unwrap_or(0); @@ -126,11 +134,38 @@ fn installed_jdks(parents: &[PathBuf], major: i64) -> Vec { homes } +/// The Java runtimes the official launcher downloaded into `runtime` (a game directory's +/// `runtime` folder: `///`, a macOS one in `jre.bundle`). Mojang's +/// runtimes are whole JDKs, javac included, so a player who has run Minecraft has one already. +pub fn minecraft_runtimes(runtime: &Path) -> Vec { + let children = |dir: &Path| -> Vec { + let mut paths: Vec = fs::read_dir(dir) + .map(|entries| entries.flatten().map(|e| e.path()).collect()) + .unwrap_or_default(); + paths.sort(); + paths + }; + let mut homes = Vec::new(); + for component in children(runtime) { + for platform in children(&component) { + let home = platform.join(component.file_name().unwrap_or_default()); + for home in [home.join("jre.bundle/Contents/Home"), home] { + if executable(&home.join("bin"), "javac").is_file() { + homes.push(home); + break; + } + } + } + } + homes +} + /// Finds a JDK with `javac` of at least `major`, the version research-lock.json pins: /// `JAVA_HOME` when it holds one, else `javac` and `java` on the PATH (as the Python preparer -/// ran them), else, on macOS, the JDK `/usr/libexec/java_home -v ` reports, else a JDK -/// in the folders installers put them in (`jdk_parents`). -pub fn find(major: i64) -> Result { +/// ran them), else, on macOS, the JDK `/usr/libexec/java_home -v ` reports, else the JDK +/// homes in `more` (the launcher's pick, Minecraft's own runtimes), else a JDK in the folders +/// installers put them in (`jdk_parents`). +pub fn find(major: i64, more: &[PathBuf]) -> Result { let need = format!( "Java {major} is needed (a JDK with javac, e.g. Eclipse Temurin {major}). \ Install it, or set JAVA_HOME to it, and try again." @@ -164,6 +199,14 @@ pub fn find(major: i64) -> Result { } } } + for home in more { + let bin = home.join("bin"); + candidates.push(( + executable(&bin, "javac"), + executable(&bin, "java"), + "Minecraft's Java", + )); + } for home in installed_jdks(&jdk_parents(), major) { let bin = home.join("bin"); candidates.push(( @@ -205,6 +248,15 @@ pub fn classpath<'a>(paths: impl IntoIterator) -> String { } impl Jdk { + /// The JDK's home, the folder above `bin` (what `JAVA_HOME` names). + pub fn home(&self) -> PathBuf { + self.javac + .parent() + .and_then(Path::parent) + .map(Path::to_path_buf) + .unwrap_or_default() + } + /// `javac --release -proc:none` with Python's `subprocess.run(..., check=True, /// stdout=log, stderr=STDOUT, timeout=3600)`. pub fn javac(&self, major: i64, args: &[String], log: &fs::File) -> Result<(), String> { @@ -301,4 +353,32 @@ mod tests { ); fs::remove_dir_all(&dir).unwrap(); } + + #[test] + fn minecraft_runtimes_are_found_in_the_launchers_layout() { + let dir = + std::env::temp_dir().join(format!("horizon-prepare-runtimes-{}", std::process::id())); + let _ = fs::remove_dir_all(&dir); + let make = |home: PathBuf, javac: bool| { + fs::create_dir_all(home.join("bin")).unwrap(); + if javac { + fs::write(executable(&home.join("bin"), "javac"), b"").unwrap(); + } + home + }; + let gamma = make( + dir.join("java-runtime-gamma/linux/java-runtime-gamma"), + true, + ); + let epsilon = make( + dir.join( + "java-runtime-epsilon/mac-os-arm64/java-runtime-epsilon/jre.bundle/Contents/Home", + ), + true, + ); + make(dir.join("jre-legacy/linux/jre-legacy"), false); + assert_eq!(minecraft_runtimes(&dir), [epsilon, gamma]); + assert!(minecraft_runtimes(&dir.join("missing")).is_empty()); + fs::remove_dir_all(&dir).unwrap(); + } } diff --git a/crates/horizon-prepare/src/lib.rs b/crates/horizon-prepare/src/lib.rs index 66ff671f9..861db254d 100644 --- a/crates/horizon-prepare/src/lib.rs +++ b/crates/horizon-prepare/src/lib.rs @@ -74,6 +74,9 @@ pub struct Options { pub minecraft_dir: Option, /// The pinned server jar the player downloaded (`--server-jar`, else HORIZON_SERVER_JAR). pub server_jar: Option, + /// The JDK to try after JAVA_HOME and the PATH (the launcher's pick, e.g. Minecraft's + /// own Java it downloaded from Mojang). + pub java_home: Option, } pub(crate) fn hex(bytes: &[u8]) -> String { @@ -402,7 +405,11 @@ pub fn prepare(options: &Options, log: Log) -> Result { .map_err(|e| format!("{}: {e}", options.root.display()))?; let lock = artifacts::Lock::read(root)?; // Java first: nothing below can finish without it, and the player should hear it at once. - let jdk = java::find(lock.java_major)?; + let mut homes: Vec = options.java_home.iter().cloned().collect(); + homes.extend(java::minecraft_runtimes( + &artifacts::minecraft_dir(options.minecraft_dir.as_deref()).join("runtime"), + )); + let jdk = java::find(lock.java_major, &homes)?; if options.import && !artifacts::reuse_imported(root, log)? { artifacts::import( root, diff --git a/crates/horizon-prepare/src/main.rs b/crates/horizon-prepare/src/main.rs index 71b7a40cc..0d3a5685d 100644 --- a/crates/horizon-prepare/src/main.rs +++ b/crates/horizon-prepare/src/main.rs @@ -62,6 +62,7 @@ fn main() -> ExitCode { import: false, minecraft_dir: None, server_jar: None, + java_home: None, }; let mut root = None; let mut args = std::env::args_os().skip(1).peekable(); diff --git a/launcher/src/backend.rs b/launcher/src/backend.rs index ba854730f..7fce7ce07 100644 --- a/launcher/src/backend.rs +++ b/launcher/src/backend.rs @@ -993,6 +993,61 @@ fn download_for_owner( } } +/// The home of a JDK of at least `major`: one horizon-prepare finds (JAVA_HOME, the PATH, an +/// installed JDK) or the Java the official launcher downloaded into one of the `games` +/// folders, else Minecraft's own Java downloaded from Mojang into Horizon's game folder, the +/// runtime the version JSON at `manifest` names (Mojang's runtimes are whole JDKs). +fn java( + major: i64, + games: &[PathBuf], + manifest: &Path, + log: &dyn Fn(String), +) -> Result { + let runtimes = || -> Vec { + let mut homes: Vec = games + .iter() + .flat_map(|game| horizon_prepare::java::minecraft_runtimes(&game.join("runtime"))) + .collect(); + homes.dedup(); + homes + }; + let missing = match horizon_prepare::java::find(major, &runtimes()) { + Ok(jdk) => return Ok(jdk.home()), + Err(e) => e, + }; + let component = read_json(manifest) + .and_then(|json| json["javaVersion"]["component"].as_str().map(String::from)) + .ok_or_else(|| missing.clone())?; + let fetch = |url: &str| -> Result, String> { + let mut body = Vec::new(); + get(url)? + .into_reader() + .read_to_end(&mut body) + .map_err(|e| e.to_string())?; + Ok(body) + }; + let runtime = data_dir().join("minecraft").join("runtime"); + let home = mojang::java_runtime(&component, &runtime, 8, &fetch, log).map_err(|e| { + format!("{missing}\n • Horizon also tried to download Minecraft's own Java from Mojang, but: {e}") + })?; + horizon_prepare::java::find(major, &[home]).map(|jdk| jdk.home()) +} + +/// Runs `cmd` with the JDK at `home`: JAVA_HOME names it and its `bin` leads the PATH, as the +/// versions' own preparers look for Java. +fn with_java(cmd: &mut Command, home: &Path) { + let mut path = vec![home.join("bin")]; + path.extend( + std::env::var_os("PATH") + .iter() + .flat_map(std::env::split_paths), + ); + cmd.env("JAVA_HOME", home); + if let Ok(path) = std::env::join_paths(path) { + cmd.env("PATH", path); + } +} + /// Checks what the preparation needs and picks how to run it. PLAY.txt describes the same /// steps: the player's own launcher install, the server jar from that version's own json, and /// Java (and Python, for versions before horizon-prepare). Also returns the Minecraft folder @@ -1006,6 +1061,7 @@ pub fn preparer( let version = game_version(tag); let dir = version_dir(tag); let reused = horizon_prepare::artifacts::reuse_imported(&dir, &|line| log(line.to_string()))?; + let chosen = minecraft.to_path_buf(); let minecraft = if reused { minecraft.to_path_buf() } else { @@ -1042,25 +1098,27 @@ pub fn preparer( .join(&version) .join(format!("{version}.json")); let binary = preparer_binary(&dir); - let python = if binary.is_some() { - // The JDK the version pins, found as horizon-prepare finds it, so a missing one is - // reported before the server jar is downloaded. - let major = read_json(&dir.join("research-lock.json")) - .and_then(|lock| lock["minecraft"]["java_major"].as_i64()) - .ok_or("The version's research-lock.json has no java_major")?; - horizon_prepare::java::find(major)?; - None - } else { - let python = python() - .ok_or("This version prepares with Python: Python 3.10 or newer is needed. Install it, or pick a newer version.")?; - if !found("java", "-version") { - return Err( - "Java 25 is needed (a JDK, e.g. Eclipse Temurin 25). Install it and try again." - .into(), - ); - } - Some(python) + let python = match binary { + Some(_) => None, + None => Some(python().ok_or( + "This version prepares with Python: Python 3.10 or newer is needed. Install it, or pick a newer version.", + )?), }; + // The JDK the version pins, found as horizon-prepare finds it, so a missing one is + // reported (or downloaded) before the server jar is downloaded. Versions from before + // java_major ran Java 25. + let major = read_json(&dir.join("research-lock.json")) + .and_then(|lock| lock["minecraft"]["java_major"].as_i64()) + .unwrap_or(25); + let mut games = vec![chosen, minecraft.clone(), data_dir().join("minecraft")]; + games.extend( + sources::Folders::current() + .map(|f| sources::known_folders(sources::Os::current(), &f)) + .unwrap_or_default() + .into_iter() + .map(|(_, dir)| dir), + ); + let java_home = java(major, &games, &manifest, log)?; let jar = data_dir() .join("server-jars") .join(format!("{version}.jar")); @@ -1092,10 +1150,12 @@ pub fn preparer( import: !reused, minecraft_dir: Some(minecraft.clone()), server_jar: (!reused).then_some(jar), + java_home: Some(java_home), }; return Ok((Preparer::InProcess(options), minecraft)); } let mut cmd = Command::new(binary); + with_java(&mut cmd, &java_home); cmd.current_dir(&dir).arg("--root").arg(&dir); if !reused { cmd.args(["--prepare", "--server-jar"]).arg(&jar); @@ -1104,6 +1164,7 @@ pub fn preparer( return Ok((Preparer::Process(cmd), minecraft)); } let mut cmd = Command::new(python.unwrap()); + with_java(&mut cmd, &java_home); cmd.current_dir(&dir) .args(["-u", "tools/prepare_native_client.py"]); if !reused { diff --git a/launcher/src/mojang.rs b/launcher/src/mojang.rs index dbe2aa7fb..070db43aa 100644 --- a/launcher/src/mojang.rs +++ b/launcher/src/mojang.rs @@ -13,6 +13,8 @@ use std::sync::Mutex; use std::time::Duration; const OBJECTS: &str = "https://resources.download.minecraft.net"; +/// The index of Mojang's Java runtimes, the one the official launcher reads. +const JAVA_RUNTIMES: &str = "https://launchermeta.mojang.com/v1/products/java-runtime/2ec0cc96c44e5a76b9c8b7c39df7210883d12871/all.json"; /// One file to download. struct File { @@ -84,6 +86,120 @@ pub fn download( Ok(()) } +/// Mojang's name for this system in its Java runtime index. +fn java_platform() -> Option<&'static str> { + Some(match (std::env::consts::OS, std::env::consts::ARCH) { + ("macos", "aarch64") => "mac-os-arm64", + ("macos", "x86_64") => "mac-os", + ("linux", "x86_64") => "linux", + ("linux", "x86") => "linux-i386", + ("windows", "x86_64") => "windows-x64", + ("windows", "aarch64") => "windows-arm64", + ("windows", "x86") => "windows-x86", + _ => return None, + }) +} + +/// Downloads Mojang's Java runtime `component` (the version JSON's `javaVersion.component`, e.g. +/// `java-runtime-epsilon`) into the game directory's `runtime` folder, as the official launcher +/// lays it out: `///`. Each file is checked against its sha1; +/// files already there and whole are kept. Mojang's runtimes are whole JDKs, javac included, +/// which is what the exporters need. +pub fn java_runtime( + component: &str, + runtime: &Path, + jobs: usize, + fetch: Fetch, + log: &dyn Fn(String), +) -> Result { + let platform = java_platform().ok_or("Mojang has no Java for this system")?; + let index: Value = serde_json::from_slice(&fetch(JAVA_RUNTIMES)?) + .map_err(|e| format!("Mojang's Java index: {e}"))?; + let entry = &index[platform][component][0]; + if entry.is_null() { + return Err(format!("Mojang has no {component} for {platform}")); + } + let manifest = &entry["manifest"]; + let manifest: Value = serde_json::from_slice(&fetch_checked( + fetch, + &text(manifest, "url")?, + &text(manifest, "sha1")?, + )?) + .map_err(|e| format!("Mojang's {component} manifest: {e}"))?; + let files = manifest["files"] + .as_object() + .ok_or_else(|| format!("Mojang's {component} manifest lists no files"))?; + let home = runtime.join(component).join(platform).join(component); + let mut downloads = Vec::new(); + let mut executables = Vec::new(); + let mut links = Vec::new(); + for (name, file) in files { + let path = home.join(relative(name)?); + match file["type"].as_str() { + Some("file") => { + let raw = &file["downloads"]["raw"]; + if file["executable"] == true { + executables.push(path.clone()); + } + downloads.push(File { + url: text(raw, "url")?, + sha1: text(raw, "sha1")?, + path, + }); + } + Some("link") => links.push((path, text(file, "target")?)), + _ => {} + } + } + let name = entry["version"]["name"].as_str().unwrap_or(component); + log(format!( + "Downloading Minecraft's Java {name} from Mojang ({} files)", + downloads.len() + )); + fetch_all(&downloads, jobs, fetch, log)?; + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + for path in &executables { + fs::set_permissions(path, fs::Permissions::from_mode(0o755)) + .map_err(|e| format!("{}: {e}", path.display()))?; + } + for (path, target) in &links { + if !link_stays_inside(name_in(&home, path), target) { + return Err(format!( + "Unsafe link {target:?} in Mojang's {component} manifest" + )); + } + let _ = fs::remove_file(path); + fs::create_dir_all(path.parent().unwrap()) + .and_then(|_| std::os::unix::fs::symlink(target, path)) + .map_err(|e| format!("{}: {e}", path.display()))?; + } + } + #[cfg(not(unix))] + let _ = (executables, links); + Ok(home) +} + +fn name_in<'a>(home: &Path, path: &'a Path) -> &'a Path { + path.strip_prefix(home).unwrap_or(path) +} + +/// Whether a link at `link` (relative to the runtime's home) pointing at `target` stays in the +/// home. +fn link_stays_inside(link: &Path, target: &str) -> bool { + let mut depth = link.components().count().saturating_sub(1); + for c in Path::new(target).components() { + match c { + Component::Normal(_) => depth += 1, + Component::CurDir => {} + Component::ParentDir if depth > 0 => depth -= 1, + _ => return false, + } + } + true +} + fn read_json(path: &Path) -> Result { let text = fs::read_to_string(path).map_err(|e| format!("{}: {e}", path.display()))?; serde_json::from_str(&text).map_err(|e| format!("{}: {e}", path.display())) diff --git a/launcher/src/mojang_tests.rs b/launcher/src/mojang_tests.rs index b50a77a93..24ed80bca 100644 --- a/launcher/src/mojang_tests.rs +++ b/launcher/src/mojang_tests.rs @@ -230,3 +230,99 @@ fn version_file_paths_stay_in_the_folder() { assert!(relative(bad).is_err(), "{bad}"); } } + +#[test] +fn links_stay_inside_the_runtime() { + let at = |p: &str| PathBuf::from(p); + assert!(link_stays_inside( + &at("legal/java.compiler/LICENSE"), + "../java.base/LICENSE" + )); + assert!(link_stays_inside(&at("lib/a"), "b")); + assert!(!link_stays_inside(&at("lib/a"), "../../b")); + assert!(!link_stays_inside(&at("a"), "/etc/passwd")); +} + +#[cfg(all(target_os = "linux", target_arch = "x86_64"))] +#[test] +fn java_runtime_lays_out_mojangs_runtime_with_its_executables_and_links() { + const JAVAC: &[u8] = b"#!/bin/sh\necho javac 25.0.1\n"; + const LICENSE: &[u8] = b"a fake license"; + let manifest = serde_json::json!({"files": { + "bin": {"type": "directory"}, + "bin/javac": {"type": "file", "executable": true, "downloads": {"raw": + {"sha1": sha1_hex(JAVAC), "size": JAVAC.len(), "url": "https://piston-data.mojang.com/javac"}}}, + "legal/java.base/LICENSE": {"type": "file", "executable": false, "downloads": {"raw": + {"sha1": sha1_hex(LICENSE), "size": LICENSE.len(), "url": "https://piston-data.mojang.com/license"}}}, + "legal/java.compiler/LICENSE": {"type": "link", "target": "../java.base/LICENSE"}, + }}) + .to_string(); + let index = serde_json::json!({"linux": {"java-runtime-epsilon": [{ + "manifest": {"sha1": sha1_hex(manifest.as_bytes()), "url": "https://piston-meta.mojang.com/m.json"}, + "version": {"name": "25.0.1"}, + }]}}) + .to_string(); + let server = Server { + files: HashMap::from([ + (JAVA_RUNTIMES.to_string(), index.into_bytes()), + ( + "https://piston-meta.mojang.com/m.json".into(), + manifest.into_bytes(), + ), + ( + "https://piston-data.mojang.com/javac".into(), + JAVAC.to_vec(), + ), + ( + "https://piston-data.mojang.com/license".into(), + LICENSE.to_vec(), + ), + ]), + asked: Mutex::new(Vec::new()), + }; + let dir = scratch("java"); + let home = java_runtime( + "java-runtime-epsilon", + &dir, + 2, + &|url| server.fetch(url), + &|_| {}, + ) + .unwrap(); + assert_eq!( + home, + dir.join("java-runtime-epsilon/linux/java-runtime-epsilon") + ); + use std::os::unix::fs::PermissionsExt; + let mode = fs::metadata(home.join("bin/javac")) + .unwrap() + .permissions() + .mode(); + assert_eq!(mode & 0o111, 0o111); + assert_eq!( + fs::read(home.join("legal/java.compiler/LICENSE")).unwrap(), + LICENSE + ); + // horizon-prepare finds it where the official launcher would have put it. + assert_eq!(horizon_prepare::java::minecraft_runtimes(&dir), [home]); + // A second call keeps the files: only the index and manifest are asked again. + server.asked(); + java_runtime( + "java-runtime-epsilon", + &dir, + 2, + &|url| server.fetch(url), + &|_| {}, + ) + .unwrap(); + assert_eq!(server.asked().len(), 2); + assert!(java_runtime( + "java-runtime-zeta", + &dir, + 2, + &|url| server.fetch(url), + &|_| {} + ) + .is_err()); + fs::remove_dir_all(&dir).unwrap(); +} diff --git a/website/docs/install.md b/website/docs/install.md index b898d79de..8f0df0834 100644 --- a/website/docs/install.md +++ b/website/docs/install.md @@ -5,7 +5,7 @@ Download the launcher, let it install Horizon, then prepare the game from your o ## What you need - **Minecraft: Java Edition 26.3, played once.** Install it with the official Minecraft launcher (or Prism Launcher, MultiMC, the Modrinth App, ATLauncher or GDLauncher) and start 26.3 once, so it downloads its files. Horizon reads them from there and never copies them anywhere else. -- **Java 25, a JDK.** For example [Eclipse Temurin 25](https://adoptium.net/temurin/releases/?version=25). Only the one-time preparation uses it. The game itself needs no Java. +- **Java 25, a JDK**, for the one-time preparation only (the game itself needs no Java). Nothing to install: the launcher uses the Java the Minecraft launcher downloaded for 26.3, or downloads that same Java from Mojang. A JDK you installed yourself (for example [Eclipse Temurin 25](https://adoptium.net/temurin/releases/?version=25)) works too. - **A computer on the [supported list](/docs/#supported-systems).** On Linux and Windows that means a graphics card with Vulkan (or DirectX 12 on Windows). ## Download the launcher diff --git a/website/docs/troubleshooting.md b/website/docs/troubleshooting.md index b7dac8fe4..c62802186 100644 --- a/website/docs/troubleshooting.md +++ b/website/docs/troubleshooting.md @@ -30,7 +30,7 @@ The launcher's **Settings** page has a **Logs** row with an **Open** button. On - **The game does not start, or crashes straight away.** Update the launcher and Horizon, and your graphics drivers. On Windows, try **Graphics API: DirectX 12** (see [Settings](/docs/settings#graphics-api-on-windows)). - **"Not prepared" or "Minecraft 26.3 was not found".** Play 26.3 once in the official Minecraft launcher, or point the launcher at your Minecraft folder (**Choose…** next to Minecraft folder on the Play page), then choose **Prepare** again. -- **Preparation says Java 25 is needed.** Install a Java 25 JDK, for example Eclipse Temurin 25. +- **Preparation says Java 25 is needed.** Update the launcher: it then uses the Java the Minecraft launcher downloaded for 26.3, or downloads it from Mojang. If it still can't (no internet, say), install a Java 25 JDK, for example Eclipse Temurin 25. - **Slow or stuttering.** Lower the render distance, turn LOD off, or pick the **Performance** preset. The performance overlay (Video, Advanced, Overlay) shows what is slow. - **A resource or shader pack broke something.** Switch it off, or take it out of its folder. From 92d2e2b86daee54096cde44bc404c528e5e7af77 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Thu, 22 Oct 2026 16:20:42 +0200 Subject: [PATCH 23/29] launcher: leave Java alone when the preparer finds one itself With a JDK installed, the launcher passed the preparation the home of the JDK horizon-prepare finds (javac/../..) as JAVA_HOME and on the PATH. Found through the PATH, that is often a stub: on macOS /usr/bin/javac gives JAVA_HOME=/usr, and `JAVA_HOME=/usr /usr/bin/javac -version` never returns (the stub calls itself), so the preparer subprocess hung on a Mac with Temurin installed. When horizon-prepare finds a JDK by itself, run the preparation as before, in an unchanged environment; only Minecraft's own Java (the official launcher's, or one downloaded from Mojang) is passed on. Also skip the Mojang download on x86-64 Linux without glibc's loader (NixOS without nix-ld, musl distributions): Mojang's Java can't start there, so say so instead of downloading 115 MB first. And keep each runtime home once (dedup only dropped neighbours). Co-Authored-By: Claude Opus 5.5 --- launcher/src/backend.rs | 59 ++++++++++++++++++++++++++++------------- 1 file changed, 40 insertions(+), 19 deletions(-) diff --git a/launcher/src/backend.rs b/launcher/src/backend.rs index 7fce7ce07..51ee81c5c 100644 --- a/launcher/src/backend.rs +++ b/launcher/src/backend.rs @@ -993,31 +993,45 @@ fn download_for_owner( } } -/// The home of a JDK of at least `major`: one horizon-prepare finds (JAVA_HOME, the PATH, an -/// installed JDK) or the Java the official launcher downloaded into one of the `games` -/// folders, else Minecraft's own Java downloaded from Mojang into Horizon's game folder, the -/// runtime the version JSON at `manifest` names (Mojang's runtimes are whole JDKs). +/// A JDK of at least `major` for the preparation. `None` when horizon-prepare finds one by +/// itself (JAVA_HOME, the PATH, an installed JDK): the preparation then runs as before, in an +/// unchanged environment (a JDK found on the PATH can be a stub such as macOS's +/// /usr/bin/javac, whose folder is no Java home: `JAVA_HOME=/usr` makes it call itself +/// forever). Else the home of the Java the official launcher downloaded into one of the +/// `games` folders, else of Minecraft's own Java downloaded from Mojang into Horizon's game +/// folder, the runtime the version JSON at `manifest` names (Mojang's runtimes are whole JDKs). fn java( major: i64, games: &[PathBuf], manifest: &Path, log: &dyn Fn(String), -) -> Result { - let runtimes = || -> Vec { - let mut homes: Vec = games - .iter() - .flat_map(|game| horizon_prepare::java::minecraft_runtimes(&game.join("runtime"))) - .collect(); - homes.dedup(); - homes - }; - let missing = match horizon_prepare::java::find(major, &runtimes()) { - Ok(jdk) => return Ok(jdk.home()), +) -> Result, String> { + if horizon_prepare::java::find(major, &[]).is_ok() { + return Ok(None); + } + let mut runtimes: Vec = Vec::new(); + for home in games + .iter() + .flat_map(|game| horizon_prepare::java::minecraft_runtimes(&game.join("runtime"))) + { + if !runtimes.contains(&home) { + runtimes.push(home); + } + } + let missing = match horizon_prepare::java::find(major, &runtimes) { + Ok(jdk) => return Ok(Some(jdk.home())), Err(e) => e, }; let component = read_json(manifest) .and_then(|json| json["javaVersion"]["component"].as_str().map(String::from)) .ok_or_else(|| missing.clone())?; + // Mojang builds its Linux Java for glibc: without glibc's loader (NixOS without nix-ld, + // musl distributions) it cannot start, so don't download it. + if cfg!(all(target_os = "linux", target_arch = "x86_64")) && !Path::new(GLIBC_LOADER).exists() { + return Err(format!( + "{missing}\n • Minecraft's own Java from Mojang needs glibc ({GLIBC_LOADER} is missing; on NixOS, enable programs.nix-ld), so install a JDK instead." + )); + } let fetch = |url: &str| -> Result, String> { let mut body = Vec::new(); get(url)? @@ -1030,9 +1044,12 @@ fn java( let home = mojang::java_runtime(&component, &runtime, 8, &fetch, log).map_err(|e| { format!("{missing}\n • Horizon also tried to download Minecraft's own Java from Mojang, but: {e}") })?; - horizon_prepare::java::find(major, &[home]).map(|jdk| jdk.home()) + horizon_prepare::java::find(major, &[home]).map(|jdk| Some(jdk.home())) } +/// The dynamic loader glibc programs on x86-64 Linux start through. +const GLIBC_LOADER: &str = "/lib64/ld-linux-x86-64.so.2"; + /// Runs `cmd` with the JDK at `home`: JAVA_HOME names it and its `bin` leads the PATH, as the /// versions' own preparers look for Java. fn with_java(cmd: &mut Command, home: &Path) { @@ -1150,12 +1167,14 @@ pub fn preparer( import: !reused, minecraft_dir: Some(minecraft.clone()), server_jar: (!reused).then_some(jar), - java_home: Some(java_home), + java_home: java_home.clone(), }; return Ok((Preparer::InProcess(options), minecraft)); } let mut cmd = Command::new(binary); - with_java(&mut cmd, &java_home); + if let Some(home) = &java_home { + with_java(&mut cmd, home); + } cmd.current_dir(&dir).arg("--root").arg(&dir); if !reused { cmd.args(["--prepare", "--server-jar"]).arg(&jar); @@ -1164,7 +1183,9 @@ pub fn preparer( return Ok((Preparer::Process(cmd), minecraft)); } let mut cmd = Command::new(python.unwrap()); - with_java(&mut cmd, &java_home); + if let Some(home) = &java_home { + with_java(&mut cmd, home); + } cmd.current_dir(&dir) .args(["-u", "tools/prepare_native_client.py"]); if !reused { From 97edfec91491825e44c10d8bb754347b95daa146 Mon Sep 17 00:00:00 2001 From: rdbtCVS <87152661+rdbtCVS@users.noreply.github.com> Date: Thu, 22 Oct 2026 15:46:39 +0100 Subject: [PATCH 24/29] perf: reuse decoded aquifer neighborhoods during terrain fill --- .../src/worldgen/aquifer/noise_based.rs | 106 +++++++++++------- docs/aquifer-cache-benchmark.md | 56 +++++++++ 2 files changed, 124 insertions(+), 38 deletions(-) create mode 100644 docs/aquifer-cache-benchmark.md diff --git a/crates/horizon-world/src/worldgen/aquifer/noise_based.rs b/crates/horizon-world/src/worldgen/aquifer/noise_based.rs index 30181686b..3f3c7dc2c 100644 --- a/crates/horizon-world/src/worldgen/aquifer/noise_based.rs +++ b/crates/horizon-world/src/worldgen/aquifer/noise_based.rs @@ -120,6 +120,7 @@ pub struct NoiseBasedAquifer { statuses: Vec>, locations: Vec, surfaces: HashMap, + neighborhood: Option<([i32; 3], [(usize, BlockPos); 12])>, skip_above_y: i32, schedule: bool, } @@ -180,6 +181,7 @@ impl NoiseBasedAquif statuses, locations, surfaces: HashMap::new(), + neighborhood: None, skip_above_y: 0, schedule: false, }; @@ -479,48 +481,76 @@ impl NoiseBasedAquif y.wrapping_add(1).div_euclid(12), z.wrapping_sub(5) >> 4, ]; - let mut distances = [i32::MAX; 4]; - let mut indices = [0usize; 4]; - for dx in 0..=1i32 { - for dy in -1..=1i32 { - for dz in 0..=1i32 { - let cell = [ - grid[0].wrapping_add(dx), - grid[1].wrapping_add(dy), - grid[2].wrapping_add(dz), - ]; - let index = self.grid_index(cell)?; - if self.locations[index] == i64::MAX { - let mut random = self.random.at(BlockPos::new(cell[0], cell[1], cell[2])); - let px = cell[0] - .wrapping_shl(4) - .wrapping_add(random.next_int_bound(10).expect("positive bound")); - let py = cell[1] - .wrapping_mul(12) - .wrapping_add(random.next_int_bound(9).expect("positive bound")); - let pz = cell[2] - .wrapping_shl(4) - .wrapping_add(random.next_int_bound(10).expect("positive bound")); - self.locations[index] = BlockPos::new(px, py, pz).pack(); - } - let point = BlockPos::unpack(self.locations[index]); - let ax = point.x.wrapping_sub(x); - let ay = point.y.wrapping_sub(y); - let az = point.z.wrapping_sub(z); - let distance = ax - .wrapping_mul(ax) - .wrapping_add(ay.wrapping_mul(ay)) - .wrapping_add(az.wrapping_mul(az)); - if let Some(slot) = distances.iter().position(|old| distance <= *old) { - for i in (slot + 1..4).rev() { - distances[i] = distances[i - 1]; - indices[i] = indices[i - 1]; + // Fill in the original cell order, publishing only after checked traversal + // succeeds. Failed traversal keeps the last complete neighborhood. + if self.neighborhood.as_ref().map(|(key, _)| *key) != Some(grid) { + let mut entries = [(0, BlockPos::default()); 12]; + let mut next = 0; + for dx in 0..=1i32 { + for dy in -1..=1i32 { + for dz in 0..=1i32 { + let cell = [ + grid[0].wrapping_add(dx), + grid[1].wrapping_add(dy), + grid[2].wrapping_add(dz), + ]; + let index = self.grid_index(cell)?; + if self.locations[index] == i64::MAX { + let mut random = + self.random.at(BlockPos::new(cell[0], cell[1], cell[2])); + let px = cell[0] + .wrapping_shl(4) + .wrapping_add(random.next_int_bound(10).expect("positive bound")); + let py = cell[1] + .wrapping_mul(12) + .wrapping_add(random.next_int_bound(9).expect("positive bound")); + let pz = cell[2] + .wrapping_shl(4) + .wrapping_add(random.next_int_bound(10).expect("positive bound")); + self.locations[index] = BlockPos::new(px, py, pz).pack(); } - distances[slot] = distance; - indices[slot] = index; + entries[next] = (index, BlockPos::unpack(self.locations[index])); + next += 1; } } } + self.neighborhood = Some((grid, entries)); + } + let mut distances = [i32::MAX; 4]; + let mut indices = [0usize; 4]; + for &(index, point) in &self + .neighborhood + .as_ref() + .expect("populated neighborhood") + .1 + { + let ax = point.x.wrapping_sub(x); + let ay = point.y.wrapping_sub(y); + let az = point.z.wrapping_sub(z); + let distance = ax + .wrapping_mul(ax) + .wrapping_add(ay.wrapping_mul(ay)) + .wrapping_add(az.wrapping_mul(az)); + // Equal distances insert before existing entries, as in vanilla. + if distance <= distances[0] { + distances = [distance, distances[0], distances[1], distances[2]]; + indices = [index, indices[0], indices[1], indices[2]]; + } else if distance <= distances[1] { + distances[3] = distances[2]; + distances[2] = distances[1]; + distances[1] = distance; + indices[3] = indices[2]; + indices[2] = indices[1]; + indices[1] = index; + } else if distance <= distances[2] { + distances[3] = distances[2]; + distances[2] = distance; + indices[3] = indices[2]; + indices[2] = index; + } else if distance <= distances[3] { + distances[3] = distance; + indices[3] = index; + } } let a = self.status(context, indices[0])?; let s01 = similarity(distances[0], distances[1]); diff --git a/docs/aquifer-cache-benchmark.md b/docs/aquifer-cache-benchmark.md new file mode 100644 index 000000000..09ce2558b --- /dev/null +++ b/docs/aquifer-cache-benchmark.md @@ -0,0 +1,56 @@ +# Aquifer neighborhood cache benchmark + +Caching the most recently used aquifer grid neighborhood reduces complete Overworld proto-generation plus decoration by a median **4.152% with one worker** and **2.377% with four workers** at the primary site. A second site improves **3.865% / 3.275%**. All 20 measured Overworld pairs improve. The implementation changes only `worldgen/aquifer/noise_based.rs`. + +## Implementation and scope + +Keep the twelve decoded cell positions and indices for the current grid in vanilla traversal order. Publish a replacement neighborhood only after every checked grid access succeeds; location entries initialized before an error remain visible in their original order. Reuse the neighborhood while its grid key matches. The existing location/status/surface caches retain their behavior. Explicit four-slot nearest insertion preserves later-cell distance ties (`<=`), wrapping distance arithmetic, random draw order, sampler/error order and fluid-update scheduling. Pinned 26.3 `Aquifer.NoiseBasedAquifer.computeSubstance` bytecode was inspected before implementation. + +No overlap with open aquifer work was found on 2026-10-22. The excluded Perlin, climate, feature-region, ore, biome-temperature and chunk-bookkeeping mechanisms are unchanged. Baseline `753c071c` includes the accepted saved-palette optimization; both variants contain that same loading code. These generation results measure only the aquifer change. + +## Measurements + +AMD Ryzen 5 5600X, 6 cores/12 logical CPUs, 32 GiB RAM; Linux 7.2.11-zen1-1-zen; Rust 1.99.0; release optimization, thin LTO, one codegen unit. Boost stayed enabled with the powersave governor and ordinary desktop activity. Resources: `/srv/bob-data/horizon/.local/horizon-client/26.3`. Both executables were rebuilt from accepted sources with identical benchmark source (SHA256 `f404feb7ec526f12735d11b15af36254fd585f39b975cdbdc71905d87f10f8cc`), dependencies, resources and flags. Builds, tests and profiling did not overlap qualification timing. + +Every row uses one discarded process pair, then five measured pairs alternating baseline/candidate order. All 84 processes produced 256 proto chunks and 196 interior decorations. Warmup generates/discards an offset 9×9 region before fresh measured workers. Primary seed/center: 12345/(0,0); second: -1/(32,-32). Values are process-median **milliseconds for the complete 256-input-chunk batch**, without a thread multiplier. Stage medians need not sum to the combined median. Reductions are medians/ranges of per-pair reductions, so they need not equal the ratio of the independently reported medians. + +| Workload | Proto baseline → candidate | Decoration baseline → candidate | Combined baseline → candidate | Paired combined reduction (range) | +| --- | ---: | ---: | ---: | ---: | +| Primary Overworld, 1 worker | 2224.363 → 2135.304 | 442.671 → 438.347 | 2667.810 → 2575.518 | 4.152% (3.324–5.131%) | +| Primary Overworld, 4 workers | 733.644 → 716.328 | 131.039 → 128.976 | 864.363 → 844.938 | 2.377% (0.736–2.800%) | +| Second Overworld, 1 worker | 1994.369 → 1913.935 | 233.564 → 230.780 | 2230.391 → 2143.154 | 3.865% (3.552–4.144%) | +| Second Overworld, 4 workers | 572.673 → 551.555 | 68.164 → 67.669 | 640.836 → 618.820 | 3.275% (2.561–4.469%) | +| Nether, 1 worker | 969.751 → 962.970 | 87.048 → 85.903 | 1058.483 → 1048.873 | 0.730% (-2.406–1.096%) | +| End, 1 worker | 211.710 → 211.837 | 15.689 → 16.119 | 227.650 → 228.151 | -0.175% (-1.801–1.859%) | +| Primary Overworld, non-warmed, 1 worker | 4361.328 → 4256.020 | 442.196 → 437.629 | 4804.541 → 4691.376 | 2.177% (1.743–4.552%) | + +The non-warmed row retains lazy generator initialization. Resource loading remains outside the generation brackets; whole-process duration was also recorded and its non-warmed paired median improves 1.733%. Nether/End show ordinary variation, including a 0.175% End combined slowdown (about 0.5 ms per batch); no material regression is established there. + +Five alternating streaming pairs after a discarded pair use four workers, radius 4, seed 12345, a three-second flight at speed 4 and 120 Hz. Median fill is 1.21 → 1.19 s with identical 81 generated chunks/504 meshed sections. Flight produces identical 121 chunks/1,704 sections. Fill update p99 is unchanged at 0.041 ms; flight p99 is 0.200 → 0.208 ms and maximum update is 0.28 → 0.23 ms. All twelve runs have zero nearby unlit frames. This short route checks responsiveness and output counts; it is not a long-travel throughput claim. + +## Footprint + +The twelve-entry cache field occupies 312 bytes on this platform, inline in the existing aquifer object, and adds no allocation calls or independently growing buffer. It enlarges the existing transient aquifer object, which is discarded after generation; resident and saved chunk representations are unchanged. Peak RSS medians are 732,032 → 732,088 KiB (primary one-worker) and 756,724 → 759,768 KiB (four-worker). Paired RSS ranges span increases and decreases; the four-worker median difference is about 0.4% of process RSS. The release executable text grows by 3,872 bytes (about 0.075%). + +## Correctness + +- Fresh authenticated vanilla 26.3 aquifer oracle: **379 fixtures, 389,804 exact lines per client/server JAR**. Covers modern/legacy factories, repeated/reversed queries, negative/wrapped coordinates, nonfinite inputs, sampler/picker traces, fluid scheduling, location/status/surface caches, bounds failures and all 18 injected sampler failure/retry cases. +- Fresh terrain-fill oracle: **114 fixtures, 200 fills, 7,950,873 exact lines per JAR**, including 61 noise-aquifer fixtures, 24 structure-density fixtures, repeated/prefilled/clipped fills, raw blocks, WG heightmaps and ordered fluid postprocessing. +- Fresh material-stage oracle: **79 fixtures, 106 surface builds, 42 generated-carving passes, 6,733,382 exact lines per JAR**, including real biome neighborhoods, aquifer traces, carving masks, top-material queries, complete sections, heightmaps and postprocessing. +- **36/36 exact baseline/candidate phase snapshots match**: three dimensions × three seeds/sites × one/four workers × before/after decoration. Each region retains 81 chunks and decorates 49 interior chunks, totaling 1,458 retained chunks per phase and 882 decorations. Seeds/sites are 12345/(0,0), -1/(-32,32), and 81985529216486895/(1874990,-1874990). Snapshots include resident palettes/words and public block reads, quart biomes, all six heightmaps, ordered postprocessing, structure summaries, binary-NBT entities/block entities, complete scheduled ticks, stage/decorated/retrogen metadata and actual non-air counts. Temporary diagnostic source was removed. A final production-only rebuild with the original package feature graph is byte-identical to the measured candidate executable (SHA256 `3b4e07bd9189270c7e0a4521bd25e156f932014d976b1c4208cde4769d5fd8ea`). + +These checks establish unchanged output for the declared cases. Complete world/gameplay parity and cold physical I/O are outside this comparison. + +## Reproduction and evidence + +Private/generated artifacts are outside Git at `/srv/bob-data/horizon-chunk-campaign-41055168/aquifer-qualification/`: preserved binaries/sources and hashes, build logs, `pairs.py`, `matrix-raw.json`, `matrix-summary.json`, every process log, streaming runner/raw/summary, snapshot source/runner/outputs, and fresh oracle reports/inputs/outputs. Oracle runners retain the repository's corpus/comparison/authentication logic, with isolated output storage and absolute hash-map keys; the worldgen runner selects only `noise_aquifer_corpus`. + +```sh +CARGO_BUILD_JOBS=6 CARGO_TARGET_DIR=/srv/bob-data/horizon-chunk-campaign-target cargo build --locked --release -p horizon-world --example chunkgen_bench -p horizon-client --no-default-features --example streaming_bench +python3 /srv/bob-data/horizon-chunk-campaign-41055168/aquifer-qualification/pairs.py +python3 /srv/bob-data/horizon-chunk-campaign-41055168/aquifer-qualification/streaming.py +``` + +The standard warmed generation command is `chunkgen_bench RESOURCES --seed 12345 --size 16 --threads 1 --warmup --json --no-map`; use four workers for the parallel row and `--seed -1 --center 32 -32` for the second site. + +Repository quick self-check passes: `CARGO_BUILD_JOBS=6 CARGO_TARGET_DIR=/srv/bob-data/horizon-chunk-campaign-check-target python3 tools/pr_ready.py --quick --base origin/main` (4 min 23 s). Public-tree validation, workspace rustfmt, clippy with warnings denied, **1,442 client/core/world tests** and the doc-test step pass. The quick check lists affected parity harnesses; the three fresh relevant oracle suites above were executed separately. Full log: `aquifer-qualification/pr-ready.txt`. From a648622b9232023b106c6b1eb4f26b46b1f5fb6f Mon Sep 17 00:00:00 2001 From: rdbtCVS <87152661+rdbtCVS@users.noreply.github.com> Date: Thu, 22 Oct 2026 15:46:40 +0100 Subject: [PATCH 25/29] bench: report exact warmed generation wall timings --- .../horizon-world/examples/chunkgen_bench.rs | 38 ++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/crates/horizon-world/examples/chunkgen_bench.rs b/crates/horizon-world/examples/chunkgen_bench.rs index 4992515c5..23360b637 100644 --- a/crates/horizon-world/examples/chunkgen_bench.rs +++ b/crates/horizon-world/examples/chunkgen_bench.rs @@ -3,6 +3,7 @@ //! chunkgen_bench [--seed S] [--size N] [--threads T] //! [--center CX CZ] [--verify ] [--no-decorate] //! [--dimension overworld|the_nether|the_end] +//! [--warmup] [--json] [--no-map] use horizon_core::position::ChunkPos; use horizon_world::{ DATA_VERSION, @@ -275,6 +276,7 @@ fn main() -> Result<(), Error> { let (mut seed, mut size, mut threads, mut center) = (0i64, 16i32, 2usize, (0, 0)); let mut parity = None; let mut decorate = true; + let (mut warmup, mut json, mut map) = (false, false, true); let mut dimension = String::from("minecraft:overworld"); let mut starts: Option<(i32, i32, i32, i32, PathBuf)> = None; while let Some(arg) = args.next() { @@ -286,6 +288,9 @@ fn main() -> Result<(), Error> { "--center" => center = (next().parse()?, next().parse()?), "--verify" => parity = Some(PathBuf::from(next())), "--no-decorate" => decorate = false, + "--warmup" => warmup = true, + "--json" => json = true, + "--no-map" => map = false, "--dimension" => { let d = next(); dimension = if d.contains(':') { @@ -326,6 +331,16 @@ fn main() -> Result<(), Error> { dump_starts(&generator, seed, [x0, z0, x1, z1], &out)?; return Ok(()); } + if warmup { + let mut worker = generator.worker(); + for z in -4..=4 { + for x in -4..=4 { + std::hint::black_box( + worker.generate_proto(ChunkPos::new(center.0 + 64 + x, center.1 + 64 + z))?, + ); + } + } + } let (x0, z0) = (center.0 - size / 2, center.1 - size / 2); let count = (size * size) as usize; let next = AtomicUsize::new(0); @@ -353,6 +368,7 @@ fn main() -> Result<(), Error> { handles.into_iter().try_for_each(|h| h.join().unwrap()) })?; let elapsed = start.elapsed(); + let cpu_elapsed = cpu_seconds() - cpu; println!( "generated {count} proto chunks with {threads} threads in {elapsed:.2?}: {:.1} chunks/s ({:.1} ms/chunk/thread)", count as f64 / elapsed.as_secs_f64(), @@ -360,7 +376,7 @@ fn main() -> Result<(), Error> { ); println!( " cpu time {:.1} ms/chunk", - (cpu_seconds() - cpu) * 1000. / count as f64 + cpu_elapsed * 1000. / count as f64 ); let timings = timings.into_inner().unwrap(); let per_chunk = |f: fn(&StageTimings) -> std::time::Duration| { @@ -379,6 +395,16 @@ fn main() -> Result<(), Error> { per_chunk(|t| t.surface), per_chunk(|t| t.carvers) ); + if json { + println!( + "BENCH {}", + serde_json::json!({"phase": "proto", "chunks": count, + "threads": threads, "wall_s": elapsed.as_secs_f64(), "cpu_s": cpu_elapsed, + "biomes_ms_chunk": per_chunk(|t| t.biomes), "noise_ms_chunk": per_chunk(|t| t.noise), + "structures_ms_chunk": per_chunk(|t| t.structures), + "surface_ms_chunk": per_chunk(|t| t.surface), "carvers_ms_chunk": per_chunk(|t| t.carvers)}) + ); + } let mut chunks: Vec> = slots .into_iter() .map(|slot| slot.into_inner().unwrap()) @@ -441,6 +467,13 @@ fn main() -> Result<(), Error> { } } let elapsed = start.elapsed(); + if json { + println!( + "BENCH {}", + serde_json::json!({"phase": "decorate", "chunks": decorated, + "wall_s": elapsed.as_secs_f64()}) + ); + } let totals = totals.into_inner().unwrap(); println!( "decorated {decorated} chunks in {elapsed:.2?}: {:.1} chunks/s; feature roots placed {}, unsupported {}, failed {}", @@ -492,6 +525,9 @@ fn main() -> Result<(), Error> { } } } + if !map { + return Ok(()); + } let mut legend = BTreeMap::>::new(); let step = (size * 16 / 96).max(1); for bz in (0..size * 16).step_by(step as usize) { From c9c7cda779e1462e1801bb601d8a9c17c1e00f00 Mon Sep 17 00:00:00 2001 From: rdbtCVS <87152661+rdbtCVS@users.noreply.github.com> Date: Thu, 22 Oct 2026 17:06:09 +0100 Subject: [PATCH 26/29] docs: record the completed ten-agent optimization sequence --- docs/chunk-performance-campaign.md | 44 ++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/docs/chunk-performance-campaign.md b/docs/chunk-performance-campaign.md index 5c9d15001..667b72b01 100644 --- a/docs/chunk-performance-campaign.md +++ b/docs/chunk-performance-campaign.md @@ -2,6 +2,20 @@ Profiling baseline: `41055168afcd8687c7efe525545de896f17f4478` (2026-10-22). No production optimization is included in this profiling step. Measurements identify work to investigate; they do not establish a candidate speedup or complete Minecraft world parity. +## Completed sequence: ten agents + +The ten sequential GPT-6.1 Sol/high agents are complete. Three production changes are retained in independent pull requests: + +| Change | Measured complete-workload reduction | Pull request | +| --- | --- | --- | +| Saved list-palette loading (Round 2) | 10.257% normal saved load | [#1223](https://github.com/ludovic111/horizon/pull/1223) | +| Aquifer neighborhood reuse (Rounds 3 and 7) | Primary generation + decoration: 4.152% / 2.377% at one/four workers | [#1229](https://github.com/ludovic111/horizon/pull/1229) | +| Saved-light reconstruction (Round 8) | 6.179% normal / 10.981% flat saved load, incremental to palette loading | [#1231](https://github.com/ludovic111/horizon/pull/1231) | + +These paired-median reductions have different workloads/baselines and must not be added together or treated as total game startup improvements. Retained changes passed their scoped pinned vanilla oracles, exact baseline/candidate snapshots, and repository quick self-checks. The final retained source passed 1,443 tests with serialization after an initial client-test crash. No universal world/gameplay parity claim is made. + +Surface dispatch, section access and NBT feasibility stopped early. The state-cache key candidate was reverted below the default 5% complete-load gate. Aquifer work resumed under the user's explicit lower threshold, then passed full qualification. The final decompression attempt passed the main loading gate but was reverted because related valid-stream workloads regressed. All rejected production/dependency changes are restored; no eleventh agent was started. Further optimization awaits user feedback. + ## Reproduction and artifacts Machine: AMD Ryzen 5 5600X, 6 cores/12 logical CPUs, 32 GiB RAM, Linux 7.2.11-zen1-1-zen x86_64; Rust/Cargo 1.99.0, OpenJDK 25.0.4.1. CPU boost remained enabled, governor `powersave`; normal desktop background activity was not isolated. Builds, timed workloads and sampling runs were sequential. Release uses `opt-level=3`, thin LTO, one codegen unit; client benchmarks disable audio/controllers and need no window/GPU. Do not compare these numbers directly with earlier `fast` profile reports. @@ -140,3 +154,33 @@ Evidence is retained under `/srv/bob-data/horizon-chunk-campaign-41055168/state- One discarded process pair followed by two alternating measured pairs used `--rounds 5 --limit 256 --checksum`. Complete normal-load paired reductions were **3.257% and 2.412%** (median **2.835%**); baseline/candidate process-median medians were **49.725 / 48.316 ms** per 256 chunks. Native decode improved **5.917%** (20.266 / 19.067 ms); inflate/NBT varied by **-0.844%**. Compressed-record and block/biome/light checksums matched in every process. Those checksums are limited benchmark checks, not comprehensive semantic or vanilla parity evidence. Decision: **early no-go** at the incremental 5% complete-load gate. Both production files were restored exactly to their pre-candidate contents; only this report remains. No flat/long qualification, comprehensive snapshot comparison, fresh parity suite, tests, commit or PR were pursued for this rejected candidate. The shared Cargo target contains the rejected executable; future work must use the preserved accepted binary or rebuild restored sources. + + +## Round 7: aquifer qualification — retained + +The user explicitly accepted the aquifer pilot's smaller gain and later confirmed the repeated 3.3–3.5% observations were sufficient. The candidate was restored and qualified with five alternating process pairs per workload, startup/streaming/memory checks, fresh client/server aquifer/terrain/material oracles, and 36 exact pre/post-decoration snapshot comparisons. All 20 measured Overworld pairs improve; no material regression was established in the declared checks. The cache adds 312 bytes per transient aquifer object with no new allocation calls. Full results, limitations and reproduction are in [PR #1229](https://github.com/ludovic111/horizon/pull/1229). + +## Round 8: saved-light loading — retained + +Detect uniform saved nibble layers before allocation and decode remaining layers in byte pairs. Final-source complete loading improves by paired medians 6.179% normal and 10.981% flat, incremental to the accepted palette optimization. All ten measured pairs improve. All 691 full metadata snapshots and 50,331,648 fresh pinned Java light values match; the repository quick self-check passes with 1,443 tests. The separate native-decode loop measured slower despite unchanged source; its cause remains unisolated and is disclosed in [PR #1231](https://github.com/ludovic111/horizon/pull/1231). + +## Round 9: NBT payload and modified-UTF fast paths — early no-go + +Inspected the unchanged NBT reader at accepted HEAD `fb2be075` and its actual release assembly in the preserved accepted saved-light executable; captured pinned 26.3 integer-array/JDK modified-UTF bytecode for evidence. Checked the open-PR catalog on 2026-10-22; no NBT-reader optimization overlap was found. Two concrete candidates were bulk big-endian integer-array decoding after one checked slice, avoiding repeated per-element bounds/offset/push checks, and an ASCII widening path before the existing lossless modified-UTF decoder. The latter must retain raw NUL, accepted overlong sequences and unpaired UTF-16 surrogates; arrays must retain count/byte-limit/truncation errors and offsets. + +Decision: **early no-go before implementation**. The valid original mixed-loader profile attributes 4.08% exclusive to all `Reader::payload` work and 2.96% exclusive to all `Reader::string` work; NBT runs in both complete-load and inflate/NBT loops. These are not complete-load shares or strict speedup ceilings. Array decoding affects only part of payload work, while ASCII widening retains string allocation and map construction. Neither narrow candidate offers a credible incremental 5% complete `SaveDir::load` reduction; combining them would still require unusually large savings across both functions. The accepted palette and saved-light improvements are already in HEAD and are not counted again. No production edits, builds, timing runs, tests, parity jobs, candidate, commit or PR were made; no new performance/correctness result is claimed. + +Bounded evidence is outside Git at `/srv/bob-data/horizon-chunk-campaign-41055168/nbt-feasibility/`: source/accepted-binary hashes, assembly, pinned bytecode and the open-PR catalog. NBT source SHA256 remains `a32fd0ac8fe94ab1b23c000b51c3a81b28d40c518c5befb5272818792d1cf1ac`. The separate decompression lead for Round 10 is the existing profile's `inflate_fast_help_avx2` at 15.71% exclusive; no backend investigation was performed in this round. + + +## Round 10: whole-buffer zlib decompression — rejected + +Inspected the actual region decoder (`crates/horizon-world/src/compression.rs`), its flate2/zlib-rs backend, current open PRs and the official libdeflate/libdeflater API and portability documentation. No overlapping decompression PR was found on 2026-10-22. Tested libdeflater 1.26.1 as a safe whole-buffer zlib fast path, with the original decoder retained for errors and output limits. Gzip, uncompressed records, LZ4 and all encoding paths were unchanged. The initial output buffer used compressed length ×16, clamped to 16–128 KiB and the caller's output limit. All 3,285 scanned real zlib records, including proto chunks, fit that policy. + +The preserved accepted `fb2be075` saved-light executable is the baseline; source/dependency/resource/corpus hashes and release flags match the campaign protocol. After a discarded pair, the two-pair normal pilot improved complete loading by **11.908% and 11.941%**. The formatted candidate then completed five alternating process pairs per corpus after a discarded pair, with no concurrent builds/tests/profiles. Complete normal loading improved **11.079% (9.548–12.314%)**, from median 46.642 to 41.474 ms per 256 chunks; inflate/NBT improved 18.452%. Flat complete loading improved **2.568% (1.915–7.363%)**, 10.635 to 10.367 ms, supporting no 5% flat claim. All timed checksums match. Peak RSS ranges overlap (normal baseline/candidate medians 161,656/161,008 KiB; flat 160,860/160,188 KiB). The normal gain is incremental to accepted palette, aquifer and saved-light changes. + +Partial correctness gates passed for that candidate: four codec tests, including differential valid/damaged/truncated/trailing/concatenated/dictionary/limit streams and all 65,536 zlib headers; all **691 comprehensive loaded-metadata snapshots** match the accepted source byte-for-byte. A fresh pinned Minecraft 26.3 `RegionFileVersion` oracle generated 62 valid fixtures covering all four codecs, large/high-ratio data and Java zlib levels 0–9 with all three strategies. Both Rust implementations decode these exactly; Horizon encoding bytes match the original, and pinned Minecraft decodes all 62 Horizon outputs exactly. These are focused loading/codec checks, not a complete world-parity claim. + +Decision: **no-go because related valid-input workloads materially regress**. The initial bounded fast path adds a failed trial before legacy decode on high-ratio/large streams; isolated decode probes showed roughly 26–39% regressions for representative 60/64/128 KiB cases. Geometric output growth was also rejected after poor uniform-input results. A final bounded policy restricted the fast path to compressed records of 2–64 KiB, resolving those first cases, but 24 additional controlled-compressibility fixtures exposed **27–71% isolated decode regressions near 131 KiB**, about **26–27% at 256 KiB**, and about **9% on one 1 MiB case**. These microbenchmarks use the original and candidate decoder in the same release executable, a discarded pair and five alternating measured pairs; they are related-workload evidence, not complete-world timing claims. No complete-load gain is claimed for the final gated policy. Removing this tradeoff requires further design beyond this bounded attempt. + +All production and Cargo changes were restored exactly, and owned temporary examples were removed. No final repository quick-check, additional parity work, commit or PR was pursued after rejection. The shared release target contains rejected candidate artifacts and must not be used as an accepted baseline. Earlier campaign documentation, including the uncommitted Round 9 report, is preserved. Evidence remains outside Git at `/srv/bob-data/horizon-chunk-campaign-41055168/decompression/`: authenticated sources and binaries for each policy, dependencies, build/test logs, paired runners/raw observations, RSS, fresh Java fixtures/bytecode, 691 snapshot outputs, controlled-compressibility probes and `decision.json`. Agent 10 ends this optimization sequence; further work awaits user feedback. From fba42920e4c112893ceab14989e83069d113d898 Mon Sep 17 00:00:00 2001 From: rdbtCVS <87152661+rdbtCVS@users.noreply.github.com> Date: Thu, 22 Oct 2026 17:15:41 +0100 Subject: [PATCH 27/29] Remove benchmark report from pull request --- docs/chunk-performance-campaign.md | 186 ----------------------------- 1 file changed, 186 deletions(-) delete mode 100644 docs/chunk-performance-campaign.md diff --git a/docs/chunk-performance-campaign.md b/docs/chunk-performance-campaign.md deleted file mode 100644 index 667b72b01..000000000 --- a/docs/chunk-performance-campaign.md +++ /dev/null @@ -1,186 +0,0 @@ -# Chunk generation and loading performance campaign - -Profiling baseline: `41055168afcd8687c7efe525545de896f17f4478` (2026-10-22). No production optimization is included in this profiling step. Measurements identify work to investigate; they do not establish a candidate speedup or complete Minecraft world parity. - -## Completed sequence: ten agents - -The ten sequential GPT-6.1 Sol/high agents are complete. Three production changes are retained in independent pull requests: - -| Change | Measured complete-workload reduction | Pull request | -| --- | --- | --- | -| Saved list-palette loading (Round 2) | 10.257% normal saved load | [#1223](https://github.com/ludovic111/horizon/pull/1223) | -| Aquifer neighborhood reuse (Rounds 3 and 7) | Primary generation + decoration: 4.152% / 2.377% at one/four workers | [#1229](https://github.com/ludovic111/horizon/pull/1229) | -| Saved-light reconstruction (Round 8) | 6.179% normal / 10.981% flat saved load, incremental to palette loading | [#1231](https://github.com/ludovic111/horizon/pull/1231) | - -These paired-median reductions have different workloads/baselines and must not be added together or treated as total game startup improvements. Retained changes passed their scoped pinned vanilla oracles, exact baseline/candidate snapshots, and repository quick self-checks. The final retained source passed 1,443 tests with serialization after an initial client-test crash. No universal world/gameplay parity claim is made. - -Surface dispatch, section access and NBT feasibility stopped early. The state-cache key candidate was reverted below the default 5% complete-load gate. Aquifer work resumed under the user's explicit lower threshold, then passed full qualification. The final decompression attempt passed the main loading gate but was reverted because related valid-stream workloads regressed. All rejected production/dependency changes are restored; no eleventh agent was started. Further optimization awaits user feedback. - -## Reproduction and artifacts - -Machine: AMD Ryzen 5 5600X, 6 cores/12 logical CPUs, 32 GiB RAM, Linux 7.2.11-zen1-1-zen x86_64; Rust/Cargo 1.99.0, OpenJDK 25.0.4.1. CPU boost remained enabled, governor `powersave`; normal desktop background activity was not isolated. Builds, timed workloads and sampling runs were sequential. Release uses `opt-level=3`, thin LTO, one codegen unit; client benchmarks disable audio/controllers and need no window/GPU. Do not compare these numbers directly with earlier `fast` profile reports. - -Private/generated evidence is outside Git at `/srv/bob-data/horizon-chunk-campaign-41055168/`: `baseline-raw.json`, `baseline-summary.json`, `run_baseline.py`, individual logs, preserved executables in `baseline/`, `binary-sha256.txt`, `fixture-sha256.txt`, the `*-profile.er` sampling experiments, and `*-functions.txt` tables. The matrix binaries are preserved separately from subsequent `*-profile` binaries. The latter add a structure timing field or optional worker execution for profiling; production sources are identical. Prepared resources: `/srv/bob-data/horizon/.local/horizon-client/26.3`. Pinned jars and source artifacts: `/srv/bob-data/horizon/.local/minecraft/26.3`. - -```sh -CARGO_BUILD_JOBS=6 CARGO_TARGET_DIR=/srv/bob-data/horizon-chunk-campaign-target cargo build --locked --release -p horizon-world --example chunkgen_bench -p horizon-client --no-default-features --example chunk_bench --example streaming_bench --example save_bench --example load_bench -``` - -`chunkgen_bench` now accepts `--warmup --json --no-map`. Warmup generates/discards an offset 9×9 region at center+(64,64), with the same seeded generator and a separate worker, before creating fresh measured workers. This removes lazy ring initialization from the steady workload without generating target chunks. Resource loading, generator construction, warmup, diagnostic output and map rendering remain outside the measured generation brackets. `BENCH` JSON reports exact elapsed seconds, process CPU accounting and cumulative stage wall brackets. Worker stage brackets are not exclusive CPU shares; elapsed/count has no thread multiplier. Non-warmed runs should be measured separately when changing startup behavior. - -`load_bench RESOURCES DIMENSION_DIR --rounds 5 --limit 256 --checksum` is read-only. It selects full chunks in sorted region/coordinate order, primes region handles and filesystem cache, then separately times `SaveDir::load` (region read, decompression, NBT, resident decode and saved light), decompression/NBT of retained compressed records, and native decode of retained tags. Allocation and destruction are inside each timed loop. Setup and checksums are outside. Stage times come from separate loops and must not be summed as an exact breakdown. It emits compressed-record SHA256 provenance and an optional complete blocks/biomes/light checksum; this checksum does not cover all saved metadata. - -## Baseline observations - -The primary workload uses seed 12345, center (0,0), size 16: 256 proto chunks and 196 interior decorations. One-worker/four-worker results have five processes each; Nether/End have three each. Values are medians. - -| Workload | Proto wall | Decoration wall | Combined ms/input chunk | -| --- | ---: | ---: | ---: | -| Overworld, 1 worker | 2.224094 s | 0.440144 s | 10.407179 | -| Overworld, 4 workers | 0.732982 s | 0.131088 s | 3.375274 | -| Nether, 1 worker | 0.981417 s | 0.084589 s | 4.164085 | -| End, 1 worker | 0.213254 s | 0.015784 s | 0.894681 | - -The one-worker Overworld brackets are approximately noise 4.20, surface 2.55, biomes 0.40 and carvers 0.34 ms/input chunk. A separate sampled size-32 run reports structures 0.395 ms/input chunk; its larger site mix differs from size 16. - -Resident `chunk_bench --seed 12345 --size 4`, three processes: finish 0.4 ms/chunk (rounded display), light median 1.43 ms/column, mesh 109.5 µs/section (each process reports its best of three mesh sweeps), 29.8 KiB resident storage/chunk. Before qualifying a finish/light/mesh change, emit exact timings on both revisions; existing rounded/best-of-three output is exploratory evidence only. - -`streaming_bench --threads 4 --radius 4 --seed 12345 --fly 4 --seconds 3 --hz 120`, three processes: fill median 1.21 s, 81 chunks generated and 504 sections meshed; median reported update p99 0.041 ms. Flight generates 121 chunks, with median update p99 0.207 ms and no sampled frame with an unlit column within two chunks of the player. This short fixed-duration flight is a responsiveness/throughput check, not a complete long-travel stress test. - -Saved loads use 256 full chunks, three independent processes, five measured rounds/process; table entries are the median of process medians for the whole 256-chunk batch. - -| Corpus | Complete load | Inflate + NBT | Native decode | -| --- | ---: | ---: | ---: | -| Normal vanilla 26.3, seed 12345 | 55.740 ms | 22.068 ms | 27.211 ms | -| Flat vanilla 26.3 locator fixture | 12.733 ms | 4.505 ms | 4.884 ms | - -The normal fixture was generated by the pinned local vanilla server, `level-type=minecraft:normal`, seed 12345, forced block bounds -128,-128 through 127,127, followed by `save-all flush` and clean stop. It contains 400 full chunks; the sorted limit selects 256. Files remain at `vanilla-normal/world/dimensions/minecraft/overworld` under the artifact root. Flat source is `/srv/bob-data/horizon/.local/locator-bar-parity/generate/world/dimensions/minecraft/overworld`. Normal compressed-record SHA256: `ff9bf9e822e334e117e3d6ba5c26a88b7698456ec6d83bdbe4355ad0af1d6f55`; blocks/biomes/light: `661dd26eed78b051afeca11d8ba69222162b5da72e5d1572a12ce2a3b3a4f764`. All three processes agree, and all 256 selected chunks have saved light. Flat checksums are in the raw logs. These are warm storage measurements; cold physical I/O and a remote multiplayer chunk stream are not measured. - -`save_bench` also read the 400 normal chunks and checked written compressed records byte-for-byte: median encode 139 ms, compression 141 ms, parallel SaveDir encode/compress/write 54 ms, five rounds. This checks existing save output consistency, not vanilla read/write parity. - -## CPU samples and ranked investigations - -`perf`, Valgrind and samply are unavailable; GNU gprofng 2.47 is installed. Collection uses `gprofng collect app -p hi -o OUTPUT.er BINARY ARGS`, nominally 997 µs samples. On this machine the collector warns that the initial thread's timer is cleared (`997 -> 0`). Initial-thread samples are therefore unusable. The measured terrain worker (thread 4) records 9.204 sampled CPU seconds against 9.22 process CPU seconds in its generation bracket; decoration worker threads record 3.098 seconds against 3.126 wall seconds. Their worker-only tables are retained as `terrain-worker-functions.txt` and `decoration-worker-functions.txt`. Startup/main-thread percentages must not be inferred from these samples. Streaming worker samples independently reproduce the same generation hotspots. - -The initial loader experiment captured only 0.108 sampled seconds against 10.899 process CPU seconds and is rejected. Repeating the identical code with optional `load_bench --profile-worker --rounds 100` captures 11.323 sampled seconds against 11.347 process CPU seconds; its table is `load-worker-profile-functions.txt`. In this mixed complete-load/NBT/native-decode workload, `PalettedContainer::from_packed` is 16.12% exclusive / 16.62% inclusive, zlib `inflate_fast_help_avx2` 15.71% exclusive, saved `load_light` 5.80% exclusive / 7.38% inclusive, and NBT `Reader::payload` 4.08% exclusive / 15.06% inclusive. Native `decode_column` is 46.97% inclusive. Formatting (`format_inner`) is 6.52% inclusive, including property-map Debug formatting at 4.79%; these nested shares cannot be added. These are shares of all three benchmark loops, not just the complete-load loop. Palette reconstruction/validation is the strongest loading lead; replacing the formatted per-chunk state-cache key is a smaller plausible lead requiring the full-load 5% gate. - -For the size-32 Overworld terrain worker, ranked opportunities outside the excluded PRs are: - -1. Aquifer `compute_substance`: 13.00% exclusive / 15.83% inclusive CPU. Investigate repeated nearest-cell/grid work while retaining exact distance ties, arithmetic, random draws and fluid scheduling. -2. Material `CompiledRule::apply`: 8.52% exclusive / 14.58% inclusive. Check the already-merged surface work before changing anything; inspect fresh evidence for remaining dispatch/data access, preserving condition order and random consumption. -3. `BlockSection::set` 6.09% and `get` 2.79%; decoration additionally spends 9.95% in `get`. Investigate construction/access patterns rather than changing vanilla palette semantics. Shared storage changes need broad callers and palette-width boundary validation. -4. Beardifier `sample_unchecked`: 4.78% exclusive. Recent bulk-density work is already in baseline; only a distinct measured bottleneck should proceed. -5. Decoration sculk `spread_all`: 8.82% exclusive / 22.85% inclusive of decoration-worker CPU at this larger site. Strongly site-dependent; require a matched sculk corpus before claiming a whole-workload win. - -Open/overlapping work excluded from optimization: #1208 Perlin floor/int conversion and float gradients; #1206 Perlin volume/climate lookup; #1207 feature-region grid, ore column/visited handling, lazy biome temperature; #1201 player proximity/ticket/requeue grids; #1192 worker priority; #1191 content/assets loading. Existing merged sampler reuse, neighbor generation, carver cache, surface, packed lighting and bulk density changes are part of this baseline. These exclusions cover the semantic mechanism, not merely the file paths. - -## Candidate acceptance protocol - -For each independent candidate, preserve identical benchmark sources, resource/corpus hashes, build flags and baseline/candidate executables. Do not compare against an open PR's branch. Run no builds, parity jobs or profilers concurrently with timed qualification. Use one discarded baseline/candidate process pair, then at least five measured pairs, alternating A/B and B/A order. Keep every observation; if external contention invalidates a batch, retain it and restart the entire batch with an explanation. - -Primary generation command: `chunkgen_bench RESOURCES --seed 12345 --size 16 --threads 1 --warmup --json --no-map`, repeated at four workers. Add seed -1 at center (32,-32), Nether and End, with five pairs for any affected claim. Report proto, decoration and complete proto+decoration wall time; input and decorated chunk counts must match. Retain a non-warmed check for startup regressions and streaming fill/flight for player-visible effects. - -Primary loading command: `load_bench RESOURCES NORMAL_DIMENSION --rounds 5 --limit 256 --checksum`, repeated with the flat fixture. Each process's median complete `load_s` is one observation; internal rounds are not independent processes. Report native decode and inflate/NBT alongside complete load. Use the same command without profiler-worker mode on both revisions for qualification. - -Open a PR only after the affected complete workload improves by at least 5% in the paired repeated measurements, the improvement is stable across the measured pairs, and correctness gates pass. Report median reduction and range of paired reductions; add pairs if noise makes the 5% threshold ambiguous. A 5% isolated-stage win without a 5% complete affected-workload win does not qualify. Check related workloads for material regression. Explain corpus-specific results instead of claiming universal speedups. - -Correctness gates must include exact baseline/candidate generated blocks, quart biomes, heightmaps, postprocessing, structures, block entities and metadata for affected cases, preserving RNG and iteration behavior, plus the pinned vanilla oracle. Use existing `tools/test_chunk_generation_parity.py` (full world stage comparisons), relevant noise/density/aquifer/material/feature/palette harnesses, and `tools/test_light_parity.py` if lighting changes. Cover negative/far coordinates, several seeds, all affected dimensions and both worker counts. `tools/parity.py --repeat 3 ...` checks repeated oracle/probe agreement; reused Java oracle output is suitable for Rust-only comparisons, with a fresh relevant oracle run before final qualification. - -Loading changes additionally require existing save unit tests and comprehensive baseline/candidate semantic output checks, including heightmaps, block entities, ticks, structures and preserved unknown NBT. The benchmark's block/biome/light checksum is only one gate. Use `tools/test_save_roundtrip_parity.py` for the full vanilla → Horizon → vanilla path when relevant. Prepared Java 25/pinned jars/resources make these checks feasible here; full roundtrip requires the repository's virtual-display/runtime setup and has not been run in this profiling step. No complete world-parity pass is claimed by this report. Run `python3 tools/pr_ready.py --base origin/main` and repository-required checks before any PR. - -## Agent 2: saved palette validation - -Candidate: list palettes in `PalettedContainer::from_packed` keep the saved words directly, so avoid allocating/filling the discarded unpacked-index vector. Reuse index validation, scanning words and their occupied slots in entry order; a palette filling the bit capacity cannot contain an invalid index. Global reencoding remains unchanged. The first invalid index, exact storage-length errors, unused high padding, palette order and resident words are preserved. Pinned 26.3 `PalettedContainer.unpack` bytecode was inspected first: matching list configurations retain `SimpleBitStorage` directly; global configurations reencode. No palette-related open PR overlap was found on 2026-10-22. - -Evidence: `/srv/bob-data/horizon-chunk-campaign-41055168/palette/`, `palette-pairs.py`, `palette-pilot.log`, `palette-qualification.log`, `palette-candidate.patch`, `palette-java-bytecode.txt`, and `palette-*.log`. Binary hashes and commands are recorded in `palette/binaries.json` and `*-raw.json`. The preserved baseline `load_bench-profile` is used without worker/profiler flags because its source exactly matches the candidate benchmark source, including the optional worker branch. Identical release flags, resources and compressed records were used. No builds, tests or profilers overlapped qualification. - -Three pilot pairs showed 9.77–10.28% complete normal-load reductions after a discarded pair. Qualification used five alternating process pairs per corpus, each after a discarded pair, with five measured rounds per process. Values below are medians of process medians for each 256-chunk batch; reductions are medians/ranges of the paired reductions. - -| Corpus / workload | Baseline | Candidate | Paired reduction (range) | -| --- | ---: | ---: | ---: | -| Normal complete load | 55.459 ms | 49.770 ms | 10.257% (9.656–10.573%) | -| Normal native decode | 26.263 ms | 19.902 ms | 24.188% (22.323–25.987%) | -| Normal inflate + NBT | 22.260 ms | 22.086 ms | 0.782% (-1.174–1.680%) | -| Flat complete load | 12.663 ms | 12.027 ms | 5.023% (2.609–5.617%) | -| Flat native decode | 5.170 ms | 4.220 ms | 13.908% (5.850–28.399%) | -| Flat inflate + NBT | 4.537 ms | 4.548 ms | -0.159% (-1.852–0.563%) | - -Normal clears the complete-workload 5% gate in every measured pair. Flat shows no observed regression, but its variation does not support a stable at-least-5% claim. This is a warm saved-load improvement on these fixtures, not a generation or cold-I/O claim. - -Correctness: all timed baseline/candidate compressed-record and block/biome/light hashes match. Core palette tests pass (7 passed; 2 existing ignored). A differential reference test covers all saved widths through 13, single/list/global modes, duplicate values, full-capacity palettes, absent/wrong storage, dirty word/final padding, and first-invalid-index errors; successful cases compare strategy, bits, palette and words exactly. A fresh pinned Java palette oracle passes 266 scenarios and 861,864 edits with identical hashes. Existing `save::tests` pass (19/19), including malformed packed palettes, stored light, block-entity roundtrips, file upgrades and level-file roundtrip. - -The temporary `load_snapshot.rs` harness and true baseline/candidate binaries are archived under `palette/`; the harness was removed from the working tree. It hashes resident palette/word representations and light values, canonical complete reencoded NBT (including heightmaps/block entities), saved root NBT/block-entity sources/order/ticks, and loaded flags, names, postprocessing, entities and structure references. Every full chunk was checked: 400 normal and 289 flat, plus a synthetic nested unknown NBT key and nonempty block/fluid ticks for each corpus. All 691 per-case baseline/candidate outputs match byte-for-byte. `palette/correctness.json` records hashes; `palette/sources.json` authenticates both core sources and the harness. Exact candidate source was restored after the baseline comparison. - -Both baseline and candidate also ran `save_parity` against the pinned vanilla-generated worlds: 324 interior normal chunks have zero block, biome, heightmap or other metadata differences, with the same preexisting 7/63,700,992 recomputed light-value differences in both revisions. All checks on 225 interior flat chunks are identical to vanilla, including all 44,236,800 light values. This establishes no added discrepancy in these comparisons; it is not a clean normal-world lighting parity pass. The full server/client `test_save_roundtrip_parity.py` simulation was not run; the candidate changes only loading/validation, and the read-only full-output comparison covers unchanged save output. No generation parity claim or PR is included in this agent's result. - - -## Round 3: aquifer neighborhood pilot — rejected - -Inspected the pinned server 26.3 `Aquifer.NoiseBasedAquifer.computeSubstance` bytecode and current open PRs; no aquifer optimization overlap was found. Tested a single-current-grid cache of the 12 decoded candidate positions/indices, populated in vanilla cell order only after successful checked accesses, plus explicit four-slot nearest insertion preserving later-cell distance ties. The candidate touched only `worldgen/aquifer/noise_based.rs`; no noise evaluation or terrain traversal was changed. - -Evidence is retained in `/srv/bob-data/horizon-chunk-campaign-41055168/aquifer/`: baseline source, candidate patch, preserved candidate executable, binary hashes, build log, pilot script, all process logs, `pilot-raw.json` and `summary.json`. Pinned Java bytecode is in `aquifer-java-bytecode.txt` at the parent artifact root. The benchmark source SHA256 is `f404feb7ec526f12735d11b15af36254fd585f39b975cdbdc71905d87f10f8cc`, identical to the preserved `chunkgen_bench-profile` baseline source. Both executables use the established release flags and resources. No builds, tests or profilers ran during timing. - -One discarded process pair followed by three alternating pairs used the primary seed-12345, center-(0,0), size-16, one-worker warmed command. Every process generated 256 proto chunks and decorated 196 chunks. Complete proto+decoration paired reductions were **3.864%, 4.326%, 3.828%** (median **3.864%**); baseline/candidate complete-time medians were **2.678451 s / 2.568094 s**. This does not clear the 5% complete-workload gate. The pilot had already completed when the parent requested early cancellation. The production candidate was reverted; no qualification, four-worker/second-site matrix, parity tests, generated snapshots or PR were pursued. No correctness claim is made for the rejected candidate. This result applies to this mechanism and corpus, not an upper bound on all aquifer optimization. - -Next lead: remaining material-rule dispatch/data access (8.52% exclusive terrain-worker CPU), after checking the already-merged surface changes. Preserve rule evaluation order and random consumption; reject early if its complete-workload pilot remains below 5%. - - -## Round 4: remaining material-rule dispatch — early no-go - -Inspected merged #801 (`Specialize constant surface material-rule branches`), `docs/surface-generation-benchmark.md`, the current compiled rule/condition/density implementation, and the pinned 26.3 material resources and Java `ConditionRule`, `SequenceRule`, `OreVeinRule`, and `BlockRule` bytecode. The open-PR list was checked on 2026-10-22; no additional material-rule dispatch PR was found, and the campaign's excluded mechanisms were left untouched. Inspection evidence is retained under `/srv/bob-data/horizon-chunk-campaign-41055168/surface-feasibility/`: `pinned-material-bytecode.txt` and `inspection.json` (source commit, pinned server-game SHA256, and static expanded rule counts). - -The existing implementation already removes constant predicates and singleton/empty sequences after compiling all children, shares eager density roots, and uses unit-step material-volume indexing. The 8.52% exclusive terrain-worker sample share for `CompiledRule::apply` includes ore-vein execution and inlined work, so it cannot be treated as removable dispatch overhead. Vanilla's top-level Overworld sequence includes both ore-vein rules before the surface predicate and underground rules. Condition/block fusion or flattening nested sequences would remove only part of this work; a broad replacement evaluator would be a speculative refactor without evidence for the required complete-workload gain. The raw expanded Overworld resource tree contains 53 sequences, 149 conditions, 110 blocks, two ore-vein rules and two band rules; these static counts precede constant specialization and are not runtime frequencies. - -Decision: **early no-go**, accepted by the parent before implementation. No production source or tests were changed, no build/timing/parity jobs were run, and no candidate or PR was created. This is a feasibility rejection, not a measured performance result or an upper bound on future material optimizations. The rejected aquifer executable in the shared target directory was not used. Earlier campaign evidence and the uncommitted Round 3 report were preserved. - - -## Round 5: section access and construction — early no-go - -Inspected `BlockSection` get/set/construction, `PackedStorage` addressing, the existing ignored section microbenchmark and differential tests, and merged commits `939eabe6`, `56a251eb` and `5c55e7d8`. Checked the open-PR list on 2026-10-22; no section-storage optimization overlap was found. Pinned 26.3 `LevelChunkSection`, `PalettedContainer` and `SimpleBitStorage` bytecode was inspected before considering changes. Evidence is retained in `/srv/bob-data/horizon-chunk-campaign-41055168/section-feasibility/`: `pinned-section-bytecode.txt`, `baseline-access-assembly.txt` and `inspection.json` (source/pinned-jar/baseline hashes and the bounded open-PR catalog). - -The current implementation already reads the old packed index directly, resolves the incoming state with one multiplicative-hash map probe, reuses freed palette slots, and skips repeated storage validation after the section checks. The ignored microbenchmark compares against older pre-optimization implementations; its historical speedups are already in the campaign baseline and cannot qualify a new candidate. Uniform construction still allocates palette/count/map storage, but construction is not identified as a substantial exclusive hotspot in the retained profile. - -The preserved baseline executable confirms runtime division in packed-index addressing; pinned Java uses reciprocal multiplication in `SimpleBitStorage.cellIndex`. This is a possible future storage investigation, but the division is only part of the sampled access cost, alongside palette/word loads, incoming-state lookup and count/free-slot handling. Terrain samples assign 6.09% exclusive CPU to `set` and 2.79% to `get`, while the separate decoration sample assigns 9.95% to `get`; these site-specific shares are not a measured removable fraction or a complete-workload speedup prediction. There is no evidence here that a small addressing or construction change can remove enough complete proto-plus-decoration work to meet the 5% gate. A dense representation or bulk terrain writer would need broader semantic and workload evidence and was not attempted. - -Decision: **early no-go**, accepted by the parent before implementation. No production source or tests were changed, no build/timing/parity jobs were run, and no candidate or PR was created. No new correctness or performance result is claimed. The rejected aquifer executable in the shared target directory was not used; assembly inspection used the preserved genuine baseline. Earlier campaign evidence and uncommitted Round 3/4 reports were preserved. - - -## Round 6: saved block-state cache keys — rejected - -Inspected the loader's formatted per-chunk state-cache keys, `BlockState::from_nbt`, registry lookup/default fallback, and pinned 26.3 `StateHolder.codec`/`BlockState` bytecode. Checked the open-PR list on 2026-10-22; no saved-state-cache optimization overlap was found. The candidate derived `Hash` for `BlockState` and replaced the formatted string key with the parsed structural state. Invalid parse results returned air directly; the existing registry/default-property resolution was retained. No palette or lighting code changed. - -Evidence is retained under `/srv/bob-data/horizon-chunk-campaign-41055168/state-key/`: candidate patch and sources, preserved baseline source copies, pinned bytecode, bounded open-PR catalog, build log, paired runner, raw process observations/logs, executable hashes and summary. The baseline was the preserved **accepted palette candidate**, so these results are incremental to commit `5d82b4c9`, with none of its earlier gain counted again. Both binaries used identical `load_bench` source (SHA256 `b6f5042731d7c9d5622758043f7c1e125a3f7d098c57ee0501012474c2df7325`), release/thin-LTO/one-codegen-unit flags, resources and normal corpus. No builds, tests or profilers overlapped timing. - -One discarded process pair followed by two alternating measured pairs used `--rounds 5 --limit 256 --checksum`. Complete normal-load paired reductions were **3.257% and 2.412%** (median **2.835%**); baseline/candidate process-median medians were **49.725 / 48.316 ms** per 256 chunks. Native decode improved **5.917%** (20.266 / 19.067 ms); inflate/NBT varied by **-0.844%**. Compressed-record and block/biome/light checksums matched in every process. Those checksums are limited benchmark checks, not comprehensive semantic or vanilla parity evidence. - -Decision: **early no-go** at the incremental 5% complete-load gate. Both production files were restored exactly to their pre-candidate contents; only this report remains. No flat/long qualification, comprehensive snapshot comparison, fresh parity suite, tests, commit or PR were pursued for this rejected candidate. The shared Cargo target contains the rejected executable; future work must use the preserved accepted binary or rebuild restored sources. - - -## Round 7: aquifer qualification — retained - -The user explicitly accepted the aquifer pilot's smaller gain and later confirmed the repeated 3.3–3.5% observations were sufficient. The candidate was restored and qualified with five alternating process pairs per workload, startup/streaming/memory checks, fresh client/server aquifer/terrain/material oracles, and 36 exact pre/post-decoration snapshot comparisons. All 20 measured Overworld pairs improve; no material regression was established in the declared checks. The cache adds 312 bytes per transient aquifer object with no new allocation calls. Full results, limitations and reproduction are in [PR #1229](https://github.com/ludovic111/horizon/pull/1229). - -## Round 8: saved-light loading — retained - -Detect uniform saved nibble layers before allocation and decode remaining layers in byte pairs. Final-source complete loading improves by paired medians 6.179% normal and 10.981% flat, incremental to the accepted palette optimization. All ten measured pairs improve. All 691 full metadata snapshots and 50,331,648 fresh pinned Java light values match; the repository quick self-check passes with 1,443 tests. The separate native-decode loop measured slower despite unchanged source; its cause remains unisolated and is disclosed in [PR #1231](https://github.com/ludovic111/horizon/pull/1231). - -## Round 9: NBT payload and modified-UTF fast paths — early no-go - -Inspected the unchanged NBT reader at accepted HEAD `fb2be075` and its actual release assembly in the preserved accepted saved-light executable; captured pinned 26.3 integer-array/JDK modified-UTF bytecode for evidence. Checked the open-PR catalog on 2026-10-22; no NBT-reader optimization overlap was found. Two concrete candidates were bulk big-endian integer-array decoding after one checked slice, avoiding repeated per-element bounds/offset/push checks, and an ASCII widening path before the existing lossless modified-UTF decoder. The latter must retain raw NUL, accepted overlong sequences and unpaired UTF-16 surrogates; arrays must retain count/byte-limit/truncation errors and offsets. - -Decision: **early no-go before implementation**. The valid original mixed-loader profile attributes 4.08% exclusive to all `Reader::payload` work and 2.96% exclusive to all `Reader::string` work; NBT runs in both complete-load and inflate/NBT loops. These are not complete-load shares or strict speedup ceilings. Array decoding affects only part of payload work, while ASCII widening retains string allocation and map construction. Neither narrow candidate offers a credible incremental 5% complete `SaveDir::load` reduction; combining them would still require unusually large savings across both functions. The accepted palette and saved-light improvements are already in HEAD and are not counted again. No production edits, builds, timing runs, tests, parity jobs, candidate, commit or PR were made; no new performance/correctness result is claimed. - -Bounded evidence is outside Git at `/srv/bob-data/horizon-chunk-campaign-41055168/nbt-feasibility/`: source/accepted-binary hashes, assembly, pinned bytecode and the open-PR catalog. NBT source SHA256 remains `a32fd0ac8fe94ab1b23c000b51c3a81b28d40c518c5befb5272818792d1cf1ac`. The separate decompression lead for Round 10 is the existing profile's `inflate_fast_help_avx2` at 15.71% exclusive; no backend investigation was performed in this round. - - -## Round 10: whole-buffer zlib decompression — rejected - -Inspected the actual region decoder (`crates/horizon-world/src/compression.rs`), its flate2/zlib-rs backend, current open PRs and the official libdeflate/libdeflater API and portability documentation. No overlapping decompression PR was found on 2026-10-22. Tested libdeflater 1.26.1 as a safe whole-buffer zlib fast path, with the original decoder retained for errors and output limits. Gzip, uncompressed records, LZ4 and all encoding paths were unchanged. The initial output buffer used compressed length ×16, clamped to 16–128 KiB and the caller's output limit. All 3,285 scanned real zlib records, including proto chunks, fit that policy. - -The preserved accepted `fb2be075` saved-light executable is the baseline; source/dependency/resource/corpus hashes and release flags match the campaign protocol. After a discarded pair, the two-pair normal pilot improved complete loading by **11.908% and 11.941%**. The formatted candidate then completed five alternating process pairs per corpus after a discarded pair, with no concurrent builds/tests/profiles. Complete normal loading improved **11.079% (9.548–12.314%)**, from median 46.642 to 41.474 ms per 256 chunks; inflate/NBT improved 18.452%. Flat complete loading improved **2.568% (1.915–7.363%)**, 10.635 to 10.367 ms, supporting no 5% flat claim. All timed checksums match. Peak RSS ranges overlap (normal baseline/candidate medians 161,656/161,008 KiB; flat 160,860/160,188 KiB). The normal gain is incremental to accepted palette, aquifer and saved-light changes. - -Partial correctness gates passed for that candidate: four codec tests, including differential valid/damaged/truncated/trailing/concatenated/dictionary/limit streams and all 65,536 zlib headers; all **691 comprehensive loaded-metadata snapshots** match the accepted source byte-for-byte. A fresh pinned Minecraft 26.3 `RegionFileVersion` oracle generated 62 valid fixtures covering all four codecs, large/high-ratio data and Java zlib levels 0–9 with all three strategies. Both Rust implementations decode these exactly; Horizon encoding bytes match the original, and pinned Minecraft decodes all 62 Horizon outputs exactly. These are focused loading/codec checks, not a complete world-parity claim. - -Decision: **no-go because related valid-input workloads materially regress**. The initial bounded fast path adds a failed trial before legacy decode on high-ratio/large streams; isolated decode probes showed roughly 26–39% regressions for representative 60/64/128 KiB cases. Geometric output growth was also rejected after poor uniform-input results. A final bounded policy restricted the fast path to compressed records of 2–64 KiB, resolving those first cases, but 24 additional controlled-compressibility fixtures exposed **27–71% isolated decode regressions near 131 KiB**, about **26–27% at 256 KiB**, and about **9% on one 1 MiB case**. These microbenchmarks use the original and candidate decoder in the same release executable, a discarded pair and five alternating measured pairs; they are related-workload evidence, not complete-world timing claims. No complete-load gain is claimed for the final gated policy. Removing this tradeoff requires further design beyond this bounded attempt. - -All production and Cargo changes were restored exactly, and owned temporary examples were removed. No final repository quick-check, additional parity work, commit or PR was pursued after rejection. The shared release target contains rejected candidate artifacts and must not be used as an accepted baseline. Earlier campaign documentation, including the uncommitted Round 9 report, is preserved. Evidence remains outside Git at `/srv/bob-data/horizon-chunk-campaign-41055168/decompression/`: authenticated sources and binaries for each policy, dependencies, build/test logs, paired runners/raw observations, RSS, fresh Java fixtures/bytecode, 691 snapshot outputs, controlled-compressibility probes and `decision.json`. Agent 10 ends this optimization sequence; further work awaits user feedback. From b054c8db1156c63e5c97584d01897ef7c3730609 Mon Sep 17 00:00:00 2001 From: rdbtCVS <87152661+rdbtCVS@users.noreply.github.com> Date: Thu, 22 Oct 2026 17:15:44 +0100 Subject: [PATCH 28/29] Remove benchmark report from pull request --- docs/aquifer-cache-benchmark.md | 56 --------------------------------- 1 file changed, 56 deletions(-) delete mode 100644 docs/aquifer-cache-benchmark.md diff --git a/docs/aquifer-cache-benchmark.md b/docs/aquifer-cache-benchmark.md deleted file mode 100644 index 09ce2558b..000000000 --- a/docs/aquifer-cache-benchmark.md +++ /dev/null @@ -1,56 +0,0 @@ -# Aquifer neighborhood cache benchmark - -Caching the most recently used aquifer grid neighborhood reduces complete Overworld proto-generation plus decoration by a median **4.152% with one worker** and **2.377% with four workers** at the primary site. A second site improves **3.865% / 3.275%**. All 20 measured Overworld pairs improve. The implementation changes only `worldgen/aquifer/noise_based.rs`. - -## Implementation and scope - -Keep the twelve decoded cell positions and indices for the current grid in vanilla traversal order. Publish a replacement neighborhood only after every checked grid access succeeds; location entries initialized before an error remain visible in their original order. Reuse the neighborhood while its grid key matches. The existing location/status/surface caches retain their behavior. Explicit four-slot nearest insertion preserves later-cell distance ties (`<=`), wrapping distance arithmetic, random draw order, sampler/error order and fluid-update scheduling. Pinned 26.3 `Aquifer.NoiseBasedAquifer.computeSubstance` bytecode was inspected before implementation. - -No overlap with open aquifer work was found on 2026-10-22. The excluded Perlin, climate, feature-region, ore, biome-temperature and chunk-bookkeeping mechanisms are unchanged. Baseline `753c071c` includes the accepted saved-palette optimization; both variants contain that same loading code. These generation results measure only the aquifer change. - -## Measurements - -AMD Ryzen 5 5600X, 6 cores/12 logical CPUs, 32 GiB RAM; Linux 7.2.11-zen1-1-zen; Rust 1.99.0; release optimization, thin LTO, one codegen unit. Boost stayed enabled with the powersave governor and ordinary desktop activity. Resources: `/srv/bob-data/horizon/.local/horizon-client/26.3`. Both executables were rebuilt from accepted sources with identical benchmark source (SHA256 `f404feb7ec526f12735d11b15af36254fd585f39b975cdbdc71905d87f10f8cc`), dependencies, resources and flags. Builds, tests and profiling did not overlap qualification timing. - -Every row uses one discarded process pair, then five measured pairs alternating baseline/candidate order. All 84 processes produced 256 proto chunks and 196 interior decorations. Warmup generates/discards an offset 9×9 region before fresh measured workers. Primary seed/center: 12345/(0,0); second: -1/(32,-32). Values are process-median **milliseconds for the complete 256-input-chunk batch**, without a thread multiplier. Stage medians need not sum to the combined median. Reductions are medians/ranges of per-pair reductions, so they need not equal the ratio of the independently reported medians. - -| Workload | Proto baseline → candidate | Decoration baseline → candidate | Combined baseline → candidate | Paired combined reduction (range) | -| --- | ---: | ---: | ---: | ---: | -| Primary Overworld, 1 worker | 2224.363 → 2135.304 | 442.671 → 438.347 | 2667.810 → 2575.518 | 4.152% (3.324–5.131%) | -| Primary Overworld, 4 workers | 733.644 → 716.328 | 131.039 → 128.976 | 864.363 → 844.938 | 2.377% (0.736–2.800%) | -| Second Overworld, 1 worker | 1994.369 → 1913.935 | 233.564 → 230.780 | 2230.391 → 2143.154 | 3.865% (3.552–4.144%) | -| Second Overworld, 4 workers | 572.673 → 551.555 | 68.164 → 67.669 | 640.836 → 618.820 | 3.275% (2.561–4.469%) | -| Nether, 1 worker | 969.751 → 962.970 | 87.048 → 85.903 | 1058.483 → 1048.873 | 0.730% (-2.406–1.096%) | -| End, 1 worker | 211.710 → 211.837 | 15.689 → 16.119 | 227.650 → 228.151 | -0.175% (-1.801–1.859%) | -| Primary Overworld, non-warmed, 1 worker | 4361.328 → 4256.020 | 442.196 → 437.629 | 4804.541 → 4691.376 | 2.177% (1.743–4.552%) | - -The non-warmed row retains lazy generator initialization. Resource loading remains outside the generation brackets; whole-process duration was also recorded and its non-warmed paired median improves 1.733%. Nether/End show ordinary variation, including a 0.175% End combined slowdown (about 0.5 ms per batch); no material regression is established there. - -Five alternating streaming pairs after a discarded pair use four workers, radius 4, seed 12345, a three-second flight at speed 4 and 120 Hz. Median fill is 1.21 → 1.19 s with identical 81 generated chunks/504 meshed sections. Flight produces identical 121 chunks/1,704 sections. Fill update p99 is unchanged at 0.041 ms; flight p99 is 0.200 → 0.208 ms and maximum update is 0.28 → 0.23 ms. All twelve runs have zero nearby unlit frames. This short route checks responsiveness and output counts; it is not a long-travel throughput claim. - -## Footprint - -The twelve-entry cache field occupies 312 bytes on this platform, inline in the existing aquifer object, and adds no allocation calls or independently growing buffer. It enlarges the existing transient aquifer object, which is discarded after generation; resident and saved chunk representations are unchanged. Peak RSS medians are 732,032 → 732,088 KiB (primary one-worker) and 756,724 → 759,768 KiB (four-worker). Paired RSS ranges span increases and decreases; the four-worker median difference is about 0.4% of process RSS. The release executable text grows by 3,872 bytes (about 0.075%). - -## Correctness - -- Fresh authenticated vanilla 26.3 aquifer oracle: **379 fixtures, 389,804 exact lines per client/server JAR**. Covers modern/legacy factories, repeated/reversed queries, negative/wrapped coordinates, nonfinite inputs, sampler/picker traces, fluid scheduling, location/status/surface caches, bounds failures and all 18 injected sampler failure/retry cases. -- Fresh terrain-fill oracle: **114 fixtures, 200 fills, 7,950,873 exact lines per JAR**, including 61 noise-aquifer fixtures, 24 structure-density fixtures, repeated/prefilled/clipped fills, raw blocks, WG heightmaps and ordered fluid postprocessing. -- Fresh material-stage oracle: **79 fixtures, 106 surface builds, 42 generated-carving passes, 6,733,382 exact lines per JAR**, including real biome neighborhoods, aquifer traces, carving masks, top-material queries, complete sections, heightmaps and postprocessing. -- **36/36 exact baseline/candidate phase snapshots match**: three dimensions × three seeds/sites × one/four workers × before/after decoration. Each region retains 81 chunks and decorates 49 interior chunks, totaling 1,458 retained chunks per phase and 882 decorations. Seeds/sites are 12345/(0,0), -1/(-32,32), and 81985529216486895/(1874990,-1874990). Snapshots include resident palettes/words and public block reads, quart biomes, all six heightmaps, ordered postprocessing, structure summaries, binary-NBT entities/block entities, complete scheduled ticks, stage/decorated/retrogen metadata and actual non-air counts. Temporary diagnostic source was removed. A final production-only rebuild with the original package feature graph is byte-identical to the measured candidate executable (SHA256 `3b4e07bd9189270c7e0a4521bd25e156f932014d976b1c4208cde4769d5fd8ea`). - -These checks establish unchanged output for the declared cases. Complete world/gameplay parity and cold physical I/O are outside this comparison. - -## Reproduction and evidence - -Private/generated artifacts are outside Git at `/srv/bob-data/horizon-chunk-campaign-41055168/aquifer-qualification/`: preserved binaries/sources and hashes, build logs, `pairs.py`, `matrix-raw.json`, `matrix-summary.json`, every process log, streaming runner/raw/summary, snapshot source/runner/outputs, and fresh oracle reports/inputs/outputs. Oracle runners retain the repository's corpus/comparison/authentication logic, with isolated output storage and absolute hash-map keys; the worldgen runner selects only `noise_aquifer_corpus`. - -```sh -CARGO_BUILD_JOBS=6 CARGO_TARGET_DIR=/srv/bob-data/horizon-chunk-campaign-target cargo build --locked --release -p horizon-world --example chunkgen_bench -p horizon-client --no-default-features --example streaming_bench -python3 /srv/bob-data/horizon-chunk-campaign-41055168/aquifer-qualification/pairs.py -python3 /srv/bob-data/horizon-chunk-campaign-41055168/aquifer-qualification/streaming.py -``` - -The standard warmed generation command is `chunkgen_bench RESOURCES --seed 12345 --size 16 --threads 1 --warmup --json --no-map`; use four workers for the parallel row and `--seed -1 --center 32 -32` for the second site. - -Repository quick self-check passes: `CARGO_BUILD_JOBS=6 CARGO_TARGET_DIR=/srv/bob-data/horizon-chunk-campaign-check-target python3 tools/pr_ready.py --quick --base origin/main` (4 min 23 s). Public-tree validation, workspace rustfmt, clippy with warnings denied, **1,442 client/core/world tests** and the doc-test step pass. The quick check lists affected parity harnesses; the three fresh relevant oracle suites above were executed separately. Full log: `aquifer-qualification/pr-ready.txt`. From c0fd8dabfd96389d23cfd0d2874ae55ef6f4de5f Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Thu, 22 Oct 2026 19:15:44 +0200 Subject: [PATCH 29/29] Fix #1222: single player runs in the frame again until server players have vehicles Since #1114 single player's window is a player of its own server thread, and a server's players can't use boats or minecarts: the client sends Attack and Interact only for mobs (remote_start_attack, remote_use_entity), the server aims a player's attack or use only at mobs and dragon parts (look_at_entity), and neither side handles riding (no SetPassengers on the client, no MoveVehicle or PaddleBoat packets). So a boat on water could not be boarded or broken in single player. The server thread is opt-in again (--server-thread or HORIZON_SERVER_THREAD=1); --no-server-thread still works. lan_e2e boats: the host right-clicks a boat on water and rides it, sneaks out, and breaks it. Without --server-thread the host now runs the game's default instead of forcing the frame, so the check covers what players get. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game.rs | 11 ++-- crates/horizon-client/src/game/integrated.rs | 7 ++- tools/lan_e2e.py | 61 ++++++++++++++++++-- 3 files changed, 67 insertions(+), 12 deletions(-) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index f706e9348..6e978a6cb 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -272,9 +272,10 @@ struct Options { benchmark: Option, /// `--dev-mod PATH` (repeatable): mods always on and reloaded when rebuilt. dev_mods: Vec, - /// Single player runs on an integrated server thread (game/integrated.rs), - /// as vanilla's does: the default. `--no-server-thread` or - /// `HORIZON_SERVER_THREAD=0` runs the world in the frame instead. + /// `--server-thread` or `HORIZON_SERVER_THREAD=1`: single player runs on an + /// integrated server thread (game/integrated.rs), as vanilla's does. Off by + /// default again until a server's players can board, ride and hit + /// vehicles (#1222); `--no-server-thread` keeps it off. server_thread: bool, /// Set on the integrated server's own options: it draws nothing, so it skips meshing. integrated: bool, @@ -331,7 +332,7 @@ impl Options { dev_mods: Vec::new(), // The command harness (HORIZON_COMMAND_SCRIPT) drives the world // itself, without the event loop: it stays in the frame. - server_thread: std::env::var("HORIZON_SERVER_THREAD").map_or(true, |v| v != "0") + server_thread: std::env::var("HORIZON_SERVER_THREAD").is_ok_and(|v| v == "1") && std::env::var_os("HORIZON_COMMAND_SCRIPT").is_none(), integrated: false, }; @@ -341,7 +342,7 @@ impl Options { match arg.as_str() { "--help" | "-h" => { println!( - "Horizon — Minecraft Java Edition {} in Rust\n\n --resources PATH Prepared resources (horizon-prepare)\n --render-distance N Chunks, 2..64 (default 12)\n --world NAME World folder under saves/ (default \"New World\")\n --seed N World seed for new worlds (default 0)\n --creative Creative mode (default survival)\n --test-terrain Simple test terrain instead of vanilla generation\n --frames N Exit after N frames (testing)\n --world-frames N Exit after N frames drawn in the world (testing)\n --smoke-report PATH Write a JSON report on exit\n --benchmark SCENARIO Run a benchmark (standard, village, forest, ocean, mountain), write its report to benchmarks/ and quit\n --control Accept horizon-ctl commands on a local socket\n --control-socket PATH Control socket path (default $XDG_RUNTIME_DIR/horizon-control.sock)\n --server HOST[:PORT] Join a multiplayer server (offline mode) at start\n --name NAME Player name for multiplayer (default: generated once, kept in horizon-player.txt)\n --session-file PATH Signed-in account from the launcher (read once, then deleted)\n --dev-mod PATH A Horizon mod (.hmod or folder) to develop: always on, reloaded when rebuilt\n --no-server-thread Run single player's world in the frame, not on its server thread\n\nControls: mouse look, WASD move, Space jump, Left Shift sneak, Left Ctrl sprint,\nleft click break, right click place/use, 1-9/scroll hotbar, E inventory,\nQ drop, F3 debug, Esc pause.", + "Horizon — Minecraft Java Edition {} in Rust\n\n --resources PATH Prepared resources (horizon-prepare)\n --render-distance N Chunks, 2..64 (default 12)\n --world NAME World folder under saves/ (default \"New World\")\n --seed N World seed for new worlds (default 0)\n --creative Creative mode (default survival)\n --test-terrain Simple test terrain instead of vanilla generation\n --frames N Exit after N frames (testing)\n --world-frames N Exit after N frames drawn in the world (testing)\n --smoke-report PATH Write a JSON report on exit\n --benchmark SCENARIO Run a benchmark (standard, village, forest, ocean, mountain), write its report to benchmarks/ and quit\n --control Accept horizon-ctl commands on a local socket\n --control-socket PATH Control socket path (default $XDG_RUNTIME_DIR/horizon-control.sock)\n --server HOST[:PORT] Join a multiplayer server (offline mode) at start\n --name NAME Player name for multiplayer (default: generated once, kept in horizon-player.txt)\n --session-file PATH Signed-in account from the launcher (read once, then deleted)\n --dev-mod PATH A Horizon mod (.hmod or folder) to develop: always on, reloaded when rebuilt\n --server-thread Run single player's world on its server thread (no vehicles yet)\n\nControls: mouse look, WASD move, Space jump, Left Shift sneak, Left Ctrl sprint,\nleft click break, right click place/use, 1-9/scroll hotbar, E inventory,\nQ drop, F3 debug, Esc pause.", horizon_core::VANILLA_VERSION ); return Ok(None); diff --git a/crates/horizon-client/src/game/integrated.rs b/crates/horizon-client/src/game/integrated.rs index ef5775174..1bd3c7696 100644 --- a/crates/horizon-client/src/game/integrated.rs +++ b/crates/horizon-client/src/game/integrated.rs @@ -6,9 +6,10 @@ //! the world's options) with [`Integrated::call`], as vanilla's client calls //! into its `IntegratedServer`. //! -//! On by default. `--no-server-thread` or `HORIZON_SERVER_THREAD=0` runs -//! the world in the frame instead (the escape hatch while the network -//! player's side catches up with the in-frame one). +//! Opt-in again (`--server-thread` or `HORIZON_SERVER_THREAD=1`): it was the +//! default for one day, but a server's players can't board, ride or hit +//! boats and minecarts yet (#1222), and single player's window is one of +//! them here. use super::{ Game, Options, PackResources, Resources, UiResources, lan::{LanSettings, Owner}, diff --git a/tools/lan_e2e.py b/tools/lan_e2e.py index 9b883b163..6c4cffe23 100755 --- a/tools/lan_e2e.py +++ b/tools/lan_e2e.py @@ -114,6 +114,8 @@ address. The script then checks, from both sides, what a guest's game used to ge server's game time after the guest's game stalls for a few seconds (#848). - growth: far from the host, the chunks around the guest tick (#890): water placed there flows and wheat on farmland grows (random ticks), however the host runs its world. +- boats: the host (single player, on its server thread with --server-thread) right-clicks a + boat on water and rides it, sneaks out, and breaks it with its fists (#1222). - flight: a player in creative double-taps jump and flies, stays up, and stops flying in survival: the guest, and on the server thread the host's window too, take what may fly from the server's abilities (ClientPacketListener.handlePlayerAbilities). @@ -155,7 +157,7 @@ ROOT = Path(__file__).resolve().parents[1] GUEST_NAME = 'Guest' # The host plays single player on its server thread (--server-thread). SERVER_THREAD = False -CHECKS = ['tab', 'self', 'armour', 'nametags', 'walls', 'chest', 'crafting', 'sign', 'damage', 'mobs', 'hazards', 'pvp', 'arrows', 'kills', 'hurt', 'respawn', 'containers', 'feedback', 'titles', 'chat', 'commands', 'recipes', 'machines', 'brewing', 'loom', 'stonecutter', 'grindstone', 'anvil', 'smithing', 'enchanting', 'beacon', 'crafter', 'lectern', 'cartography', 'trials', 'clicks', 'sleep', 'predict', 'push', 'eat', 'eating', 'xp', 'objects', 'projectiles', 'worn', 'blast', 'barter', 'deflect', 'fireball', 'light', 'time', 'swings', 'cracks', 'plates', 'effects', 'portals', 'distance', 'growth', 'flight'] +CHECKS = ['tab', 'self', 'armour', 'nametags', 'walls', 'chest', 'crafting', 'sign', 'damage', 'mobs', 'hazards', 'pvp', 'arrows', 'kills', 'hurt', 'respawn', 'containers', 'feedback', 'titles', 'chat', 'commands', 'recipes', 'machines', 'brewing', 'loom', 'stonecutter', 'grindstone', 'anvil', 'smithing', 'enchanting', 'beacon', 'crafter', 'lectern', 'cartography', 'trials', 'clicks', 'sleep', 'predict', 'push', 'eat', 'eating', 'xp', 'objects', 'projectiles', 'worn', 'blast', 'barter', 'deflect', 'fireball', 'light', 'time', 'swings', 'cracks', 'plates', 'effects', 'portals', 'distance', 'growth', 'flight', 'boats'] class Game: @@ -2279,6 +2281,52 @@ def check_flight(host, guest, out): return seen + +def check_boats(host, guest, out): + """#1222: the single player boards a boat on water with a right click, sneaks out, and breaks it.""" + x, y, z = (int(v // 1) for v in pos_of(host)) + bx = x + 3 + host('command', command='gamemode survival') + host('command', command=f'fill {bx - 1} {y} {z - 1} {bx + 1} {y + 3} {z + 1} minecraft:air') + host('command', command=f'fill {bx - 1} {y - 1} {z - 1} {bx + 1} {y - 1} {z + 1} minecraft:water') + host('command', command=f'summon minecraft:oak_boat {bx + 0.5} {y - 0.1} {z + 0.5}') + host.wait(30) + boats = [o for o in host('status')['objects'] if o['kind'].endswith('oak_boat') + and abs(o['position'][0] - (bx + 0.5)) < 2 and abs(o['position'][2] - (z + 0.5)) < 2] + try: + assert boats, 'the host sees no boat on the water' + boat = boats[0] + bp = boat['position'] + host.raw('look-at', f'{bp[0]:.2f}', f'{bp[1] + 0.3:.2f}', f'{bp[2]:.2f}') + host.raw('mouse', 'right', 'click') + host.wait(10) + riding = host('status').get('riding') + assert riding == boat['id'], f'right-clicking the boat {boat["id"]}: riding {riding}' + host.raw('key', 'shift', 'down') + host.wait(10) + host.raw('key', 'shift', 'up') + host.wait(10) + riding = host('status').get('riding') + assert riding is None, f'sneaking out of the boat: riding {riding}' + host('command', command=f'tp @s {x + 0.5} {y} {z + 0.5}') + host.wait(5) + gone = False + for _ in range(10): + here = [o for o in host('status')['objects'] if o['id'] == boat['id']] + if not here: + gone = True + break + bp = here[0]['position'] + host.raw('look-at', f'{bp[0]:.2f}', f'{bp[1] + 0.3:.2f}', f'{bp[2]:.2f}') + host.raw('mouse', 'left', 'click') + # VehicleEntity.tick takes 1 off the damage every tick: hit fast. + host.wait(2) + assert gone, 'ten hits left the boat in place' + return {'boat': boat['id'], 'rode': True, 'broken': True} + finally: + host('command', command=f'kill @e[type=minecraft:oak_boat,x={bx},y={y},z={z},distance=..6]') + host('command', command=f'fill {bx - 1} {y - 1} {z - 1} {bx + 1} {y - 1} {z + 1} minecraft:stone') + def check_growth(host, guest, out): """Block ticks near a guest 1000 blocks from the host: only the guest's tickets tick them.""" x0, y0, z0 = pos_of(guest) @@ -2591,13 +2639,18 @@ CHECK_FNS = {'tab': check_tab, 'self': check_self, 'armour': check_armour, 'name 'xp': check_xp, 'objects': check_objects, 'projectiles': check_projectiles, 'blast': check_blast, 'light': check_light, 'plates': check_plates, 'swings': check_swings, 'cracks': check_cracks, - 'time': check_time, 'growth': check_growth, 'flight': check_flight, 'fireball': check_fireball, 'deflect': check_deflect, 'barter': check_barter, 'worn': check_worn, 'kills': check_kills, 'arrows': check_arrows, 'predict': check_predict} + 'time': check_time, 'growth': check_growth, 'flight': check_flight, 'fireball': check_fireball, 'deflect': check_deflect, 'barter': check_barter, 'worn': check_worn, 'kills': check_kills, 'arrows': check_arrows, 'predict': check_predict, 'boats': check_boats} def run(ctl, out, resources, checks): host, guest = Game('host', ctl, resources), Game('guest', ctl, resources) - # Either way explicitly: the game's default is the server thread. - host.d.env['HORIZON_SERVER_THREAD'] = '1' if SERVER_THREAD else '0' + # Without --server-thread the host runs the game's default, the world in the frame, as players + # get it (`boats` fails there if the default goes back to the server thread before it has + # vehicles, #1222). + if SERVER_THREAD: + host.d.env['HORIZON_SERVER_THREAD'] = '1' + else: + host.d.env.pop('HORIZON_SERVER_THREAD', None) results = {} try: host.pid = host('launch', wait=False, offscreen=sys.platform == 'darwin')['pid']