From ed8f3d70b8528c1d5a5711303ba0ee1c635ec7f4 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Thu, 15 Oct 2026 04:06:42 +0200 Subject: [PATCH 1/3] Keep each worker's face visibility table across mesh jobs Every mesh job cloned the whole FaceVisibility (every state, every face VoxelShape and the occlusion cache) and dropped it afterwards: about half of the workers' busy time while a world loads. Each worker now keeps its copy, like vanilla's per-thread occlusion cache, and clones a new one only when the mesh tables change. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/streaming.rs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/crates/horizon-client/src/streaming.rs b/crates/horizon-client/src/streaming.rs index 4d9bac937..b77825b2f 100644 --- a/crates/horizon-client/src/streaming.rs +++ b/crates/horizon-client/src/streaming.rs @@ -10,6 +10,7 @@ use crate::{ world::World, }; use horizon_core::biome::BiomeZoom; +use horizon_world::block_render::FaceVisibility; use horizon_world::worldgen::decoration_order::DecorationOrder; use std::{ collections::{BinaryHeap, HashSet}, @@ -182,6 +183,10 @@ impl Streaming { .name(format!("horizon-worker-{i}")) .spawn(move || { let mut worker = generator.worker(); + // The face visibility table and its occlusion cache stay with + // the worker across mesh jobs, like vanilla's per-thread cache: + // cloning it per job copied every state and face shape. + let mut faces: Option<(Arc, FaceVisibility)> = None; loop { let job = { let mut queue = shared.queue.lock().unwrap(); @@ -210,6 +215,14 @@ impl Streaming { } Job::Mesh(x, sy, z, region, seq, settings) => { let context = shared.context.read().unwrap().clone(); + let visibility = match faces.take() { + Some((tables, visibility)) + if Arc::ptr_eq(&tables, &context.tables) => + { + visibility + } + _ => context.tables.visibility.clone(), + }; let mut mesher = Mesher { nether_shading: settings.nether, zoom: settings.zoom, @@ -219,9 +232,11 @@ impl Streaming { assets: &context.assets, tables: &context.tables, biomes: &context.biomes, - visibility: context.tables.visibility.clone(), + visibility, }; - Done::Meshed(x, sy, z, mesher.mesh(&borrow(®ion), sy), seq) + let mesh = mesher.mesh(&borrow(®ion), sy); + faces = Some((context.tables.clone(), mesher.visibility)); + Done::Meshed(x, sy, z, mesh, seq) } }; if send.send(done).is_err() { From 50a1649dea147ee3af1e2ee5cb6cf42635937fee Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Thu, 15 Oct 2026 04:06:42 +0200 Subject: [PATCH 2/3] Pack small palettes without hashing PalettedContainer::pack hashed all 4096 values with SipHash to build the palette. It now shares from_values' scan, which only switches to a map past 16 distinct values. Same palette order, same output. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-core/src/paletted.rs | 67 ++++++++++++++--------------- 1 file changed, 32 insertions(+), 35 deletions(-) diff --git a/crates/horizon-core/src/paletted.rs b/crates/horizon-core/src/paletted.rs index 9344d9392..f162404f5 100644 --- a/crates/horizon-core/src/paletted.rs +++ b/crates/horizon-core/src/paletted.rs @@ -151,6 +151,36 @@ fn write(words: &mut [u64], bits: u8, i: usize, value: u32) { let (word, shift) = locate(bits, i); words[word] = (words[word] & !(mask(bits) << shift)) | (u64::from(value) << shift); } +/// The distinct `values` in first-occurrence order and each value's index in +/// them. Up to 16 distinct values a scan finds them faster than hashing. +fn palette_ids(values: &[u32]) -> (Vec, Vec) { + let mut palette: Vec = Vec::new(); + let mut small = true; + let mut lookup: std::collections::HashMap = std::collections::HashMap::new(); + let mut ids = Vec::with_capacity(values.len()); + for &value in values { + let id = if small { + match palette.iter().position(|&v| v == value) { + Some(id) => id as u32, + None => { + palette.push(value); + if palette.len() > 16 { + small = false; + lookup.extend(palette.iter().enumerate().map(|(i, &v)| (v, i as u32))); + } + palette.len() as u32 - 1 + } + } + } else { + *lookup.entry(value).or_insert_with(|| { + palette.push(value); + palette.len() as u32 - 1 + }) + }; + ids.push(id); + } + (palette, ids) +} /// Words holding `ids` in `bits`-wide slots. fn pack_words(bits: u8, entries: usize, ids: impl Iterator) -> Vec { let mut words = vec![0u64; storage_len(bits, entries)]; @@ -403,31 +433,7 @@ impl PalettedContainer { /// container unpacked from disk. pub fn from_values(strategy: Strategy, values: &[u32]) -> Self { assert_eq!(values.len(), strategy.entries()); - let mut palette: Vec = Vec::new(); - let mut small = true; - let mut lookup: std::collections::HashMap = std::collections::HashMap::new(); - let mut ids = Vec::with_capacity(values.len()); - for &value in values { - let id = if small { - match palette.iter().position(|&v| v == value) { - Some(id) => id as u32, - None => { - palette.push(value); - if palette.len() > 16 { - small = false; - lookup.extend(palette.iter().enumerate().map(|(i, &v)| (v, i as u32))); - } - palette.len() as u32 - 1 - } - } - } else { - *lookup.entry(value).or_insert_with(|| { - palette.push(value); - palette.len() as u32 - 1 - }) - }; - ids.push(id); - } + let (palette, ids) = palette_ids(values); Self::build(strategy, palette, &ids) } /// A container over a distinct-value `palette` and an id per entry. @@ -530,18 +536,9 @@ impl PalettedContainer { /// re-encoded for the saved bit width. pub fn pack(&self) -> Packed { let entries = self.strategy.entries(); - let mut palette: Vec = Vec::new(); - let mut ids = Vec::with_capacity(entries); let mut values = vec![0u32; entries]; self.unpack(&mut values); - let mut lookup: std::collections::HashMap = std::collections::HashMap::new(); - for &value in &values { - let id = *lookup.entry(value).or_insert_with(|| { - palette.push(value); - palette.len() as u32 - 1 - }); - ids.push(id); - } + let (palette, ids) = palette_ids(&values); let bits = match self.strategy.config(ceil_log2(palette.len())) { Config::Single => 0, Config::List(bits) => bits, From 213af61b92ba23ca7bfe1e947a3231332330981c Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Thu, 15 Oct 2026 04:55:14 +0200 Subject: [PATCH 3/3] status: report each streaming stage's work `status` gains `streaming` (queued jobs, chunks generating, protos waiting for decoration, decorating, finishing, lighting, meshing) and `dirty_light`, so a slow load shows which stage it waits on. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game/control.rs | 2 ++ crates/horizon-client/src/streaming.rs | 16 ++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/crates/horizon-client/src/game/control.rs b/crates/horizon-client/src/game/control.rs index 61c0cce0a..d1ec2cdb8 100644 --- a/crates/horizon-client/src/game/control.rs +++ b/crates/horizon-client/src/game/control.rs @@ -647,6 +647,8 @@ impl Game<'_> { "pending_chunks": self.streaming.pending() + self.pending_uploads.len(), "generated_chunks": self.streaming.generated_total, "meshed_sections": self.streaming.meshed_total, + "streaming": self.streaming.stages().into_iter().map(|(k, v)| (k.to_string(), v.into())).collect::>(), + "dirty_light": self.world.dirty_light.len(), "renderer": renderer, "uptime_seconds": self.started.elapsed().as_secs_f64(), "server": self.remote_status(), diff --git a/crates/horizon-client/src/streaming.rs b/crates/horizon-client/src/streaming.rs index b77825b2f..346c9fc7b 100644 --- a/crates/horizon-client/src/streaming.rs +++ b/crates/horizon-client/src/streaming.rs @@ -360,6 +360,22 @@ impl Streaming { pub fn pending(&self) -> usize { self.shared.queue.lock().unwrap().len() } + /// How much work each stage holds: queued jobs, then what is in flight or + /// waiting at each step from generation to meshing. + pub fn stages(&self) -> [(&'static str, usize); 7] { + [ + ("queued", self.pending()), + ("generating", self.generating.len()), + ( + "protos", + self.protos.values().filter(|p| p.is_some()).count(), + ), + ("decorating", self.decorating), + ("finishing", self.finishing.len()), + ("lighting", self.lighting.len()), + ("meshing", self.meshing.len()), + ] + } /// Advance streaming: collect finished work, then schedule more. /// Returns meshes ready for upload and columns that were unloaded. pub fn update(