From b23b7421cfd262411bbff1f21c896f72e0ef0d8f Mon Sep 17 00:00:00 2001 From: Snowiiii Date: Sun, 27 Oct 2024 12:29:53 +0100 Subject: [PATCH] Revert "CChunkData optimizations (#193)" This reverts commit a1b97730bdd5029f3076269ce6e4a297883c8262. Due to Errors ``` thread 'tokio-runtime-worker' panicked at pumpkin-protocol/src/client/play/c_chunk_data.rs:84:25: index out of bounds: the len is 4096 but the index is 25374 ``` --- Cargo.toml | 4 -- pumpkin-protocol/Cargo.toml | 1 - .../src/client/play/c_chunk_data.rs | 64 +++++++++---------- pumpkin-world/src/chunk/mod.rs | 7 +- pumpkin/src/main.rs | 20 +++--- 5 files changed, 40 insertions(+), 56 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 57455b51e..9e1f2106b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,10 +20,6 @@ edition = "2021" lto = true codegen-units = 1 -[profile.profiling] -inherits = "release" -debug = true - [workspace.dependencies] log = "0.4" tokio = { version = "1.41", features = [ diff --git a/pumpkin-protocol/Cargo.toml b/pumpkin-protocol/Cargo.toml index 871fbd302..abb54fc80 100644 --- a/pumpkin-protocol/Cargo.toml +++ b/pumpkin-protocol/Cargo.toml @@ -19,7 +19,6 @@ num-traits.workspace = true num-derive.workspace = true bytes = "1.8" -arrayvec = "0.7.6" flate2 = "1.0" diff --git a/pumpkin-protocol/src/client/play/c_chunk_data.rs b/pumpkin-protocol/src/client/play/c_chunk_data.rs index 0c3a6a6cf..a3b8098bb 100644 --- a/pumpkin-protocol/src/client/play/c_chunk_data.rs +++ b/pumpkin-protocol/src/client/play/c_chunk_data.rs @@ -1,12 +1,10 @@ +use std::collections::HashMap; + use crate::{bytebuf::ByteBuffer, BitSet, ClientPacket, VarInt}; -use arrayvec::ArrayVec; use itertools::Itertools; use pumpkin_macros::client_packet; -use pumpkin_world::{ - chunk::{ChunkData, TOTAL_SUBCHUNK}, - DIRECT_PALETTE_BITS, -}; +use pumpkin_world::{chunk::ChunkData, DIRECT_PALETTE_BITS}; use super::ClientboundPlayPackets; @@ -27,23 +25,8 @@ impl<'a> ClientPacket for CChunkData<'a> { buf.put_slice(&heightmap_nbt); let mut data_buf = ByteBuffer::empty(); - let mut palette_map = [0u16; 4096]; - let mut lighting_subchunks = ArrayVec::<[u8; 2048], TOTAL_SUBCHUNK>::new(); self.0.blocks.iter_subchunks().for_each(|chunk| { - let mut block_count = 0; - let mut chunk_light = [0u8; 2048]; - for (i, block) in chunk.iter().enumerate() { - if !block.is_air() { - block_count += 1; - continue; - } - let index = i / 2; - let mask = if i % 2 == 1 { 0xF0 } else { 0x0F }; - chunk_light[index] |= mask; - } - - lighting_subchunks.push(chunk_light); - + let block_count = chunk.iter().filter(|block| !block.is_air()).count() as i16; // Block count data_buf.put_i16(block_count); //// Block states @@ -68,28 +51,26 @@ impl<'a> ClientPacket for CChunkData<'a> { // PaletteType::Direct }; - let chunk_size = match palette_type { - PaletteType::Indirect(block_size) => 64 / block_size as usize, - PaletteType::Direct => 64 / DIRECT_PALETTE_BITS as usize, - }; - - let mut block_data_array = ArrayVec::::new(); + let mut block_data_array = Vec::new(); match palette_type { PaletteType::Indirect(block_size) => { // Bits per entry data_buf.put_u8(block_size as u8); // Palette length data_buf.put_var_int(&VarInt(palette.len() as i32)); + let mut palette_map = HashMap::new(); palette.iter().enumerate().for_each(|(i, id)| { - palette_map[id.data as usize] = i as u16; + palette_map.insert(*id, i); // Palette data_buf.put_var_int(&VarInt(id.get_id_mojang_repr())); }); - for block_clump in chunk.chunks(chunk_size) { + for block_clump in chunk.chunks(64 / block_size as usize) { let mut out_long: i64 = 0; for block in block_clump.iter().rev() { - let index = palette_map[block.data as usize]; - out_long = out_long << block_size | (index as i64); + let index = palette_map + .get(block) + .expect("Its just got added, ofc it should be there"); + out_long = out_long << block_size | (*index as i64); } block_data_array.push(out_long); } @@ -97,7 +78,7 @@ impl<'a> ClientPacket for CChunkData<'a> { PaletteType::Direct => { // Bits per entry data_buf.put_u8(DIRECT_PALETTE_BITS as u8); - for block_clump in chunk.chunks(chunk_size) { + for block_clump in chunk.chunks(64 / DIRECT_PALETTE_BITS as usize) { let mut out_long: i64 = 0; let mut shift = 0; for block in block_clump { @@ -113,9 +94,6 @@ impl<'a> ClientPacket for CChunkData<'a> { // TODO: precompute this and omit making the `block_data_array` data_buf.put_var_int(&VarInt(block_data_array.len() as i32)); // Data array - let bytes = data_buf.buf(); - bytes.reserve(block_data_array.len() * 8); - for data_int in block_data_array { data_buf.put_i64(data_int); } @@ -146,6 +124,22 @@ impl<'a> ClientPacket for CChunkData<'a> { // Empty Block Light Mask buf.put_bit_set(&BitSet(VarInt(1), &[0])); + let mut lighting_subchunks = Vec::new(); + + self.0.blocks.iter_subchunks().for_each(|chunk| { + let mut chunk_light = [0u8; 2048]; + for (i, block) in chunk.iter().enumerate() { + if !block.is_air() { + continue; + } + let index = i / 2; + let mask = if i % 2 == 1 { 0xF0 } else { 0x0F }; + chunk_light[index] |= mask; + } + + lighting_subchunks.push(chunk_light); + }); + buf.put_var_int(&lighting_subchunks.len().into()); for subchunk in lighting_subchunks { buf.put_var_int(&VarInt(subchunk.len() as i32)); diff --git a/pumpkin-world/src/chunk/mod.rs b/pumpkin-world/src/chunk/mod.rs index 135778ea0..585ce82d8 100644 --- a/pumpkin-world/src/chunk/mod.rs +++ b/pumpkin-world/src/chunk/mod.rs @@ -16,10 +16,9 @@ use crate::{ pub mod anvil; -pub const CHUNK_AREA: usize = 16 * 16; -pub const SUBCHUNK_VOLUME: usize = CHUNK_AREA * 16; -pub const CHUNK_VOLUME: usize = CHUNK_AREA * WORLD_HEIGHT; -pub const TOTAL_SUBCHUNK: usize = CHUNK_VOLUME / SUBCHUNK_VOLUME; +const CHUNK_AREA: usize = 16 * 16; +const SUBCHUNK_VOLUME: usize = CHUNK_AREA * 16; +const CHUNK_VOLUME: usize = CHUNK_AREA * WORLD_HEIGHT; pub trait ChunkReader: Sync + Send { fn read_chunk( diff --git a/pumpkin/src/main.rs b/pumpkin/src/main.rs index 4d4459b45..9823afdad 100644 --- a/pumpkin/src/main.rs +++ b/pumpkin/src/main.rs @@ -87,19 +87,13 @@ const fn convert_logger_filter(level: pumpkin_config::logging::LevelFilter) -> L } } -fn main() { - // ensure rayon is built outside of tokio scope - rayon::ThreadPoolBuilder::new().build_global().unwrap(); - tokio::runtime::Builder::new_multi_thread() - .enable_all() - .build() - .unwrap() - .block_on(async_main()) - .unwrap(); -} - -async fn async_main() -> io::Result<()> { +#[tokio::main] +async fn main() -> io::Result<()> { init_logger(); + // let rt = tokio::runtime::Builder::new_multi_thread() + // .enable_all() + // .build() + // .unwrap(); ctrlc::set_handler(|| { log::warn!( "{}", @@ -110,6 +104,8 @@ async fn async_main() -> io::Result<()> { std::process::exit(0); }) .unwrap(); + // ensure rayon is built outside of tokio scope + rayon::ThreadPoolBuilder::new().build_global().unwrap(); let default_panic = std::panic::take_hook(); std::panic::set_hook(Box::new(move |info| { default_panic(info);