Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

small CChunkData write improvement #216

Merged
merged 2 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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