Merge pull request #623 from ludovic111/perf/server-chunk-encoding

Dedicated server: encode each chunk section once, not once per player
This commit is contained in:
Ludovic Marie
2026-10-17 12:43:29 +02:00
committed by GitHub
2 changed files with 392 additions and 62 deletions
+308 -62
View File
@@ -90,12 +90,78 @@ pub struct ChunkEncoder {
pub block_ids: Vec<i32>,
/// The save's tables, for the heightmaps.
pub tables: Option<Arc<crate::save::SaveTables>>,
/// Sections already looked at, by address (see `Encoded`).
cache: std::sync::Mutex<HashMap<usize, Encoded>>,
/// Each column's heightmaps, with the sections they were made from.
heights: std::sync::Mutex<HashMap<(i32, i32), Heights>>,
/// The level's sections among a column's: all of them in the Overworld,
/// y 0 to 255 in the Nether and the End (`DimensionType` min_y and height).
sections: std::ops::Range<usize>,
/// Whether the level has sky light (`DimensionType.hasSkyLight`).
sky: bool,
}
/// A column's client heightmaps, valid while it holds the same sections.
struct Heights {
sections: Vec<std::sync::Weak<Section>>,
maps: Vec<(i32, Vec<i64>)>,
}
/// What a resident section gives the wire, kept while the section is
/// unchanged. Resident sections are immutable behind `Arc` (edits go through
/// `Arc::make_mut`, which moves an edited section to a new address while a
/// `Weak` points at the old one), and the `Weak` keeps the old address from
/// being reused: the same address is the same, unchanged section.
struct Encoded {
section: std::sync::Weak<Section>,
non_air: bool,
/// LevelChunkSection.write, and whether it holds block entity blocks.
wire: Option<(Arc<[u8]>, bool)>,
/// The section's light as the wire's two DataLayers, when not uniform.
light: Option<Arc<Layers>>,
}
/// One section's light as DataLayers (two blocks per byte, the even one in
/// the low nibble), and whether each holds any light.
struct Layers {
sky: Vec<u8>,
block: Vec<u8>,
sky_any: bool,
block_any: bool,
}
impl Layers {
fn of(light: &Light) -> Self {
let (mut sky, mut block) = (vec![0u8; 2048], vec![0u8; 2048]);
let (sky_any, block_any) = match light {
Light::Uniform(v) => {
let (sk, bl) = (v >> 4, v & 15);
sky.fill(sk | sk << 4);
block.fill(bl | bl << 4);
(sk != 0, bl != 0)
}
Light::Full(a) => {
for ((pair, sky), block) in
a.as_chunks::<2>().0.iter().zip(&mut sky).zip(&mut block)
{
let [even, odd] = *pair;
*sky = even >> 4 | (odd & 0xF0);
*block = (even & 15) | odd << 4;
}
let all = a.iter().fold(0u8, |acc, &v| acc | v);
(all & 0xF0 != 0, all & 15 != 0)
}
};
Self {
sky,
block,
sky_any,
block_any,
}
}
}
/// Sections kept before the ones no longer resident are dropped.
const CACHE_SWEEP: usize = 1 << 15;
impl ChunkEncoder {
/// `biome_names`: the internal biome list in order; `biome_registry`:
/// the synchronized biome entries in network order.
@@ -148,10 +214,94 @@ impl ChunkEncoder {
biome_bits: ceil_log2(biome_registry.len()),
block_ids,
tables: None,
cache: Default::default(),
heights: Default::default(),
sections: 0..SECTIONS,
sky: true,
}
}
/// The client heightmaps (Heightmap.prime over the column), from the
/// cache while the column's sections are unchanged.
fn heightmaps(&self, chunk: &Chunk, tables: &crate::save::SaveTables) -> Vec<(i32, Vec<i64>)> {
let same = |h: &Heights| {
h.sections.len() == chunk.sections.len()
&& h.sections
.iter()
.zip(&chunk.sections)
.all(|(w, s)| std::ptr::eq(w.as_ptr(), Arc::as_ptr(s)))
};
let mut heights = self.heights.lock().unwrap();
if let Some(h) = heights.get(&(chunk.x, chunk.z))
&& same(h)
{
return h.maps.clone();
}
if heights.len() >= CACHE_SWEEP / 4 {
heights.retain(|_, h| h.sections.iter().all(|w| w.strong_count() > 0));
if heights.len() >= CACHE_SWEEP / 8 {
heights.clear();
}
}
let min_y = MIN_Y + 16 * self.sections.start as i32;
let height = 16 * self.sections.len() as i32;
let maps = crate::save::client_heightmaps_in(chunk, tables, min_y, height);
heights.insert(
(chunk.x, chunk.z),
Heights {
sections: chunk.sections.iter().map(Arc::downgrade).collect(),
maps: maps.clone(),
},
);
maps
}
/// The cached entry for a section, made with `non_air` when missing.
fn cached<T>(&self, section: &Arc<Section>, read: impl FnOnce(&mut Encoded) -> T) -> T {
let mut cache = self.cache.lock().unwrap();
if cache.len() >= CACHE_SWEEP {
cache.retain(|_, e| e.section.strong_count() > 0);
if cache.len() >= CACHE_SWEEP / 2 {
cache.clear();
}
}
let entry = 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.
fn non_air(&self, section: &Section) -> bool {
let non_air = |v: u32| self.flags.get(v as usize).copied().unwrap_or(0) & NON_AIR != 0;
// The palette first: most sections are all air, or one block.
if !section.blocks.maybe_has(non_air) {
false
} else if let Some(v) = section.blocks.uniform() {
non_air(v)
} else {
// Stops at the first block, usually near the bottom.
(0..BLOCKS).any(|i| non_air(section.blocks.get(i)))
}
}
/// A section on the wire, from the cache or encoded now.
fn section_wire(&self, section: &Arc<Section>) -> (Arc<[u8]>, bool) {
if let Some(wire) = self.cached(section, |e| e.wire.clone()) {
return wire;
}
let (encoded, entities) = self.section(section);
let mut w = horizon_protocol::buf::Writer::new();
encoded.write(&mut w);
let wire: (Arc<[u8]>, bool) = (w.data.into(), entities);
self.cached(section, |e| e.wire = Some(wire.clone()));
wire
}
/// Encode for a level of `min_y` and `height` (multiples of 16 within
/// the column), with or without sky light.
pub fn set_dimension(&mut self, min_y: i32, height: i32, sky: bool) {
@@ -161,6 +311,8 @@ impl ChunkEncoder {
let n = usize::try_from(height >> 4).unwrap_or(SECTIONS);
self.sections = lo..(lo + n).min(SECTIONS);
self.sky = sky;
// The heightmaps depend on the level's height.
self.heights.get_mut().unwrap().clear();
}
pub fn entity_type(&self, state: u16) -> Option<i32> {
self.entity_types
@@ -187,11 +339,17 @@ impl ChunkEncoder {
}
f & ENTITY != 0
};
let mut unpacked = [0u16; BLOCKS];
section.unpack(&mut unpacked);
for &v in &unpacked {
entities |= count(v, 1);
values.push(i32::from(v));
// One block everywhere (all air, mostly): what from_values would give.
let uniform = section.blocks.uniform();
if let Some(v) = uniform {
entities = count(v as u16, BLOCKS as i16);
} else {
let mut unpacked = [0u16; BLOCKS];
section.unpack(&mut unpacked);
for &v in &unpacked {
entities |= count(v, 1);
values.push(i32::from(v));
}
}
let biome_values: Vec<i32> = (0..BIOMES)
.map(|j| {
@@ -206,7 +364,14 @@ impl ChunkEncoder {
horizon_protocol::chunk::Section {
non_empty,
fluids,
states: Paletted::from_values(&values, Strategy::Blocks, 16),
states: match uniform {
Some(v) => Paletted {
bits: 0,
palette: vec![v as i32],
data: Vec::new(),
},
None => Paletted::from_values(&values, Strategy::Blocks, 16),
},
biomes: Paletted::from_values(&biome_values, Strategy::Biomes, self.biome_bits),
},
entities,
@@ -218,12 +383,7 @@ impl ChunkEncoder {
fn occupied(&self, chunk: &Chunk) -> u64 {
let mut bits = 0u64;
for (i, s) in chunk.sections[self.sections.clone()].iter().enumerate() {
let mut unpacked = [0u16; BLOCKS];
s.unpack(&mut unpacked);
let any = unpacked
.iter()
.any(|&v| self.flags.get(usize::from(v)).copied().unwrap_or(0) & NON_AIR != 0);
if any {
if self.cached(s, |e| e.non_air) {
bits |= 1 << i;
}
}
@@ -241,8 +401,8 @@ impl ChunkEncoder {
}
}
}
let mut packet = self.encode(chunk, &world.block_entities);
packet.light = self.light(chunk, around);
let mut packet = self.encode_blocks(chunk, &world.block_entities);
packet.light = self.light_data(chunk, around);
packet
}
@@ -251,7 +411,18 @@ impl ChunkEncoder {
chunk: &Chunk,
block_entities: &HashMap<(i32, i32, i32), BlockEntity>,
) -> LevelChunkWithLight {
let mut sections = Vec::with_capacity(self.sections.len());
let mut packet = self.encode_blocks(chunk, block_entities);
packet.light = self.light_data(chunk, u64::MAX);
packet
}
/// The packet without its light.
fn encode_blocks(
&self,
chunk: &Chunk,
block_entities: &HashMap<(i32, i32, i32), BlockEntity>,
) -> LevelChunkWithLight {
let mut buffer = Vec::new();
let mut entity_infos = Vec::new();
for (i, s) in chunk
.sections
@@ -260,7 +431,7 @@ impl ChunkEncoder {
.skip(self.sections.start)
.take(self.sections.len())
{
let (encoded, has_entities) = self.section(s);
let (encoded, has_entities) = self.section_wire(s);
if has_entities {
for y in 0..16 {
for z in 0..16 {
@@ -284,7 +455,7 @@ impl ChunkEncoder {
}
}
}
sections.push(encoded);
buffer.extend_from_slice(&encoded);
}
// A loaded chunk's block entities enter its maps in the save's order
// (ChunkAccess.pendingBlockEntities, then LevelChunk.blockEntities):
@@ -316,22 +487,38 @@ impl ChunkEncoder {
heightmaps: self
.tables
.as_ref()
.map(|t| {
let min_y = MIN_Y + 16 * self.sections.start as i32;
let height = 16 * self.sections.len() as i32;
crate::save::client_heightmaps_in(chunk, t, min_y, height)
})
.map(|t| self.heightmaps(chunk, t))
.unwrap_or_default(),
buffer: ChunkData::from_sections(&sections),
buffer,
block_entities: entity_infos,
},
light: self.light(chunk, u64::MAX),
light: LightData {
sky_mask: Vec::new(),
block_mask: Vec::new(),
empty_sky_mask: Vec::new(),
empty_block_mask: Vec::new(),
sky: Vec::new(),
block: Vec::new(),
},
}
}
/// The light layers of the level's sections.
fn light(&self, chunk: &Chunk, around: u64) -> LightData {
let mut data = light_data(&chunk.sections[self.sections.clone()], around);
/// ClientboundLightUpdatePacketData: the light layers of the sections
/// that exist around the column (`around`: the non-air sections of it
/// and its neighbours, as bits). A resident section's layers come from
/// the cache while it is unchanged (uniform light is quicker to fill than
/// to keep).
fn light_data(&self, chunk: &Chunk, around: u64) -> LightData {
let mut data = light_data_with(&chunk.sections[self.sections.clone()], around, |s| {
if matches!(s.light, Light::Uniform(_)) {
return Arc::new(Layers::of(&s.light));
}
self.cached(s, |e| e.light.clone()).unwrap_or_else(|| {
let layers = Arc::new(Layers::of(&s.light));
self.cached(s, |e| e.light = Some(layers.clone()));
layers
})
});
if !self.sky {
// No sky light storage at all.
data.sky_mask.clear();
@@ -412,7 +599,17 @@ pub fn entity_tag(entity: &BlockEntity) -> Option<Tag> {
/// non-air section of this column or a neighbouring one (`around`, bit i for
/// section i; LayerLightSectionStorage.updateSectionStatus). An existing
/// layer is sent, or marked empty when all dark; the others are left out.
#[cfg(test)]
fn light_data(sections: &[Arc<Section>], around: u64) -> LightData {
light_data_with(sections, around, |s| Arc::new(Layers::of(&s.light)))
}
/// The light layers, `section` giving a resident section's.
fn light_data_with(
sections: &[Arc<Section>],
around: u64,
section: impl Fn(&Arc<Section>) -> Arc<Layers>,
) -> LightData {
let n = sections.len();
let mut data = LightData {
sky_mask: Vec::new(),
@@ -425,52 +622,25 @@ fn light_data(sections: &[Arc<Section>], around: u64) -> LightData {
// Light section b holds chunk section b - 1.
let exists =
|b: usize| (b.saturating_sub(2)..=b.min(n - 1)).any(|s| s + 2 >= b && around >> s & 1 != 0);
let full_sky = Light::Uniform(0xF0);
for b in 0..n + 2 {
if !exists(b) {
continue;
}
let light = match b {
let layers = match b {
// Below the world nothing is lit; above it the sky is full.
0 => &Light::Uniform(0),
_ if b == n + 1 => &full_sky,
_ => &sections[b - 1].light,
0 => Arc::new(Layers::of(&Light::Uniform(0))),
_ if b == n + 1 => Arc::new(Layers::of(&Light::Uniform(0xF0))),
_ => section(&sections[b - 1]),
};
let (mut sky, mut block) = (vec![0u8; 2048], vec![0u8; 2048]);
let (mut sky_any, mut block_any) = (false, false);
match light {
Light::Uniform(v) => {
let (sk, bl) = (v >> 4, v & 15);
sky_any = sk != 0;
block_any = bl != 0;
let (sb, bb) = (sk | sk << 4, bl | bl << 4);
sky.fill(sb);
block.fill(bb);
}
Light::Full(a) => {
for (j, &v) in a.iter().enumerate() {
let (sk, bl) = (v >> 4, v & 15);
sky_any |= sk != 0;
block_any |= bl != 0;
if j & 1 == 0 {
sky[j >> 1] |= sk;
block[j >> 1] |= bl;
} else {
sky[j >> 1] |= sk << 4;
block[j >> 1] |= bl << 4;
}
}
}
}
if sky_any {
if layers.sky_any {
set_bit(&mut data.sky_mask, b);
data.sky.push(sky);
data.sky.push(layers.sky.clone());
} else {
set_bit(&mut data.empty_sky_mask, b);
}
if block_any {
if layers.block_any {
set_bit(&mut data.block_mask, b);
data.block.push(block);
data.block.push(layers.block.clone());
} else {
set_bit(&mut data.empty_block_mask, b);
}
@@ -499,6 +669,82 @@ mod tests {
assert_eq!(block_entity_type("minecraft:stone"), None);
}
fn encoder() -> ChunkEncoder {
let states = ["minecraft:air", "minecraft:stone", "minecraft:water"]
.map(|n| (Arc::<str>::from(n), n.ends_with("water")));
ChunkEncoder::new(states, &Builtin::default(), &[], &[])
}
/// The bytes on the wire for a section, encoded afresh.
fn fresh(encoder: &ChunkEncoder, section: &Section) -> Vec<u8> {
let mut w = horizon_protocol::buf::Writer::new();
encoder.section(section).0.write(&mut w);
w.data
}
#[test]
fn cached_sections_follow_edits() {
let encoder = encoder();
let mut chunk = Chunk::empty(0, 0);
chunk.set(1, MIN_Y + 2, 3, 1);
let before = encoder.encode(&chunk, &HashMap::new());
assert_eq!(encoder.occupied(&chunk), 1);
assert_eq!(
&*encoder.section_wire(&chunk.sections[0]).0,
fresh(&encoder, &chunk.sections[0])
);
// An edit moves the section: the cache sees a new one.
chunk.set(4, MIN_Y + 20, 4, 2);
chunk.set(1, MIN_Y + 2, 3, 0);
assert_eq!(encoder.occupied(&chunk), 2);
let after = encoder.encode(&chunk, &HashMap::new());
assert_ne!(before.chunk.buffer, after.chunk.buffer);
let mut expected = Vec::new();
for s in &chunk.sections {
expected.extend(fresh(&encoder, s));
}
assert_eq!(after.chunk.buffer, expected);
}
#[test]
fn light_layers_pack_two_blocks_per_byte() {
let mut chunk = Chunk::empty(0, 0);
let mut light = Box::new([0u8; 4096]);
for (i, v) in light.iter_mut().enumerate() {
*v = (i * 37 % 251) as u8;
}
Arc::make_mut(&mut chunk.sections[3]).light = Light::Full(light.clone());
let data = light_data(&chunk.sections, u64::MAX);
// Layer 4 holds section 3. Below the world (layer 0) there is no sky
// light, so the sky layers listed start at layer 1; section 3 has the
// only block light.
let (sky, block) = (&data.sky[3], &data.block[0]);
for j in 0..4096 {
let (s, b) = if j & 1 == 0 {
(sky[j >> 1] & 15, block[j >> 1] & 15)
} else {
(sky[j >> 1] >> 4, block[j >> 1] >> 4)
};
assert_eq!((s, b), (light[j] >> 4, light[j] & 15), "block {j}");
}
}
#[test]
fn cached_light_follows_edits() {
let encoder = encoder();
let mut chunk = Chunk::empty(0, 0);
let mut light = Box::new([0x5Au8; 4096]);
Arc::make_mut(&mut chunk.sections[2]).light = Light::Full(light.clone());
let first = encoder.encode(&chunk, &HashMap::new()).light;
assert_eq!(first, light_data(&chunk.sections, u64::MAX));
// Relighting moves the section, so the cached layers aren't reused.
light[7] = 0xF3;
Arc::make_mut(&mut chunk.sections[2]).light = Light::Full(light);
let second = encoder.encode(&chunk, &HashMap::new()).light;
assert_ne!(first, second);
assert_eq!(second, light_data(&chunk.sections, u64::MAX));
}
#[test]
fn light_masks_cover_the_world_plus_borders() {
let chunk = Chunk::empty(0, 0);
+84
View File
@@ -114,6 +114,67 @@ impl Paletted {
/// Pack values the way the server's container would have grown:
/// the smallest configuration for the number of distinct values.
pub fn from_values(values: &[i32], strategy: Strategy, global_bits: u8) -> Self {
// Palette indices by value, in first-seen order: a table over the
// value range (registry ids are small), kept per thread and cleared
// after use, with the previous value remembered, since neighbouring
// blocks repeat.
thread_local! {
static INDEX: std::cell::RefCell<Vec<u32>> = const { std::cell::RefCell::new(Vec::new()) };
}
let max = values.iter().copied().max().unwrap_or(0);
if values.iter().any(|&v| v < 0) || max >= 1 << 20 {
return Self::from_values_slow(values, strategy, global_bits);
}
let mut palette: Vec<i32> = Vec::new();
let mut ids = Vec::with_capacity(values.len());
INDEX.with_borrow_mut(|index| {
if index.len() <= max as usize {
index.resize(max as usize + 1, u32::MAX);
}
let (mut last, mut last_id) = (-1i32, 0u32);
for &v in values {
if v != last {
let slot = &mut index[v as usize];
if *slot == u32::MAX {
*slot = palette.len() as u32;
palette.push(v);
}
(last, last_id) = (v, *slot);
}
ids.push(last_id);
}
for &v in &palette {
index[v as usize] = u32::MAX;
}
});
let needed = if palette.len() <= 1 {
0
} else {
(usize::BITS - (palette.len() - 1).leading_zeros()) as u8
};
let (has_palette, bits) = strategy.configuration(needed, global_bits);
if bits == 0 {
return Self {
bits: 0,
palette: vec![palette.first().copied().unwrap_or(0)],
data: Vec::new(),
};
}
let mut data = vec![0i64; storage_len(bits, values.len())];
let per = 64 / bits as usize;
for (i, (&v, &id)) in values.iter().zip(&ids).enumerate() {
let id = if has_palette { u64::from(id) } else { v as u64 };
data[i / per] |= (id << ((i % per) * bits as usize)) as i64;
}
Self {
bits,
palette: if has_palette { palette } else { Vec::new() },
data,
}
}
/// `from_values` for any values: linear palette searches.
fn from_values_slow(values: &[i32], strategy: Strategy, global_bits: u8) -> Self {
let mut palette: Vec<i32> = Vec::new();
for &v in values {
if !palette.contains(&v) {
@@ -290,6 +351,29 @@ pub fn nibble(layer: &[u8], i: usize) -> u8 {
mod tests {
use super::*;
#[test]
fn from_values_matches_the_linear_search() {
let mut seed = 12345u64;
let mut next = |n: u64| {
seed = seed
.wrapping_mul(6364136223846793005)
.wrapping_add(1442695040888963407);
((seed >> 33) % n) as i32
};
for distinct in [1u64, 2, 16, 17, 200, 300, 5000] {
let values: Vec<i32> = (0..BLOCKS).map(|_| next(distinct) * 7).collect();
assert_eq!(
Paletted::from_values(&values, Strategy::Blocks, 16),
Paletted::from_values_slow(&values, Strategy::Blocks, 16)
);
let biomes: Vec<i32> = values[..BIOMES].to_vec();
assert_eq!(
Paletted::from_values(&biomes, Strategy::Biomes, 7),
Paletted::from_values_slow(&biomes, Strategy::Biomes, 7)
);
}
}
#[test]
fn bit_storage_matches_simple_bit_storage() {
// 5 bits: 12 values per long, the top 4 bits of each long unused.