Skip to content

Commit

Permalink
Disabled compression and made the registry use memory slice instead o…
Browse files Browse the repository at this point in the history
…f a heap allocated vec. Disabled flate temporarily.
  • Loading branch information
Sweattypalms committed Sep 15, 2024
1 parent 83c42ed commit c4af40f
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 23 deletions.
13 changes: 13 additions & 0 deletions src/crates/ferurmc_codec/src/enc/non_primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,17 @@ impl<O: NetEncode> NetEncode for Option<O> {
None => Ok(()),
}
}
}
impl<'a, E: NetEncode> NetEncode for &'a [E] {
async fn net_encode<W>(&self, writer: &mut W) -> Result<()>
where
W: AsyncWrite + Unpin,
{
//! <WARNING> This function does not encode the size of the slice.
for v in self.iter() {
v.net_encode(writer).await?;
}

Ok(())
}
}
2 changes: 1 addition & 1 deletion src/database/chunks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl Database {

// Now, proceed with the async operation without holding `ro_tx`
if let Some(data) = data {
let chunk = ZstdCodec::decompress_data::<Chunk>(data)
let chunk = ZstdCodec::decompress_data::<Chunk>(data.as_slice())
.await
.expect("Failed to decompress chunk");
Ok(Some(chunk))
Expand Down
32 changes: 16 additions & 16 deletions src/database/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,22 @@ pub struct ZstdCodec;

impl ZstdCodec {
pub async fn compress_data<T: Encode + Send + 'static>(data: T) -> crate::Result<Vec<u8>> {
tokio::task::spawn_blocking(move || {
let mut bytes = Vec::new();
let mut compressor = zstd::Encoder::new(&mut bytes, 3)?;
bincode::encode_into_std_write(&data, &mut compressor, standard())?;
compressor.finish()?;
Ok(bytes)
})
.await?
/* let mut bytes = Vec::new();
let mut compressor = zstd::Encoder::new(&mut bytes, 3)?;
bincode::encode_into_std_write(&data, &mut compressor, standard())?;
compressor.finish()?;*/
let mut bytes = Vec::new();
bincode::encode_into_std_write(&data, &mut bytes, standard())?;
Ok(bytes)
}
pub async fn decompress_data<T: Decode + Send + 'static>(data: Vec<u8>) -> crate::Result<T> {
tokio::task::spawn_blocking(move || {
let mut decoder = zstd::Decoder::new(data.as_slice())?;
// let decoded = bincode::decode_from_slice(data.as_slice(), standard())?;
let decoded = bincode::decode_from_std_read(&mut decoder, standard())?;
Ok(decoded)
})
.await?
pub async fn decompress_data<T: Decode + Send + 'static>(data: &[u8]) -> crate::Result<T> {

// let mut decoder = zstd::Decoder::new(data.as_slice())?;
// let decoded = bincode::decode_from_slice(data.as_slice(), standard())?;
// let decoded = bincode::decode_from_std_read(&mut decoder, standard())?;

let decoded = bincode::decode_from_slice(data, standard())?;

Ok(decoded.0)
}
}
6 changes: 3 additions & 3 deletions src/net/packets/incoming/login_start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::time::Instant;

use ferrumc_codec::network_types::varint::VarInt;
#[cfg(not(test))]
use include_flate::flate;
use rand::random;
use tracing::{debug, info};
use uuid::Uuid;
Expand Down Expand Up @@ -45,7 +44,8 @@ pub struct LoginStart {
// MAKE SURE YOU RUN THE TEST IN THE login_play.rs FILE TO GENERATE THE NBT FILE
// The NBT encoded data for the dimension codec. Using flate_include cos the codec file is like 40kb
#[cfg(not(test))]
flate!(pub static NBT_CODEC: [u8] from "./.etc/nbt_codec.nbt");
// flate!(pub static NBT_CODEC: [u8] from "./.etc/nbt_codec.nbt");
const NBT_CODEC: &[u8] = include_bytes!("../../../../.etc/nbt_codec.nbt");

#[cfg(test)]
const NBT_CODEC: &[u8] = &[0u8; 1];
Expand Down Expand Up @@ -131,7 +131,7 @@ impl LoginStart {
previous_gamemode: -1,
dimension_length: VarInt::new(1),
dimension_names: vec!["minecraft:overworld".to_string()],
registry_codec: NBT_CODEC.to_vec(),
registry_codec: NBT_CODEC,
dimension_type: "minecraft:overworld".to_string(),
dimension_name: "minecraft:overworld".to_string(),
seed_hash: 0,
Expand Down
1 change: 1 addition & 0 deletions src/net/packets/outgoing/chunk_and_light_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ impl ChunkDataAndUpdateLight {
world_surface: Some(vec![i64::MAX; 37]),
}
});

let res = ChunkDataAndUpdateLight {
packet_id: VarInt::from(0x24),
chunk_x,
Expand Down
6 changes: 3 additions & 3 deletions src/net/packets/outgoing/login_play.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use ferrumc_macros::NetEncode;
/// The login play packet is sent by the server to the client to start the play state.
/// Contains info about the world
#[derive(NetEncode)]
pub struct LoginPlay {
pub struct LoginPlay<'a> {
pub packet_id: VarInt,
pub entity_id: i32,
pub hardcore: bool,
Expand All @@ -14,8 +14,8 @@ pub struct LoginPlay {
pub dimension_length: VarInt,
pub dimension_names: Vec<String>,
/// The codec for the dimension. Baked into the binary, see [crate::net::packets::incoming::login_start::LoginStart::decode].
#[encode(raw_bytes(prepend_length = false))]
pub registry_codec: Vec<u8>,
// #[encode(raw_bytes(prepend_length = false))]
pub registry_codec: &'a [u8],
pub dimension_type: String,
pub dimension_name: String,
pub seed_hash: i64,
Expand Down

0 comments on commit c4af40f

Please sign in to comment.