Skip to content

Commit

Permalink
yoo finally
Browse files Browse the repository at this point in the history
rle enabled: 45mb mem usage
rle disabled: 65mb mem usage
  • Loading branch information
suprohub committed Jan 6, 2025
1 parent 9b59198 commit b9895dd
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 23 deletions.
8 changes: 1 addition & 7 deletions pumpkin-config/src/chunk_optimization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,11 @@ use serde::{Deserialize, Serialize};

#[derive(Deserialize, Serialize)]
#[serde(default)]
#[derive(Default)]
pub struct ChunkOptimizationConfig {
pub rle_compression: Option<RleCompression>,
}

impl Default for ChunkOptimizationConfig {
fn default() -> Self {
Self {
rle_compression: Some(Default::default()),
}
}
}

#[derive(Deserialize, Serialize)]
#[serde(default)]
Expand Down
26 changes: 12 additions & 14 deletions pumpkin-world/src/chunk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,38 +236,35 @@ impl Subchunk {
Self::Rle(blocks) => {
blocks.set(convert_index(position), new_block);

if blocks.iter().all(|b| *b == new_block) {
*self = Self::Single(new_block)
} else if !compressed {
*self = Self::Multi(blocks.to_vec().try_into().unwrap())
if compressed {
self.compress();
}
}
Self::Multi(blocks) => {
blocks[convert_index(position)] = new_block;

if blocks.iter().all(|b| *b == new_block) {
*self = Self::Single(new_block)
} else if compressed && ADVANCED_CONFIG.chunk_optimization.rle_compression.is_some()
{
*self = Self::Rle(RleVec::from_iter(blocks.into_iter()))
if compressed {
self.compress();
}
}
}
}

#[allow(clippy::useless_conversion)]
pub fn optimize(&mut self) {
pub fn compress(&mut self) {
match self {
Self::Multi(blocks) => {
if blocks.iter().all(|b| b == blocks.first().unwrap()) {
*self = Self::Single(*blocks.first().unwrap())
} else if ADVANCED_CONFIG.chunk_optimization.rle_compression.is_some() {
log::info!("converting to rle");
*self = Self::Rle(RleVec::from_iter(blocks.into_iter()))
}
}
Self::Rle(blocks) => {
let mut runs = blocks.runs();
let first_run = runs.next().unwrap();
log::info!("trying convert rle to single");

if runs.all(|r| r == first_run) {
*self = Self::Single(*first_run.value)
Expand Down Expand Up @@ -344,10 +341,11 @@ impl Subchunks {
}
}

pub fn optimize(&mut self) {
pub fn compress(&mut self) {
if let Self::Multi(subchunks) = self {
log::info!("optimize started");
for subchunk in subchunks.iter_mut() {
subchunk.optimize();
subchunk.compress();
}
}
}
Expand Down Expand Up @@ -397,8 +395,8 @@ impl ChunkData {
.set_block_no_heightmap_update(position, block, compressed);
}

pub fn optimize(&mut self) {
self.subchunks.optimize();
pub fn compress(&mut self) {
self.subchunks.compress();
}

#[expect(dead_code)]
Expand Down
11 changes: 9 additions & 2 deletions pumpkin/src/world/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{collections::HashMap, sync::Arc};
use std::{collections::HashMap, sync::Arc, time::Duration};

pub mod level_time;
pub mod player_chunker;
Expand Down Expand Up @@ -597,7 +597,14 @@ impl World {
}

if ADVANCED_CONFIG.chunk_optimization.rle_compression.is_some() {
chunk_data_ref.write().await.optimize();
let chunk_ref = chunk_data_ref.clone();

tokio::spawn(async move {
tokio::time::sleep(Duration::from_millis(100)).await;

log::info!("trying optimize and not get deadlock");
chunk_ref.write().await.compress();
});
}
}

Expand Down

0 comments on commit b9895dd

Please sign in to comment.