Merge pull request #269 from ludovic111/perf/worker-face-visibility

Load worlds ~4.7x faster: stop copying the face visibility table per mesh job
This commit is contained in:
Ludovic Marie
2026-10-15 11:26:54 +02:00
committed by GitHub
3 changed files with 67 additions and 37 deletions
@@ -670,6 +670,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::<serde_json::Map<_, _>>(),
"dirty_light": self.world.dirty_light.len(),
"renderer": renderer,
"display": self.window.as_ref().and_then(|w| w.current_monitor()).map(|m| json!({
"width": m.size().width,
+33 -2
View File
@@ -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},
@@ -342,6 +343,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<MeshTables>, FaceVisibility)> = None;
loop {
let job = {
let mut queue = shared.queue.lock().unwrap();
@@ -374,6 +379,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,
@@ -383,9 +396,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(&region), sy), seq)
let mesh = mesher.mesh(&borrow(&region), sy);
faces = Some((context.tables.clone(), mesher.visibility));
Done::Meshed(x, sy, z, mesh, seq)
}
};
if send.send(done).is_err() {
@@ -572,6 +587,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(
+32 -35
View File
@@ -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<u32>, Vec<u32>) {
let mut palette: Vec<u32> = Vec::new();
let mut small = true;
let mut lookup: std::collections::HashMap<u32, u32> = 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<Item = u32>) -> Vec<u64> {
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<u32> = Vec::new();
let mut small = true;
let mut lookup: std::collections::HashMap<u32, u32> = 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<u32> = 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<u32, u32> = 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,