8a03758c3a
A content pack's first start rewrote 19 per-state tables one after another and reopened each zip mod's archive for every file it copied (its whole index each time). Every start then decoded the atlases' mod textures and parsed the mods' block models one file at a time on the main thread: with ATM10's 42 data mods that was about 70 % of the sampled startup. - mod_content: the tables are rewritten on up to three threads, largest first; a zip mod's archive is opened once for all its files (horizon-mods' Content::reader). - hud::atlas_sprites and ParsedModels::load list their files first, then decode and parse them on every core (par::par_map, results in the input order). The sprites come out in the order the one-by-one loop gave, and the content root is byte-identical. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
55 lines
1.8 KiB
Rust
55 lines
1.8 KiB
Rust
//! Loading many small files at once: startup reads tens of thousands of
|
|
//! textures and models with a content pack, and one at a time it waits on
|
|
//! each open and read in turn.
|
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
|
|
|
/// `f` over `items` on the machine's threads, results in `items`' order.
|
|
/// Threads take the next item as they finish one, so a run of slow items
|
|
/// doesn't hold back the others.
|
|
pub fn par_map<T: Sync, R: Send>(items: &[T], f: impl Fn(&T) -> R + Sync) -> Vec<R> {
|
|
let threads = std::thread::available_parallelism()
|
|
.map_or(1, |n| n.get())
|
|
.min(items.len());
|
|
if threads <= 1 {
|
|
return items.iter().map(f).collect();
|
|
}
|
|
let next = AtomicUsize::new(0);
|
|
let mut out: Vec<(usize, R)> = std::thread::scope(|scope| {
|
|
let workers: Vec<_> = (0..threads)
|
|
.map(|_| {
|
|
scope.spawn(|| {
|
|
let mut done = Vec::new();
|
|
loop {
|
|
let i = next.fetch_add(1, Ordering::Relaxed);
|
|
let Some(item) = items.get(i) else {
|
|
return done;
|
|
};
|
|
done.push((i, f(item)));
|
|
}
|
|
})
|
|
})
|
|
.collect();
|
|
workers
|
|
.into_iter()
|
|
.flat_map(|w| w.join().unwrap_or_else(|e| std::panic::resume_unwind(e)))
|
|
.collect()
|
|
});
|
|
out.sort_unstable_by_key(|(i, _)| *i);
|
|
out.into_iter().map(|(_, r)| r).collect()
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::par_map;
|
|
|
|
#[test]
|
|
fn results_keep_the_items_order() {
|
|
let items: Vec<u32> = (0..1000).collect();
|
|
assert_eq!(
|
|
par_map(&items, |x| x * 2),
|
|
items.iter().map(|x| x * 2).collect::<Vec<_>>()
|
|
);
|
|
assert!(par_map(&[] as &[u32], |x| *x).is_empty());
|
|
}
|
|
}
|