From 4cdad5aae68cb3ace52f156d81a3a74c8922c871 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sat, 17 Oct 2026 13:00:27 +0200 Subject: [PATCH] Chunk packets: one cache lock per column for its non-air sections encode_in asks whether each of the nine surrounding columns' 24 sections holds anything, to decide which light layers exist: 216 cache lookups, each locking the cache and SipHashing a section's address. The column's sections now share one lock, and the cache uses the Fx hash. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/lan/chunks.rs | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/crates/horizon-client/src/lan/chunks.rs b/crates/horizon-client/src/lan/chunks.rs index 6b8c41e55..7d09427f7 100644 --- a/crates/horizon-client/src/lan/chunks.rs +++ b/crates/horizon-client/src/lan/chunks.rs @@ -4,6 +4,7 @@ use crate::{ chunk::{Chunk, Light, MIN_Y, SECTIONS, Section}, containers::BlockEntity, + fast_hash::FxHashMap, }; use horizon_protocol::{ buf::set_bit, @@ -91,7 +92,7 @@ pub struct ChunkEncoder { /// The save's tables, for the heightmaps. pub tables: Option>, /// Sections already looked at, by address (see `Encoded`). - cache: std::sync::Mutex>, + cache: std::sync::Mutex>, /// Each column's heightmaps, with the sections they were made from. heights: std::sync::Mutex>, /// The level's sections among a column's: all of them in the Overworld, @@ -258,22 +259,29 @@ impl ChunkEncoder { /// The cached entry for a section, made with `non_air` when missing. fn cached(&self, section: &Arc
, read: impl FnOnce(&mut Encoded) -> T) -> T { - let mut cache = self.cache.lock().unwrap(); + read(self.entry(&mut self.cache.lock().unwrap(), section)) + } + + /// A section's entry in the locked cache, made with `non_air` when missing. + fn entry<'a>( + &self, + cache: &'a mut FxHashMap, + section: &Arc
, + ) -> &'a mut Encoded { if cache.len() >= CACHE_SWEEP { cache.retain(|_, e| e.section.strong_count() > 0); if cache.len() >= CACHE_SWEEP / 2 { cache.clear(); } } - let entry = cache + cache .entry(Arc::as_ptr(section) as usize) .or_insert_with(|| Encoded { section: Arc::downgrade(section), non_air: self.non_air(section), wire: None, light: None, - }); - read(entry) + }) } /// LevelChunkSection.hasOnlyAir, negated. @@ -382,8 +390,10 @@ impl ChunkEncoder { /// The non-air sections of a column as bits (LevelChunkSection.hasOnlyAir). fn occupied(&self, chunk: &Chunk) -> u64 { let mut bits = 0u64; + // One lock for the column: encode_in asks for nine columns' sections. + let mut cache = self.cache.lock().unwrap(); for (i, s) in chunk.sections[self.sections.clone()].iter().enumerate() { - if self.cached(s, |e| e.non_air) { + if self.entry(&mut cache, s).non_air { bits |= 1 << i; } }