Skip to content

Commit

Permalink
refactor: remove unnecessary blocking in AnvilChunkFile methods for i…
Browse files Browse the repository at this point in the history
…mproved performance
  • Loading branch information
Mili committed Feb 25, 2025
1 parent 5dde386 commit 824ec85
Showing 1 changed file with 18 additions and 26 deletions.
44 changes: 18 additions & 26 deletions pumpkin-world/src/chunk/format/anvil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,40 +351,32 @@ impl ChunkSerializer for AnvilChunkFile {
}

fn update_chunks(&mut self, chunks_data: &[&Self::Data]) -> Result<(), ChunkWritingError> {
// We block here to avoid context switching
// because Anvil Format compress the chunks sequentially
tokio::task::block_in_place(|| {
for chunk in chunks_data {
let index = AnvilChunkFile::get_chunk_index(&chunk.position);
self.chunks_data[index] = Some(AnvilChunkData::from_chunk(chunk)?);
}
for chunk in chunks_data {
let index = AnvilChunkFile::get_chunk_index(&chunk.position);
self.chunks_data[index] = Some(AnvilChunkData::from_chunk(chunk)?);
}

Ok(())
})
Ok(())
}

fn get_chunks(
&self,
chunks: &[Vector2<i32>],
) -> Vec<LoadedData<Self::Data, ChunkReadingError>> {
// We block here to avoid context switching
// because Anvil Format decompress the chunks sequentially
tokio::task::block_in_place(|| {
chunks
.par_iter()
.map(|chunk| {
let index = AnvilChunkFile::get_chunk_index(chunk);
if let Some(data) = &self.chunks_data[index] {
match data.to_chunk(*chunk) {
Ok(chunk) => LoadedData::Loaded(chunk),
Err(err) => LoadedData::Error((*chunk, err)),
}
} else {
LoadedData::Missing(*chunk)
chunks
.par_iter()
.map(|chunk| {
let index = AnvilChunkFile::get_chunk_index(chunk);
if let Some(data) = &self.chunks_data[index] {
match data.to_chunk(*chunk) {
Ok(chunk) => LoadedData::Loaded(chunk),
Err(err) => LoadedData::Error((*chunk, err)),
}
})
.collect::<Vec<_>>()
})
} else {
LoadedData::Missing(*chunk)
}
})
.collect::<Vec<_>>()
}
}

Expand Down

0 comments on commit 824ec85

Please sign in to comment.