From 1179f208ef91793107e1b60e73f3c78a59f689e0 Mon Sep 17 00:00:00 2001 From: Alvsch <94403567+Alvsch@users.noreply.github.com> Date: Wed, 30 Oct 2024 02:28:54 +0100 Subject: [PATCH 1/2] small CChunkData write improvement --- Cargo.toml | 4 +++ .../src/client/play/c_chunk_data.rs | 30 ++++++++++--------- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index fa2adc6c..427f6edd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,6 +20,10 @@ 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/src/client/play/c_chunk_data.rs b/pumpkin-protocol/src/client/play/c_chunk_data.rs index 36d4c57f..07849d42 100644 --- a/pumpkin-protocol/src/client/play/c_chunk_data.rs +++ b/pumpkin-protocol/src/client/play/c_chunk_data.rs @@ -1,5 +1,3 @@ -use std::collections::HashMap; - use crate::{bytebuf::ByteBuffer, BitSet, ClientPacket, VarInt}; use itertools::Itertools; @@ -55,23 +53,26 @@ impl<'a> ClientPacket for CChunkData<'a> { 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.insert(*id, i); + + let mut palette_ids = Vec::with_capacity(palette.len()); + palette.iter().for_each(|id| { + palette_ids.push(*id); // Palette data_buf.put_var_int(&VarInt(id.get_id_mojang_repr())); }); // Data array length - data_buf.put_var_int(&VarInt( - chunk.len().div_ceil(64 / block_size as usize) as i32 - )); + let data_array_len = chunk.len().div_ceil(64 / block_size as usize); + data_buf.put_var_int(&VarInt(data_array_len as i32)); + + data_buf.reserve(data_array_len * 8); 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 - .get(block) + let index = palette_ids + .iter() + .position(|b| *b == block) .expect("Its just got added, ofc it should be there"); - out_long = out_long << block_size | (*index as i64); + out_long = out_long << block_size | (index as i64); } data_buf.put_i64(out_long); } @@ -80,9 +81,10 @@ impl<'a> ClientPacket for CChunkData<'a> { // Bits per entry data_buf.put_u8(DIRECT_PALETTE_BITS as u8); // Data array length - data_buf.put_var_int(&VarInt( - chunk.len().div_ceil(64 / DIRECT_PALETTE_BITS as usize) as i32, - )); + let data_array_len = chunk.len().div_ceil(64 / DIRECT_PALETTE_BITS as usize); + data_buf.put_var_int(&VarInt(data_array_len as i32)); + + data_buf.reserve(data_array_len * 8); for block_clump in chunk.chunks(64 / DIRECT_PALETTE_BITS as usize) { let mut out_long: i64 = 0; let mut shift = 0; From 5f9970ecdcd6a9c62e4b6d09100955c15ad0a0e3 Mon Sep 17 00:00:00 2001 From: Alvsch <94403567+Alvsch@users.noreply.github.com> Date: Wed, 30 Oct 2024 12:50:18 +0100 Subject: [PATCH 2/2] Reuse palette instead of palette_ids --- pumpkin-protocol/src/client/play/c_chunk_data.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pumpkin-protocol/src/client/play/c_chunk_data.rs b/pumpkin-protocol/src/client/play/c_chunk_data.rs index 07849d42..ecea8bb8 100644 --- a/pumpkin-protocol/src/client/play/c_chunk_data.rs +++ b/pumpkin-protocol/src/client/play/c_chunk_data.rs @@ -54,9 +54,7 @@ impl<'a> ClientPacket for CChunkData<'a> { // Palette length data_buf.put_var_int(&VarInt(palette.len() as i32)); - let mut palette_ids = Vec::with_capacity(palette.len()); palette.iter().for_each(|id| { - palette_ids.push(*id); // Palette data_buf.put_var_int(&VarInt(id.get_id_mojang_repr())); }); @@ -68,7 +66,7 @@ impl<'a> ClientPacket for CChunkData<'a> { 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_ids + let index = palette .iter() .position(|b| *b == block) .expect("Its just got added, ofc it should be there");