From 08f4fd5c8eafe7de5b27faf4d368dbe342d75831 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Mon, 26 Oct 2026 11:10:18 +0100 Subject: [PATCH] Teleports free their dropped columns off the frame and in batches A teleport far away (or a storage that moves by more than its size) drops every column in one frame: 3,725 at RD 32. That frame freed every column's sections, then removed each section from the renderer one at a time, with a few hash lookups per section and a free-list tree update per mesh range. - The dropped columns, and a left dimension's whole chunk map, are freed on a short-lived thread of their own ("Chunk freeing"). - Renderer::remove_columns removes whole columns: one lookup per column for the tables kept by column, and the meshes' ranges go back to the arena in one sorted merge per page (FreeList::give_all) instead of a tree update each. Per-section Renderer::remove and Occlusion::remove had no other callers and are gone. - The renderer's block-entity and detail-column tables use FxHashMap. Mac mini (M4 Pro), RD 32, explored world, server thread, 3 alternated runs, two long teleports each: the drop took 26.1-39.7 ms, now 6.9-8.5 ms. Entering the Nether: 9.7-13.4 ms, now 4.0 ms or under 6 ms. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/chunk.rs | 14 ++++ crates/horizon-client/src/game.rs | 6 +- crates/horizon-client/src/game/remote.rs | 21 +++--- crates/horizon-client/src/gfx.rs | 55 ++++++++++---- crates/horizon-client/src/gfx/arena.rs | 87 ++++++++++++++++++++++ crates/horizon-client/src/gfx/occlusion.rs | 27 +++++-- 6 files changed, 174 insertions(+), 36 deletions(-) diff --git a/crates/horizon-client/src/chunk.rs b/crates/horizon-client/src/chunk.rs index 41330df5f..c0bec476b 100644 --- a/crates/horizon-client/src/chunk.rs +++ b/crates/horizon-client/src/chunk.rs @@ -24,6 +24,20 @@ pub fn block_strategy() -> Strategy { Strategy::blocks(BLOCK_BITS.load(std::sync::atomic::Ordering::Relaxed)) } +/// Frees `columns` (`count` of them) on a short-lived thread of its own +/// when there are many: a teleport or a dimension change drops a few +/// thousand columns at once, and freeing their sections took the frame +/// 15-20 ms. A few are freed here. +pub fn free_columns(columns: T, count: usize) { + if count < 64 { + return; + } + // Should the thread not start, the closure drops `columns` here. + let _ = std::thread::Builder::new() + .name("Chunk freeing".into()) + .spawn(move || drop(columns)); +} + /// Sizes the block states' global palette for a registry of `states`. pub fn set_block_states(states: usize) { let bits = horizon_core::paletted::ceil_log2(states).max(16); diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 8591c721f..17d8586ec 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -11011,11 +11011,7 @@ impl Game<'_> { bytes += size; } } - for c in unloaded { - for sy in 0..(HEIGHT / 16) { - renderer.remove((c.x, sy, c.z)); - } - } + renderer.remove_columns(unloaded.iter().map(|c| (c.x, c.z))); if let (Some(lod), Some(player)) = (&mut self.lod, &self.player) { let lod_distance = self.settings.horizon.lod_distance_on(&renderer.info); lod.set_near_loading(self.streaming.busy()); diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index 07ab0c80d..b8153ff0a 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -11,7 +11,7 @@ use super::{Dim, Game, Mining, Screen}; use horizon_client::first_person::{Hand, SwingAnimation}; use horizon_client::{ Result, - chunk::{Chunk, SECTIONS}, + chunk::Chunk, containers::{BlockEntity, Furnace, FurnaceKind}, entities::{Experience, ExperienceOrb, ItemEntity}, hud::{HudBatch, rgba}, @@ -998,6 +998,9 @@ impl Game<'_> { self.world.day_time, self.world.weather, ); + let old = std::mem::take(&mut self.world.chunks); + let count = old.len(); + horizon_client::chunk::free_columns(old, count); self.world = World::new(self.res.facts().air, spawn.seed); self.world.game_time = time; self.world.day_time = day; @@ -1059,14 +1062,14 @@ impl Game<'_> { if gone.is_empty() { return; } + let mut freed = Vec::with_capacity(gone.len()); for &(x, z) in &gone { - self.streaming.forget(&mut self.world, x, z); - if let Some(renderer) = self.renderer.as_mut() { - for sy in 0..SECTIONS as i32 { - renderer.remove((x, sy, z)); - } - } + freed.extend(self.streaming.forget(&mut self.world, x, z)); } + if let Some(renderer) = self.renderer.as_mut() { + renderer.remove_columns(gone.iter().copied()); + } + horizon_client::chunk::free_columns(freed, gone.len()); // One pass for all of them. self.world .block_entities @@ -1631,9 +1634,7 @@ impl Game<'_> { .block_entities .retain(|k, _| (k.0 >> 4, k.2 >> 4) != (x, z)); if let Some(renderer) = self.renderer.as_mut() { - for sy in 0..SECTIONS as i32 { - renderer.remove((x, sy, z)); - } + renderer.remove_columns([(x, z)]); } } C::ChunkBatchFinished(_) => { diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index 78f73bc8b..a9ae26cf2 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -459,7 +459,7 @@ pub struct Renderer { lod_retiring: crate::fast_hash::FxHashSet, /// Finished tile meshes `upload_lods` has yet to upload. lod_pending: Option, - detail_columns: HashMap<(i32, i32), u32>, + detail_columns: crate::fast_hash::FxHashMap<(i32, i32), u32>, /// Bumped whenever a column becomes or stops being complete. detail_revision: u64, arena: arena::Arena, @@ -493,7 +493,7 @@ pub struct Renderer { /// The shadow camera's draws by page, in bands of distance from the light. shadow_runs: page_runs::PageRuns, /// Block-entity blocks per meshed section (world position, state id). - block_entities: HashMap>, + block_entities: crate::fast_hash::FxHashMap>, /// Changes whenever `block_entities` does (unique across renderers). block_entity_revision: u64, /// VisGraph face connectivity per meshed section (cave culling). @@ -2390,7 +2390,7 @@ impl Renderer { lod_cutout: Default::default(), lod_retiring: Default::default(), lod_pending: None, - detail_columns: HashMap::new(), + detail_columns: Default::default(), detail_revision: 0, arena, sorted_indices, @@ -2411,7 +2411,7 @@ impl Renderer { solid_runs: page_runs::PageRuns::default(), phase2_runs: page_runs::PageRuns::default(), shadow_runs: page_runs::PageRuns::default(), - block_entities: HashMap::new(), + block_entities: Default::default(), block_entity_revision: next_block_entity_revision(), occlusion: occlusion::Occlusion::new(crate::chunk::MIN_Y, crate::chunk::HEIGHT / 16), world_post, @@ -2847,18 +2847,47 @@ impl Renderer { self.detail_columns.insert(column, next); } } - pub fn remove(&mut self, key: SectionKey) { - self.set_detail(key, false); - if self.block_entities.remove(&key).is_some() { + /// Every section of these columns goes: one lookup per column where the + /// tables are by column, and the meshes' ranges freed in one batch. A + /// teleport removes a few thousand columns in one frame. + pub fn remove_columns(&mut self, columns: impl IntoIterator) { + let mut released = Vec::new(); + let mut block_entities = false; + for (x, z) in columns { + if self.detail_columns.remove(&(x, z)) == Some(COMPLETE_COLUMN) { + self.detail_revision += 1; + } + self.occlusion.remove_column(x, z); + let retiring = !self.lod_cutout.is_empty() && self.lod_cutout.contains(&(x, z)); + for sy in 0..crate::chunk::SECTIONS as i32 { + let key = (x, sy, z); + block_entities |= self.block_entities.remove(&key).is_some(); + if retiring { + // Distant tiles still leave its column out: drawn until they don't. + self.lod_retiring.insert(key); + } else if let Some(section) = self.sections.remove(key) { + released.push(section); + } + } + } + if block_entities { self.block_entity_revision = next_block_entity_revision(); } - self.occlusion.remove(key); - if !self.lod_cutout.is_empty() && self.lod_cutout.contains(&(key.0, key.2)) { - // Distant tiles still leave its column out: drawn until they don't. - self.lod_retiring.insert(key); - } else { - self.release_section(key); + if let Some(flat) = &mut self.flat { + for id in released.iter().filter_map(|s| s.flat_id) { + flat.remove(id); + } } + self.sorted_indices + .release_all(released.iter().filter_map(|s| { + let (page, start) = s.sorted_indices?; + Some((page, start, u64::from(s.translucent) * 6)) + })); + self.arena.release_all( + released + .iter() + .map(|s| (s.page, s.start, u64::from(s.solid + s.translucent) * 4)), + ); } /// Horizon's resolution scale (world pixels per window pixel) and FXAA. pub fn set_world_post(&mut self, scale: f32, antialias: bool) { diff --git a/crates/horizon-client/src/gfx/arena.rs b/crates/horizon-client/src/gfx/arena.rs index 34b4d332d..8f320a408 100644 --- a/crates/horizon-client/src/gfx/arena.rs +++ b/crates/horizon-client/src/gfx/arena.rs @@ -39,6 +39,35 @@ impl FreeList { } self.0.insert(start, len); } + /// `give` for many ranges: one merge of the sorted ranges into the list, + /// where giving them one by one costs a few tree lookups each. + fn give_all(&mut self, mut ranges: Vec<(u64, u64)>) { + if ranges.len() < 64 { + for (start, len) in ranges { + self.give(start, len); + } + return; + } + ranges.sort_unstable(); + let mut free = std::mem::take(&mut self.0).into_iter().peekable(); + let mut given = ranges.into_iter().peekable(); + let mut merged: Vec<(u64, u64)> = Vec::new(); + loop { + let next = match (free.peek(), given.peek()) { + (Some(a), Some(b)) if a.0 <= b.0 => free.next(), + (_, Some(_)) => given.next(), + (Some(_), None) => free.next(), + (None, None) => break, + }; + let (start, len) = next.expect("peeked"); + match merged.last_mut() { + Some(last) if last.0 + last.1 == start => last.1 += len, + _ => merged.push((start, len)), + } + } + // Sorted: the tree is built in one pass. + self.0 = merged.into_iter().collect(); + } } /// One vertex buffer of the arena; only the newest page grows. @@ -237,6 +266,26 @@ impl Arena { .free .give(start, len); } + /// `release` for many ranges (page, start, vertices) at once. + pub fn release_all(&mut self, ranges: impl IntoIterator) { + let mut by_page: Vec> = Vec::new(); + for (page, start, vertices) in ranges { + let page = page as usize; + if by_page.len() <= page { + by_page.resize_with(page + 1, Vec::new); + } + by_page[page].push((start, Self::span(vertices).min(self.page_limit))); + } + for (page, ranges) in by_page.into_iter().enumerate() { + if !ranges.is_empty() { + self.pages[page] + .as_mut() + .expect("live arena page") + .free + .give_all(ranges); + } + } + } /// Moves a page into a larger buffer; false (the page unchanged) when /// there is no memory for it. fn grow( @@ -435,6 +484,44 @@ mod tests { assert_eq!(free, FreeList::new(4 * GRAIN)); } + #[test] + fn many_ranges_given_at_once_coalesce_as_one_by_one() { + let fill = || { + let mut free = FreeList::new(1 << 30); + let taken: Vec<(u64, u64)> = (0..500u64) + .map(|i| { + let len = GRAIN * (1 + i * 7 % 5); + (free.take(len).unwrap(), len) + }) + .collect(); + for &(start, len) in taken.iter().step_by(3) { + free.give(start, len); + } + (free, taken) + }; + // Every other range of those still taken, in a scattered order. + let (mut one_by_one, taken) = fill(); + let (mut at_once, _) = fill(); + let mut given: Vec<(u64, u64)> = (0..taken.len()) + .filter(|i| i % 3 != 0 && i % 2 == 0) + .map(|i| taken[i]) + .collect(); + given.sort_by_key(|&(start, _)| start.wrapping_mul(0x9E37_79B9_7F4A_7C15)); + assert!(given.len() >= 64); + for &(start, len) in &given { + one_by_one.give(start, len); + } + at_once.give_all(given); + assert_eq!(at_once, one_by_one); + // The rest too: everything is free again, in one range. + let rest: Vec<(u64, u64)> = (0..taken.len()) + .filter(|i| i % 3 != 0 && i % 2 != 0) + .map(|i| taken[i]) + .collect(); + at_once.give_all(rest); + assert_eq!(at_once, FreeList::new(1 << 30)); + } + #[test] fn first_fit_reuses_the_lowest_hole() { let mut free = FreeList::new(8 * GRAIN); diff --git a/crates/horizon-client/src/gfx/occlusion.rs b/crates/horizon-client/src/gfx/occlusion.rs index e17ba87d1..22e2f3c81 100644 --- a/crates/horizon-client/src/gfx/occlusion.rs +++ b/crates/horizon-client/src/gfx/occlusion.rs @@ -522,16 +522,27 @@ impl Occlusion { } pub fn set(&mut self, key: SectionKey, vis: u64) { if self.columns.set(key, vis) { - self.edits.push(Edit::Set(key, vis)); - self.dirty = true; - self.changed.push(key); - if self.running.is_some() { - self.changed_late.push(key); - } + self.changed(key, vis); } } - pub fn remove(&mut self, key: SectionKey) { - self.set(key, ALL_VISIBLE); + fn changed(&mut self, key: SectionKey, vis: u64) { + self.edits.push(Edit::Set(key, vis)); + self.dirty = true; + self.changed.push(key); + if self.running.is_some() { + self.changed_late.push(key); + } + } + /// Every section of the column `x`, `z` goes (sees through every face). + pub fn remove_column(&mut self, x: i32, z: i32) { + let Some(column) = self.columns.map.remove(&(x, z)) else { + return; + }; + for (y, &vis) in column.iter().enumerate() { + if vis != ALL_VISIBLE { + self.changed((x, y as i32, z), ALL_VISIBLE); + } + } } pub fn clear(&mut self) { self.columns.map.clear();