Skip to content

Minecraft: Java Edition 26.3 rewritten from scratch in Rust, checked against vanilla with 281 differential harnesses. Get it with the Horizon launcher.

Streaming: run on the shared worker pool

Ludovic Maried5be28d945ec
diff --git a/crates/horizon-client-render/src/crash.rs b/crates/horizon-client-render/src/crash.rs
index 7a094a308..34b0a2b7e 100644
--- a/crates/horizon-client-render/src/crash.rs
+++ b/crates/horizon-client-render/src/crash.rs
@@ -518,7 +518,8 @@ pub fn panic_report(info: &std::panic::PanicHookInfo, side: &'static str) -> Rep
}
}
-/// Whether `name` is one of the streaming pool's threads, vanilla's `Util` worker threads.
+/// Whether `name` is one of the shared worker pool's threads (`horizon_threads::jobs`), vanilla's
+/// `Util` worker threads.
fn is_worker_thread(name: Option<&str>) -> bool {
name.is_some_and(|n| n.starts_with("horizon-worker-"))
}
diff --git a/crates/horizon-client-render/src/streaming.rs b/crates/horizon-client-render/src/streaming.rs
index 2a835efe7..85c8a3037 100644
--- a/crates/horizon-client-render/src/streaming.rs
+++ b/crates/horizon-client-render/src/streaming.rs
@@ -12,15 +12,12 @@ use crate::{
world::World,
};
use horizon_core::biome::BiomeZoom;
+use horizon_threads::jobs::{Class, PerWorker, Rank, Registration, Source};
use horizon_world::block_render::FaceVisibility;
use horizon_world::worldgen::decoration_order::DecorationOrder;
use std::{
collections::{BinaryHeap, HashSet, VecDeque},
- sync::{
- Arc, Condvar, Mutex, MutexGuard, RwLock, TryLockError,
- atomic::{AtomicBool, AtomicUsize, Ordering},
- },
- thread,
+ sync::{Arc, Condvar, Mutex, MutexGuard, RwLock, TryLockError},
};
/// How long a worker waits with nothing to do before it gives back its
@@ -33,7 +30,8 @@ pub type Proto = Box<dyn std::any::Any + Send>;
/// (biomes, noise, surface, carvers), decoration of a 3x3 neighbourhood
/// (FEATURES, which may write into neighbours), then conversion of a chunk
/// whose eight neighbours are decorated into a final column. Implementations
-/// must be thread-safe; each worker thread owns one `Worker`.
+/// must be thread-safe; each worker of the shared pool
+/// (`horizon_threads::jobs`) keeps one `Worker` per streamer.
pub trait ChunkGenerator: Send + Sync + 'static {
type Worker: Send + 'static;
fn worker(&self) -> Self::Worker;
@@ -276,8 +274,6 @@ impl Ord for Queued {
}
struct Shared {
queue: Mutex<BinaryHeap<Queued>>,
- ready: Condvar,
- stop: AtomicBool,
/// What meshing reads (block models and sprites), swapped when resource
/// packs reload; each mesh job takes the current one.
context: RwLock<Arc<WorkerContext>>,
@@ -285,11 +281,6 @@ struct Shared {
/// Only workers use this pool; frames never wait for its lock. Retaining
/// cleared buffers avoids racing the renderer for the process heap.
meshes: Mutex<MeshPool>,
- /// Workers that have freed their buffers ([`IDLE_RELEASE`]) and taken no
- /// job since: the last of them gives the freed memory back to the OS.
- released: AtomicUsize,
- /// The workers that started.
- workers: AtomicUsize,
}
#[derive(Default)]
struct MeshPool {
@@ -364,7 +355,11 @@ pub struct Streaming {
/// next arrive, though read from the save (`World::loaded_chunks`).
dropped_new: FxHashSet<(i32, i32)>,
non_air_sections: Arc<dyn Fn(&Proto) -> Option<SectionMask> + Send + Sync>,
- threads: Vec<thread::JoinHandle<()>>,
+ /// This streamer's place in the shared pool (`horizon_threads::jobs`);
+ /// dropping it waits for its running jobs.
+ jobs: Registration,
+ /// The workers its in-flight work is sized for.
+ workers: usize,
order: u64,
/// Ticks since the columns and protos that fell out of range were swept.
scan_age: u8,
@@ -614,6 +609,168 @@ impl GenerationScan {
}
}
}
+/// What a pool worker keeps for one streamer between its jobs: the
+/// generator's worker, and the face visibility table and its occlusion
+/// cache (like vanilla's per-thread cache: cloning it per job copied every
+/// state and face shape) and the mesher's scratch.
+struct WorkerLocal<W> {
+ worker: W,
+ faces: Option<(Arc<MeshTables>, FaceVisibility)>,
+ mesh_scratch: MeshScratch,
+}
+/// A streamer's jobs, run by the process's shared pool (`horizon_threads::jobs`).
+struct Jobs<G: ChunkGenerator> {
+ shared: Arc<Shared>,
+ generator: Arc<G>,
+ light: Arc<LightTables>,
+ locals: PerWorker<WorkerLocal<G::Worker>>,
+}
+impl<G: ChunkGenerator> Source for Jobs<G> {
+ fn next(&self) -> Option<Rank> {
+ let queue = self.shared.queue.lock().unwrap();
+ queue.peek().map(|q| {
+ // Jobs without a column (saves, meshes to free) go first.
+ let class = if q.at.is_none() {
+ Class::Urgent
+ } else {
+ Class::Near
+ };
+ Rank::new(class, q.priority)
+ })
+ }
+ fn run(&self, worker: usize) -> bool {
+ let Some(job) = self.shared.queue.lock().unwrap().pop() else {
+ return false;
+ };
+ let done = self.locals.with(
+ worker,
+ || WorkerLocal {
+ worker: self.generator.worker(),
+ faces: None,
+ mesh_scratch: MeshScratch::default(),
+ },
+ |local| self.run_job(job.job, local),
+ );
+ if let Some(done) = done {
+ self.shared.done.send(done);
+ }
+ true
+ }
+ fn idle(&self, worker: usize) {
+ // A server nobody is on, a client standing still: free the
+ // generator's buffers (and the mesher's face cache and scratch)
+ // until the next job needs them.
+ self.locals.with_existing(worker, |local| {
+ self.generator.idle(&mut local.worker);
+ local.faces = None;
+ local.mesh_scratch = MeshScratch::default();
+ });
+ }
+ fn all_idle(&self) {
+ // No worker needs retained mesh capacity now. Drop it outside the
+ // pool lock; the pool then asks the allocator to return freed pages.
+ let meshes = std::mem::take(&mut *self.shared.meshes.lock().unwrap());
+ drop(meshes);
+ }
+}
+impl<G: ChunkGenerator> Jobs<G> {
+ fn run_job(&self, job: Job, local: &mut WorkerLocal<G::Worker>) -> Option<Done> {
+ let (generator, shared, light) = (&self.generator, &self.shared, &self.light);
+ let WorkerLocal {
+ worker,
+ faces,
+ mesh_scratch,
+ } = local;
+ let done = match job {
+ Job::Generate(x, z) => match generator.load(x, z) {
+ Some(chunk) => {
+ generator.prepare_loaded(&mut *worker, &chunk);
+ Done::Generated(chunk)
+ }
+ None => {
+ let proto = generator.proto(&mut *worker, x, z);
+ let decorated = generator.is_decorated(&proto);
+ Done::Proto(x, z, proto, decorated)
+ }
+ },
+ Job::Decorate(x, z, mut region, finals) => {
+ // The final chunks' proto chunks, made here: a
+ // generated proto chunk holding their blocks.
+ for (i, snapshot) in &finals {
+ if let Some(proto) = generator.proto_beside_final(&mut *worker, snapshot) {
+ region[*i] = proto;
+ }
+ }
+ generator.decorate(&mut *worker, &mut region);
+ let mut writes = FinalWrites::default();
+ for (i, snapshot) in &finals {
+ let blocks = generator.final_writes(snapshot, &region[*i]);
+ writes
+ .block_entities
+ .extend(generator.final_block_entities(&region[*i], &blocks));
+ writes.blocks.extend(blocks);
+ region[*i] = Box::new(());
+ }
+ let finals = finals.into_iter().map(|(i, _)| i).collect();
+ Done::Decorated(x, z, region, finals, writes)
+ }
+ Job::Finish(proto) => {
+ let mut chunk = generator.finish(proto);
+ chunk.generated = true;
+ Done::Generated(chunk)
+ }
+ Job::Save(protos) => {
+ let refs: Vec<&Proto> = protos.iter().map(|(_, p)| p).collect();
+ generator.save_protos(&refs);
+ Done::Saved(protos)
+ }
+ Job::LightView(x, z, proto) => {
+ let view = generator.light_view(&proto);
+ Done::LightView(x, z, proto, view)
+ }
+ Job::Free(meshes) => {
+ for (_, mesh) in meshes {
+ let rejected = shared.meshes.lock().unwrap().put(mesh);
+ drop(rejected);
+ }
+ return None;
+ }
+ Job::Light(x, z, region, seq, sky) => {
+ Done::Lit(x, z, light_chunk(&borrow(&region), light, sky), seq)
+ }
+ 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,
+ shader_pack_lighting: settings.shader_pack_lighting,
+ zoom: settings.zoom,
+ blend_radius: settings.blend_radius,
+ smooth_lighting: settings.smooth_lighting,
+ cutout_leaves: settings.cutout_leaves,
+ translucent_layers: &settings.translucent_layers,
+ leaf_culling: settings.leaf_culling,
+ assets: &context.assets,
+ render: &context.render,
+ tables: &context.tables,
+ biomes: &context.biomes,
+ visibility,
+ };
+ let recycled = shared.meshes.lock().unwrap().take();
+ let mut mesh = mesher.mesh_reusing(&borrow(&region), sy, recycled, mesh_scratch);
+ mesh.sort_solid();
+ *faces = Some((context.tables.clone(), mesher.visibility));
+ Done::Meshed(x, sy, z, mesh, seq)
+ }
+ };
+ Some(done)
+ }
+}
pub struct WorkerContext {
pub assets: Arc<crate::assets::Assets>,
pub render: Arc<crate::assets::RenderAssets>,
@@ -629,190 +786,23 @@ impl Streaming {
) -> Self {
let shared = Arc::new(Shared {
queue: Mutex::new(BinaryHeap::new()),
- ready: Condvar::new(),
- stop: AtomicBool::new(false),
context: RwLock::new(context.clone()),
done: Mailbox::default(),
meshes: Mutex::new(MeshPool::default()),
- released: AtomicUsize::new(0),
- workers: AtomicUsize::new(0),
});
let light_tables = context.light.clone();
let saves: Arc<dyn Saves> = generator.clone();
- let threads = (0..threads.max(1))
- .map(|i| {
- let shared = shared.clone();
- let generator = generator.clone();
- let light = context.light.clone();
- thread::Builder::new()
- .name(format!("horizon-worker-{i}"))
- .spawn(move || {
- // Ticks and frames come first (horizon_threads).
- horizon_threads::behind_server();
- 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;
- let mut mesh_scratch = MeshScratch::default();
- loop {
- let job = {
- let mut queue = shared.queue.lock().unwrap();
- let mut released = false;
- loop {
- if shared.stop.load(Ordering::Relaxed) {
- return;
- }
- if let Some(job) = queue.pop() {
- if released {
- shared.released.fetch_sub(1, Ordering::Relaxed);
- }
- break job.job;
- }
- if released {
- queue = shared.ready.wait(queue).unwrap();
- continue;
- }
- let (woken, wait) =
- shared.ready.wait_timeout(queue, IDLE_RELEASE).unwrap();
- queue = woken;
- if wait.timed_out() && queue.is_empty() {
- // A server nobody is on, a client standing still:
- // free the generator's buffers (and the mesher's
- // face cache and scratch) until the next job
- // needs them.
- drop(queue);
- generator.idle(&mut worker);
- faces = None;
- mesh_scratch = MeshScratch::default();
- released = true;
- // The allocator keeps freed pages until memory
- // runs short: once every worker has freed its own,
- // hand them back.
- if shared.released.fetch_add(1, Ordering::Relaxed) + 1
- == shared.workers.load(Ordering::Relaxed)
- {
- // No worker needs retained mesh capacity now.
- // Drop it outside the pool lock before asking
- // the allocator to return freed pages.
- let meshes =
- std::mem::take(&mut *shared.meshes.lock().unwrap());
- drop(meshes);
- horizon_threads::release_free_memory();
- }
- queue = shared.queue.lock().unwrap();
- }
- }
- };
- let done = match job {
- Job::Generate(x, z) => match generator.load(x, z) {
- Some(chunk) => {
- generator.prepare_loaded(&mut worker, &chunk);
- Done::Generated(chunk)
- }
- None => {
- let proto = generator.proto(&mut worker, x, z);
- let decorated = generator.is_decorated(&proto);
- Done::Proto(x, z, proto, decorated)
- }
- },
- Job::Decorate(x, z, mut region, finals) => {
- // The final chunks' proto chunks, made here: a
- // generated proto chunk holding their blocks.
- for (i, snapshot) in &finals {
- if let Some(proto) =
- generator.proto_beside_final(&mut worker, snapshot)
- {
- region[*i] = proto;
- }
- }
- generator.decorate(&mut worker, &mut region);
- let mut writes = FinalWrites::default();
- for (i, snapshot) in &finals {
- let blocks = generator.final_writes(snapshot, &region[*i]);
- writes.block_entities.extend(
- generator.final_block_entities(&region[*i], &blocks),
- );
- writes.blocks.extend(blocks);
- region[*i] = Box::new(());
- }
- let finals = finals.into_iter().map(|(i, _)| i).collect();
- Done::Decorated(x, z, region, finals, writes)
- }
- Job::Finish(proto) => {
- let mut chunk = generator.finish(proto);
- chunk.generated = true;
- Done::Generated(chunk)
- }
- Job::Save(protos) => {
- let refs: Vec<&Proto> = protos.iter().map(|(_, p)| p).collect();
- generator.save_protos(&refs);
- Done::Saved(protos)
- }
- Job::LightView(x, z, proto) => {
- let view = generator.light_view(&proto);
- Done::LightView(x, z, proto, view)
- }
- Job::Free(meshes) => {
- for (_, mesh) in meshes {
- let rejected = shared.meshes.lock().unwrap().put(mesh);
- drop(rejected);
- }
- continue;
- }
- Job::Light(x, z, region, seq, sky) => {
- Done::Lit(x, z, light_chunk(&borrow(&region), &light, sky), seq)
- }
- 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,
- shader_pack_lighting: settings.shader_pack_lighting,
- zoom: settings.zoom,
- blend_radius: settings.blend_radius,
- smooth_lighting: settings.smooth_lighting,
- cutout_leaves: settings.cutout_leaves,
- translucent_layers: &settings.translucent_layers,
- leaf_culling: settings.leaf_culling,
- assets: &context.assets,
- render: &context.render,
- tables: &context.tables,
- biomes: &context.biomes,
- visibility,
- };
- let recycled = shared.meshes.lock().unwrap().take();
- let mut mesh = mesher.mesh_reusing(
- &borrow(&region),
- sy,
- recycled,
- &mut mesh_scratch,
- );
- mesh.sort_solid();
- faces = Some((context.tables.clone(), mesher.visibility));
- Done::Meshed(x, sy, z, mesh, seq)
- }
- };
- shared.done.send(done);
- }
- })
- })
- // A thread the system refuses (a container's process limit reached) leaves the pool a
- // worker short: a server that panicked here went down with its players' moves unsaved.
- .filter_map(|spawned| {
- spawned
- .inspect_err(|e| log::error!("Horizon: chunk worker not started: {e}"))
- .ok()
- })
- .collect::<Vec<_>>();
- shared.workers.store(threads.len(), Ordering::Relaxed);
+ let pool = horizon_threads::jobs::pool();
+ let source = Arc::new(Jobs {
+ shared: shared.clone(),
+ generator: generator.clone(),
+ light: context.light.clone(),
+ locals: horizon_threads::jobs::PerWorker::new(pool.threads()),
+ });
+ let jobs = pool.register(source);
+ // The work kept in flight is sized for the workers this streamer may
+ // use: the pool's, or fewer when asked (tests, a placeholder).
+ let workers = threads.clamp(1, pool.threads());
let non_air_sections = {
let generator = generator.clone();
Arc::new(move |p: &Proto| generator.non_air_sections(p))
@@ -820,7 +810,8 @@ impl Streaming {
Self {
shared,
non_air_sections,
- threads,
+ jobs,
+ workers,
order: 0,
scan_age: 0,
scan_area: ((i32::MIN, i32::MIN), i32::MIN),
@@ -1019,7 +1010,7 @@ impl Streaming {
order: self.order,
job,
});
- self.shared.ready.notify_one();
+ self.jobs.notify();
}
/// Rank the queued jobs again when what their priorities were computed
/// from changed: the centre, the extra areas, the band, or the loading
@@ -1538,7 +1529,7 @@ impl Streaming {
// Decorate chunks whose 3x3 proto neighbourhood is available, in
// vanilla's order: decoration is order dependent where write regions
// overlap (see `DecorationOrder`).
- let workers = self.threads.len();
+ let workers = self.workers;
if self.decorates && self.order_area != Some((self.center, self.radius)) {
let (decorated, chunks) = (&self.decorated, &world.chunks);
// The render distance is the server view distance in singleplayer.
@@ -1680,7 +1671,7 @@ impl Streaming {
for key in gone {
world.dirty_light.remove(&key);
}
- let room = (self.threads.len() * 4).saturating_sub(self.lighting.len());
+ let room = (self.workers * 4).saturating_sub(self.lighting.len());
nearest_first(&mut light, room);
for (_, x, z) in light {
let Some(region) = self.light_region(world, x, z) else {
@@ -1766,7 +1757,7 @@ impl Streaming {
// jobs went stale long before a worker reached them (#3629). What
// stays dirty is asked for again, from the current centre, as
// capacity frees; `requeue` then has a small queue to keep close.
- let room = (self.threads.len() * 32).saturating_sub(self.meshing.len());
+ let room = (self.workers * 32).saturating_sub(self.meshing.len());
nearest_first(&mut candidates, room);
for (_, x, z, sections) in candidates {
let Some(region) = Self::region(world, x, z) else {
@@ -2438,11 +2429,8 @@ impl Drop for Streaming {
None => break,
}
}
- self.shared.stop.store(true, Ordering::Relaxed);
- self.shared.ready.notify_all();
- for t in self.threads.drain(..) {
- let _ = t.join();
- }
+ // `jobs` leaves the pool after this, once its running jobs are
+ // done; the jobs still queued go with the queue.
}
}
/// The queued jobs' priorities computed again from each one's column
diff --git a/crates/horizon-client/examples/chunk_load_bench.rs b/crates/horizon-client/examples/chunk_load_bench.rs
index 9f651215b..9b1590ba0 100644
--- a/crates/horizon-client/examples/chunk_load_bench.rs
+++ b/crates/horizon-client/examples/chunk_load_bench.rs
@@ -89,8 +89,9 @@ fn main() -> Result<(), Error> {
});
let terrain = VanillaTerrain::new(&root.join("worldgen"), assets.registry, &biomes, seed)?;
- // Generation and light, on the game's worker pool. Meshing is left out
- // here and measured on its own below.
+ // Generation and light, on the game's worker pool (`threads` workers).
+ // Meshing is left out here and measured on its own below.
+ horizon_threads::jobs::configure(threads);
let mut streaming = Streaming::new(Arc::new(terrain), context.clone(), threads);
streaming.mesh = false;
streaming.center = center;
diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs
index 209d1b9b8..f111723c2 100644
--- a/crates/horizon-client/src/game.rs
+++ b/crates/horizon-client/src/game.rs
@@ -19368,6 +19368,9 @@ fn load_resources(
publish: &mut dyn FnMut(UiResources, &std::path::Path),
) -> Result<Loaded> {
let threads = settings.horizon.worker_threads();
+ // One pool of workers for every level's streaming, the client's and the
+ // server thread's (horizon_threads::jobs), sized before any world opens.
+ horizon_threads::jobs::configure(threads);
horizon_client::sprites::set_texture_budget(settings.horizon.texture_budget_mib);
horizon_client::sprites::set_labpbr_maps(shader_packs::picks_native_pack(settings));
// Stage A: the menu bundle (atlas and panorama). With a loading screen it is
@@ -20683,9 +20686,8 @@ pub fn run_with() -> Result<()> {
horizon_threads::put_helpers_behind();
if options.execution_role.is_client() {
// The render thread, like the server thread, ahead of the workers on
- // macOS and Windows. While terrain loads, the worker pools (two with
- // single player's server thread) and other programs outnumber the
- // cores, and a frame that gave its core up (in a GPU submit) waited
+ // macOS and Windows. While terrain loads, the workers
+ // (horizon_threads::jobs) and other programs outnumber the cores, and a frame that gave its core up (in a GPU submit) waited
// 10 to 200 ms to get one back.
horizon_threads::favour_current();
}
diff --git a/crates/horizon-client/tests/generation_saves.rs b/crates/horizon-client/tests/generation_saves.rs
index 13453a0e0..97d7bd942 100644
--- a/crates/horizon-client/tests/generation_saves.rs
+++ b/crates/horizon-client/tests/generation_saves.rs
@@ -156,7 +156,7 @@ impl Session {
let before = (self.streaming.generated_total, self.world.chunks.len());
self.update();
thread::sleep(Duration::from_millis(5));
- let idle = self.streaming.pending() == 0
+ let idle = !self.streaming.busy()
&& before == (self.streaming.generated_total, self.world.chunks.len());
quiet = if idle { quiet + 1 } else { 0 };
}
diff --git a/crates/horizon-client/tests/streaming_meshes.rs b/crates/horizon-client/tests/streaming_meshes.rs
index 1f4f114ad..51059f1e5 100644
--- a/crates/horizon-client/tests/streaming_meshes.rs
+++ b/crates/horizon-client/tests/streaming_meshes.rs
@@ -86,7 +86,7 @@ fn columns_loaded_past_the_render_distance_mesh_when_they_come_in_range() {
// and nothing comes back (`pending` empties).
streaming.free(uploads);
thread::sleep(Duration::from_millis(5));
- let idle = streaming.pending() == 0 && before == (world.chunks.len(), meshed.len());
+ let idle = !streaming.busy() && before == (world.chunks.len(), meshed.len());
quiet = if idle { quiet + 1 } else { 0 };
}
};
@@ -181,7 +181,7 @@ fn relit_columns_keep_every_mesh_current() {
while quiet < 40 {
assert!(Instant::now() < deadline, "streaming never settled");
let (uploads, _) = streaming.update(world);
- let idle = uploads.is_empty() && streaming.pending() == 0;
+ let idle = uploads.is_empty() && !streaming.busy();
meshes.extend(uploads);
thread::sleep(Duration::from_millis(5));
quiet = if idle { quiet + 1 } else { 0 };
diff --git a/crates/horizon-client/tests/tall_column.rs b/crates/horizon-client/tests/tall_column.rs
index 1a534b09a..b536b2d6d 100644
--- a/crates/horizon-client/tests/tall_column.rs
+++ b/crates/horizon-client/tests/tall_column.rs
@@ -90,7 +90,7 @@ fn a_tall_column_streams_lights_and_meshes_to_its_top() {
while quiet < 40 {
assert!(Instant::now() < deadline, "streaming never settled");
let (uploads, _) = streaming.update(world);
- let idle = uploads.is_empty() && streaming.pending() == 0;
+ let idle = uploads.is_empty() && !streaming.busy();
meshes.extend(uploads);
thread::sleep(Duration::from_millis(5));
quiet = if idle { quiet + 1 } else { 0 };