From 99514c4b7944d630f0b4eec52ab5c11357207e80 Mon Sep 17 00:00:00 2001 From: rdbtCVS <87152661+rdbtCVS@users.noreply.github.com> Date: Wed, 14 Oct 2026 23:16:21 +0100 Subject: [PATCH] perf(client): buffer model and render JSON reads --- .../examples/json_readers_bench.rs | 205 ++++++++++++++++++ crates/horizon-client/src/models.rs | 4 +- crates/horizon-client/src/models/catalog.rs | 2 +- 3 files changed, 208 insertions(+), 3 deletions(-) create mode 100644 crates/horizon-client/examples/json_readers_bench.rs diff --git a/crates/horizon-client/examples/json_readers_bench.rs b/crates/horizon-client/examples/json_readers_bench.rs new file mode 100644 index 000000000..a9e370f0a --- /dev/null +++ b/crates/horizon-client/examples/json_readers_bench.rs @@ -0,0 +1,205 @@ +//! Compare the exact read/deserialize sequences used by the model loaders. +//! Run with `cargo run -p horizon-client --profile fast --example json_readers_bench`. +//! Optional arguments: resource directory, repetitions (default 9). +use serde_json::{Map, Value, json}; +use std::{ + collections::BTreeMap, + fmt::Debug, + fs::{self, File}, + hint::black_box, + io::BufReader, + path::{Path, PathBuf}, + time::Instant, +}; + +type Result = horizon_client::Result; + +#[derive(Clone, Copy)] +enum Reader { + File, + Buffered, + Slice, +} +const READERS: [Reader; 3] = [Reader::File, Reader::Buffered, Reader::Slice]; + +// These match the map types and reader expressions at all three production sites. +macro_rules! reader { + ($name:ident, $map:ty) => { + fn $name(path: &Path, reader: Reader) -> Result<$map> { + Ok(match reader { + Reader::File => serde_json::from_reader(File::open(path)?)?, + Reader::Buffered => serde_json::from_reader(BufReader::new(File::open(path)?))?, + Reader::Slice => serde_json::from_slice(&fs::read(path)?)?, + }) + } + }; +} +reader!(prepare_facts, Map); +reader!(catalog_facts, BTreeMap); + +struct Fixture(PathBuf); +impl Fixture { + fn new() -> Result { + let root = + std::env::temp_dir().join(format!("horizon-json-readers-{}", std::process::id())); + fs::create_dir(&root)?; + Ok(Self(root)) + } +} +impl Drop for Fixture { + fn drop(&mut self) { + let _ = fs::remove_dir_all(&self.0); + } +} + +fn fixtures(root: &Path) -> Result<()> { + let states: Vec<_> = (0..56) + .map(|i| { + json!({"facing": (["north", "east", "south", "west"][i % 4]), + "powered": if i / 4 % 2 == 0 { "false" } else { "true" }, + "age": (i / 8).to_string()}) + }) + .collect(); + let mut models = Map::new(); + let mut render = Map::new(); + for i in 0..1024 { + let name = format!("minecraft:fixture_block_{i:04}"); + models.insert( + name.clone(), + json!({ + "schema": {"facing": {"named": ["north", "east", "south", "west"]}, + "powered": {"named": ["false", "true"]}, "age": {"integer": [0, 6]}}, + "states": states + }), + ); + render.insert( + name, + json!({"rule": "never", "can_occlude": true, "bars_tag": false}), + ); + } + fs::write(root.join("model-states.json"), serde_json::to_vec(&models)?)?; + fs::write( + root.join("render-definitions.json"), + serde_json::to_vec(&render)?, + )?; + Ok(()) +} + +fn benchmark( + label: &str, + path: &Path, + repetitions: usize, + read: impl Fn(&Path, Reader) -> Result, +) -> Result<()> { + let expected = read(path, Reader::File)?; + for reader in READERS { + assert_eq!(expected, read(path, reader)?); + for _ in 0..2 { + black_box(read(black_box(path), reader)?); + } + } + drop(expected); + let mut samples: [Vec; 3] = std::array::from_fn(|_| Vec::new()); + for repetition in 0..repetitions { + // Rotate reader order to distribute drift across implementations. + for offset in 0..3 { + let index = (repetition + offset) % 3; + let start = Instant::now(); + let value = black_box(read(black_box(path), READERS[index])?); + samples[index].push(start.elapsed().as_secs_f64() * 1000.0); + drop(value); // Match the loaders: retain the parsed tree after loading. + } + } + let median: Vec<_> = samples + .iter_mut() + .map(|s| { + s.sort_by(f64::total_cmp); + s[s.len() / 2] + }) + .collect(); + println!( + "{label}: {} bytes, {repetitions} samples/reader, median ms: File={:.3}, BufReader={:.3}, read+slice={:.3}; slice speedup={:.2}x", + fs::metadata(path)?.len(), + median[0], + median[1], + median[2], + median[0] / median[2] + ); + Ok(()) +} + +fn main() -> Result<()> { + let args: Vec<_> = std::env::args().skip(1).collect(); + let fixture = if args.is_empty() { + Some(Fixture::new()?) + } else { + None + }; + let root = fixture + .as_ref() + .map(|f| f.0.clone()) + .unwrap_or_else(|| PathBuf::from(&args[0])); + if fixture.is_some() { + fixtures(&root)?; + println!( + "Generated representative fixtures: 1024 blocks, 57344 model states; warm filesystem cache." + ); + } + let repetitions = args.get(1).map(|s| s.parse()).transpose()?.unwrap_or(9); + assert!(repetitions > 0, "repetitions must be positive"); + benchmark( + "prepare model states", + &root.join("model-states.json"), + repetitions, + prepare_facts, + )?; + benchmark( + "catalog model states", + &root.join("model-states.json"), + repetitions, + catalog_facts, + )?; + benchmark( + "render definitions", + &root.join("render-definitions.json"), + repetitions, + catalog_facts, + ) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn reader_equivalence_and_errors() -> Result<()> { + let fixture = Fixture::new()?; + let path = fixture.0.join("test.json"); + fs::write(&path, br#"{"minecraft:stone":{"schema":{},"states":[{}]},"unicode":"\u2603","duplicate":1,"duplicate":2}"#)?; + let expected = prepare_facts(&path, Reader::File)?; + for reader in READERS { + assert_eq!(expected, prepare_facts(&path, reader)?); + assert_eq!( + expected.iter().collect::>(), + catalog_facts(&path, reader)?.iter().collect() + ); + } + for invalid in [b"{\"a\": ".as_slice(), b"{} trailing", b"{\"a\":\"\xff\"}"] { + fs::write(&path, invalid)?; + for reader in READERS { + let error = prepare_facts(&path, reader).unwrap_err(); + assert!(error.downcast_ref::().is_some()); + assert!(catalog_facts(&path, reader).is_err()); + } + } + fs::remove_file(&path)?; + for reader in READERS { + let error = prepare_facts(&path, reader).unwrap_err(); + assert_eq!( + error.downcast_ref::().unwrap().kind(), + std::io::ErrorKind::NotFound + ); + } + Ok(()) + } +} diff --git a/crates/horizon-client/src/models.rs b/crates/horizon-client/src/models.rs index 23280150b..b593c1f66 100644 --- a/crates/horizon-client/src/models.rs +++ b/crates/horizon-client/src/models.rs @@ -56,7 +56,7 @@ pub fn prepare( sprites: &mut Sprites, ) -> Result { let facts: serde_json::Map = - serde_json::from_reader(std::fs::File::open(root.join("model-states.json"))?)?; + serde_json::from_slice(&std::fs::read(root.join("model-states.json"))?)?; let blocks: Vec<&str> = facts .keys() .filter(|name| registry.default_state(name).is_some()) @@ -111,7 +111,7 @@ pub fn load_visibility( .tag; let occlusion = CachedCollisionRegistry::from_nbt(&data, registry)?; let raw: BTreeMap = - serde_json::from_reader(fs::File::open(root.join("render-definitions.json"))?)?; + serde_json::from_slice(&fs::read(root.join("render-definitions.json"))?)?; let definitions = raw .into_iter() .map(|(name, v)| { diff --git a/crates/horizon-client/src/models/catalog.rs b/crates/horizon-client/src/models/catalog.rs index 61db847a0..d39d19a49 100644 --- a/crates/horizon-client/src/models/catalog.rs +++ b/crates/horizon-client/src/models/catalog.rs @@ -56,7 +56,7 @@ impl StateModels { sprites: &mut Sprites, ) -> Result { let facts: BTreeMap = - serde_json::from_reader(fs::File::open(root.join("model-states.json"))?)?; + serde_json::from_slice(&fs::read(root.join("model-states.json"))?)?; let mut choices = Vec::new(); let mut states = BTreeMap::new(); let mut variants = BTreeSet::new();