Skip to content

Commit

Permalink
small CChunkData write improvement (#216)
Browse files Browse the repository at this point in the history
* small CChunkData write improvement

* Reuse palette instead of palette_ids
  • Loading branch information
Alvsch authored Oct 31, 2024
1 parent 99c9866 commit 3f2bbcc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
28 changes: 14 additions & 14 deletions pumpkin-protocol/src/client/play/c_chunk_data.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::collections::HashMap;

use crate::{bytebuf::ByteBuffer, BitSet, ClientPacket, VarInt};
use itertools::Itertools;

Expand Down Expand Up @@ -55,23 +53,24 @@ 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);

palette.iter().for_each(|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
.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);
}
Expand All @@ -80,9 +79,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;
Expand Down

0 comments on commit 3f2bbcc

Please sign in to comment.