This commit is contained in:
Piotrek
2021-05-16 17:19:23 +02:00
parent 76e952995d
commit 67bb4fddd0
4 changed files with 37 additions and 81 deletions
+8 -27
View File
@@ -56,35 +56,16 @@ impl WorldChunk {
// Blocks
let mut block_bytes = vec![0_u8; num_blocks*block::SIZE_QB];
lz4::decompress(&block_bytes_compressed, &mut block_bytes[0..num_blocks*block::SIZE_QB]).unwrap();
let block_bytes = Arc::new(block_bytes);
// Spawn workers
let (tx, rx) = mpsc::channel();
let per_worker = num_blocks / NUM_WORKERS;
let mut workers = vec![];
for t in 0..NUM_WORKERS {
let by = block_bytes.clone();
let tx = tx.clone();
// Each worker will process their part
workers.push(thread::spawn(move || {
for i in t*per_worker..(t+1)*per_worker {
let mut block = WorldBlock::new();
let idx = i * block::SIZE_QB;
block.materials.clone_from_slice(&by[idx..idx+block::SIZE_QB]);
tx.send((i, block)).unwrap();
}
}));
if lz4::decompress(&block_bytes_compressed, &mut block_bytes[0..num_blocks*block::SIZE_QB]).is_err() {
}
// Wait for workers to finish.
for handle in workers { handle.join().unwrap(); }
let mut blocks: Vec<(usize, WorldBlock)> = rx.try_iter().collect();
// Sort and add result blocks to chunk
blocks.sort_by(|a, b| (*a).0.cmp(&(*b).0));
for b in blocks { instance.blocks.push(b.1); }
for i in 0..num_blocks {
let mut block = WorldBlock::new();
let idx = i * block::SIZE_QB;
block.materials.clone_from_slice(&block_bytes[idx..idx+block::SIZE_QB]);
instance.blocks.push(block);
}
instance
}