Merge pull request #2108 from ludovic111/perf/worldgen-data-parallel-load
🤖 Agent: pr_ready passed (horizon-world and client tests, affected harnesses), lan_e2e passed in-frame and on the server thread. GeneratorData::load on the ThinkPad 0.64 → 0.45 s warm. Queued for merge.
This commit is contained in:
@@ -339,7 +339,9 @@ pub fn shared_data(
|
||||
if let Some((_, data)) = cache.iter().find(|(path, _)| path == dir) {
|
||||
return Ok(data.clone());
|
||||
}
|
||||
let started = std::time::Instant::now();
|
||||
let data = GeneratorData::load(dir, registry)?;
|
||||
log::info!("Horizon: worldgen data loaded in {:?}", started.elapsed());
|
||||
cache.push((dir.to_owned(), data.clone()));
|
||||
Ok(data)
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ use serde_json::Value;
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
fs::{self, File},
|
||||
io::{self, BufReader},
|
||||
io,
|
||||
path::Path,
|
||||
sync::Arc,
|
||||
};
|
||||
@@ -582,8 +582,10 @@ fn fail(message: &str) -> io::Error {
|
||||
invalid(message)
|
||||
}
|
||||
|
||||
/// From the file's bytes in memory: several times faster than through a
|
||||
/// reader on gameplay-facts.json (55 MB), which a first start parses.
|
||||
fn read_json(path: &Path) -> io::Result<Value> {
|
||||
serde_json::from_reader(BufReader::new(File::open(path)?)).map_err(|e| invalid(e.to_string()))
|
||||
serde_json::from_slice(&fs::read(path)?).map_err(|e| invalid(e.to_string()))
|
||||
}
|
||||
|
||||
fn interned(cache: &mut HashMap<String, Arc<str>>, text: &str) -> Arc<str> {
|
||||
|
||||
@@ -269,6 +269,20 @@ fn quantize_parameter_report(json: &str) -> Result<String, GeneratorError> {
|
||||
}
|
||||
Ok(Value::Array(entries).to_string())
|
||||
}
|
||||
/// `load` on a thread of its own ("Worldgen loader").
|
||||
fn loading<T: Send + 'static>(
|
||||
load: impl FnOnce() -> T + Send + 'static,
|
||||
) -> io::Result<std::thread::JoinHandle<T>> {
|
||||
std::thread::Builder::new()
|
||||
.name("Worldgen loader".into())
|
||||
.spawn(load)
|
||||
}
|
||||
/// A loading thread's result; its panic goes on in this thread.
|
||||
fn joined<T>(thread: std::thread::JoinHandle<T>) -> T {
|
||||
thread
|
||||
.join()
|
||||
.unwrap_or_else(|panic| std::panic::resume_unwind(panic))
|
||||
}
|
||||
impl GeneratorData {
|
||||
/// Load a worldgen bundle directory. Required files are JSON objects mapping
|
||||
/// resource identifiers to the resource's JSON text: `noises`, `densities`,
|
||||
@@ -290,6 +304,50 @@ impl GeneratorData {
|
||||
let block_tags = build_tags(&read_bundle(dir, "block_tags")?, |id| {
|
||||
registry.default_state(id).is_some()
|
||||
})?;
|
||||
let fluid_tags = match read_optional_bundle(dir, "fluid_tags")? {
|
||||
Some(raw) => build_tags(&raw, |id| {
|
||||
matches!(
|
||||
id,
|
||||
"minecraft:water"
|
||||
| "minecraft:flowing_water"
|
||||
| "minecraft:lava"
|
||||
| "minecraft:flowing_lava"
|
||||
| "minecraft:empty"
|
||||
)
|
||||
})?,
|
||||
None => BTreeMap::new(),
|
||||
};
|
||||
// The three biggest reads (the block tables, the structure templates
|
||||
// and the block behaviours) need only the tags: they run on threads of
|
||||
// their own while this one reads the rest. A new world's first chunks
|
||||
// wait for all of it, with the chunk workers idle.
|
||||
let blocks = loading({
|
||||
let (dir, tags, fluids) = (dir.join("blocks"), block_tags.clone(), fluid_tags.clone());
|
||||
move || super::generator_blocks::BlockBehavior::load(&dir, registry, &tags, &fluids)
|
||||
})?;
|
||||
let structures = loading({
|
||||
let (dir, tags) = (dir.to_owned(), block_tags.clone());
|
||||
move || -> Result<_, GeneratorError> {
|
||||
let Ok(json) = fs::read_to_string(dir.join("blocks/structure-blocks.json")) else {
|
||||
return Ok(None);
|
||||
};
|
||||
Ok(Some(Arc::new(super::structure::StructureResources::new(
|
||||
registry,
|
||||
super::structure::StateTransforms::from_json(&json, registry.len())?,
|
||||
&dir.join("structure"),
|
||||
read_optional_bundle(&dir, "processor_lists")?.unwrap_or_default(),
|
||||
&tags,
|
||||
))))
|
||||
}
|
||||
})?;
|
||||
let behaviours = loading({
|
||||
let root = dir.parent().map(Path::to_owned);
|
||||
move || {
|
||||
root.filter(|root| root.join("block-behaviours.json").exists())
|
||||
.and_then(|root| crate::level::Behaviours::load(registry, &root).ok())
|
||||
.map(Arc::new)
|
||||
}
|
||||
})?;
|
||||
let tag_set = |name: &str| -> Result<BTreeSet<&str>, GeneratorError> {
|
||||
Ok(block_tags
|
||||
.get(name)
|
||||
@@ -382,47 +440,17 @@ impl GeneratorData {
|
||||
read_optional_bundle(dir, "features")?,
|
||||
read_optional_bundle(dir, "placed_features")?,
|
||||
) {
|
||||
(Some(features), Some(placed)) => {
|
||||
let fluid_tags = match read_optional_bundle(dir, "fluid_tags")? {
|
||||
Some(raw) => build_tags(&raw, |id| {
|
||||
matches!(
|
||||
id,
|
||||
"minecraft:water"
|
||||
| "minecraft:flowing_water"
|
||||
| "minecraft:lava"
|
||||
| "minecraft:flowing_lava"
|
||||
| "minecraft:empty"
|
||||
)
|
||||
})?,
|
||||
None => BTreeMap::new(),
|
||||
};
|
||||
Some(FeatureResources {
|
||||
features,
|
||||
placed,
|
||||
feature_tags: read_optional_bundle(dir, "feature_tags")?.unwrap_or_default(),
|
||||
blocks: super::generator_blocks::BlockBehavior::load(
|
||||
&dir.join("blocks"),
|
||||
registry,
|
||||
&block_tags,
|
||||
&fluid_tags,
|
||||
)?,
|
||||
fluid_tags,
|
||||
state_providers: read_optional_bundle(dir, "state_providers")?
|
||||
.unwrap_or_default(),
|
||||
})
|
||||
}
|
||||
(Some(features), Some(placed)) => Some(FeatureResources {
|
||||
features,
|
||||
placed,
|
||||
feature_tags: read_optional_bundle(dir, "feature_tags")?.unwrap_or_default(),
|
||||
blocks: joined(blocks)?,
|
||||
fluid_tags,
|
||||
state_providers: read_optional_bundle(dir, "state_providers")?.unwrap_or_default(),
|
||||
}),
|
||||
_ => None,
|
||||
};
|
||||
let structures = match fs::read_to_string(dir.join("blocks/structure-blocks.json")) {
|
||||
Ok(json) => Some(Arc::new(super::structure::StructureResources::new(
|
||||
registry,
|
||||
super::structure::StateTransforms::from_json(&json, registry.len())?,
|
||||
&dir.join("structure"),
|
||||
read_optional_bundle(dir, "processor_lists")?.unwrap_or_default(),
|
||||
&block_tags,
|
||||
))),
|
||||
Err(_) => None,
|
||||
};
|
||||
let structures = joined(structures)?;
|
||||
let structure_resources = match (
|
||||
read_optional_bundle(dir, "structures")?,
|
||||
read_optional_bundle(dir, "structure_sets")?,
|
||||
@@ -435,11 +463,7 @@ impl GeneratorData {
|
||||
}),
|
||||
_ => None,
|
||||
};
|
||||
let behaviours = dir
|
||||
.parent()
|
||||
.filter(|root| root.join("block-behaviours.json").exists())
|
||||
.and_then(|root| crate::level::Behaviours::load(registry, root).ok())
|
||||
.map(Arc::new);
|
||||
let behaviours = joined(behaviours);
|
||||
Ok(Arc::new(Self {
|
||||
behaviours,
|
||||
structure_resources,
|
||||
|
||||
@@ -108,15 +108,26 @@ impl BlockBehavior {
|
||||
let json = |name: &str| -> Result<Value, GeneratorError> {
|
||||
Ok(serde_json::from_str(&fs::read_to_string(dir.join(name))?)?)
|
||||
};
|
||||
let collisions =
|
||||
CachedCollisionRegistry::from_table(read("cached-collision.nbt")?.table("collision")?)?;
|
||||
let properties = BlockPropertiesRegistry::from_table(
|
||||
read("block-properties.nbt")?.table("block property")?,
|
||||
&collisions,
|
||||
)?;
|
||||
let support = Arc::new(BlockSupportRegistry::from_table(
|
||||
read("cached-support.nbt")?.table("support")?,
|
||||
)?);
|
||||
// The support table reads on its own thread while the collision and
|
||||
// property tables (which need the collisions) read on this one.
|
||||
let (collisions, properties, support) = std::thread::scope(|scope| {
|
||||
let support = scope.spawn(|| -> Result<_, GeneratorError> {
|
||||
Ok(BlockSupportRegistry::from_table(
|
||||
read("cached-support.nbt")?.table("support")?,
|
||||
)?)
|
||||
});
|
||||
let collisions = CachedCollisionRegistry::from_table(
|
||||
read("cached-collision.nbt")?.table("collision")?,
|
||||
)?;
|
||||
let properties = BlockPropertiesRegistry::from_table(
|
||||
read("block-properties.nbt")?.table("block property")?,
|
||||
&collisions,
|
||||
)?;
|
||||
let support = support
|
||||
.join()
|
||||
.unwrap_or_else(|panic| std::panic::resume_unwind(panic))?;
|
||||
Ok::<_, GeneratorError>((collisions, properties, Arc::new(support)))
|
||||
})?;
|
||||
let collision = Arc::new(BlockCollisionRegistry::new(Arc::new(collisions))?);
|
||||
let rules = json("survival-rules.json")?
|
||||
.as_object()
|
||||
|
||||
Reference in New Issue
Block a user