Skip to content

Commit

Permalink
Use rayon for processing chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
lukas0008 committed Aug 13, 2024
1 parent ccd915a commit b68e9b8
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 23 deletions.
40 changes: 40 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pumpkin-world/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ flate2 = "1.0.31"
serde = { version = "1.0.205", features = ["derive"] }
lazy_static = "1.5.0"
serde_json = "1.0.122"
rayon = "1.10.0"
36 changes: 21 additions & 15 deletions pumpkin-world/src/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::{io::Read, path::PathBuf, sync::Arc};

use flate2::bufread::ZlibDecoder;
use itertools::Itertools;
use rayon::prelude::*;
use thiserror::Error;
use tokio::{
fs::File,
Expand Down Expand Up @@ -201,23 +202,28 @@ impl Level {

let chunk_data = file_buf.drain(0..size as usize).collect_vec();

let mut z = ZlibDecoder::new(&chunk_data[..]);
let mut chunk_data = Vec::new();
match z.read_to_end(&mut chunk_data) {
Ok(_) => {}
Err(err) => {
return ((old_chunk_x, old_chunk_z), Err(WorldError::ZlibError(err)))
}
}

(
(old_chunk_x, old_chunk_z),
ChunkData::from_bytes(chunk_data, (old_chunk_x, old_chunk_z)),
)
((old_chunk_x, old_chunk_z), Ok(chunk_data))
}
}))
.await
.into_iter()
.collect_vec()
.into_par_iter()
.map(|((old_chunk_x, old_chunk_z), chunk_data)| {
let chunk_data = match chunk_data {
Ok(c) => c,
Err(e) => return ((old_chunk_x, old_chunk_z), Err(e)),
};
let mut z = ZlibDecoder::new(&chunk_data[..]);
let mut chunk_data = Vec::new();
match z.read_to_end(&mut chunk_data) {
Ok(_) => {}
Err(err) => return ((old_chunk_x, old_chunk_z), Err(WorldError::ZlibError(err))),
}

(
(old_chunk_x, old_chunk_z),
ChunkData::from_bytes(chunk_data, (old_chunk_x, old_chunk_z)),
)
})
.collect()
}
}
11 changes: 3 additions & 8 deletions pumpkin/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,8 +332,8 @@ impl Server {
async fn spawn_test_chunk(client: &mut Client) {
let mut wanted_chunks = Vec::new();

for i in -3i32..3 {
for j in -3i32..3 {
for i in -32i32..32 {
for j in -32i32..32 {
wanted_chunks.push((i, j))
}
}
Expand Down Expand Up @@ -363,12 +363,7 @@ impl Server {
);
}
match &chunk.1 {
Err(err) => log::warn!(
"Chunk loading failed for chunk ({},{}): {}",
chunk.0 .0,
chunk.0 .1,
err
),
Err(err) => {},
Ok(data) => client.send_packet(&CChunkData(data)),
}
});
Expand Down

0 comments on commit b68e9b8

Please sign in to comment.