diff --git a/Cargo.lock b/Cargo.lock index 429b2100..744918da 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -223,6 +223,25 @@ dependencies = [ "crossbeam-utils", ] +[[package]] +name = "crossbeam-deque" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "613f8cc01fe9cf1a3eb3d7f488fd2fa8388403e97039e2f73692932e291a770d" +dependencies = [ + "crossbeam-epoch", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.9.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e" +dependencies = [ + "crossbeam-utils", +] + [[package]] name = "crossbeam-utils" version = "0.8.20" @@ -1215,6 +1234,7 @@ dependencies = [ "futures", "itertools", "lazy_static", + "rayon", "serde", "serde_json", "thiserror", @@ -1260,6 +1280,26 @@ dependencies = [ "getrandom", ] +[[package]] +name = "rayon" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b418a60154510ca1a002a752ca9714984e21e4241e804d32555251faf8b78ffa" +dependencies = [ + "either", + "rayon-core", +] + +[[package]] +name = "rayon-core" +version = "1.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1465873a3dfdaa8ae7cb14b4383657caab0b3e8a0aa9ae8e04b044854c8dfce2" +dependencies = [ + "crossbeam-deque", + "crossbeam-utils", +] + [[package]] name = "reqwest" version = "0.12.5" diff --git a/pumpkin-world/Cargo.toml b/pumpkin-world/Cargo.toml index 4f2e6399..38dfc8a0 100644 --- a/pumpkin-world/Cargo.toml +++ b/pumpkin-world/Cargo.toml @@ -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" diff --git a/pumpkin-world/src/world.rs b/pumpkin-world/src/world.rs index f41582b5..28a9d905 100644 --- a/pumpkin-world/src/world.rs +++ b/pumpkin-world/src/world.rs @@ -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, @@ -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() } } diff --git a/pumpkin/src/server.rs b/pumpkin/src/server.rs index e92f9bbd..7df712b1 100644 --- a/pumpkin/src/server.rs +++ b/pumpkin/src/server.rs @@ -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)) } } @@ -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)), } });