From bead8549d49dd3f236739326868bdfa2f1aeca9b Mon Sep 17 00:00:00 2001 From: Lol3rrr Date: Sun, 22 Sep 2024 17:21:12 +0200 Subject: [PATCH] Performance Improvements MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Work on improving the performance of the entity parsing stuff Benchmarked using hyperfine with 2 warmup runs on my M1 Macbook Entities - Previous: 14.845 s ± 0.095 s No-Entities - Previous: 318.4 ms ± 18.1 ms Entities - New: 1.117 s ± 0.021 s No-Entities - New: 325.4 ms ± 16.7 ms --- src/parser/entities.rs | 6 ++---- src/parser/propcontroller.rs | 12 ++++++------ 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/src/parser/entities.rs b/src/parser/entities.rs index ed54381..d23fc5c 100644 --- a/src/parser/entities.rs +++ b/src/parser/entities.rs @@ -75,7 +75,7 @@ impl EntityContext { None => panic!(), }; - let mut fields = Vec::new(); + let mut fields = Vec::with_capacity(n_updates); for path in paths.paths().take(n_updates) { let field = path.find(&class.serializer)?; let field_info = field.get_propinfo(path); @@ -84,9 +84,7 @@ impl EntityContext { if let Some(fi) = field_info { if let Some(prop_info) = prop_controller - .prop_infos - .iter() - .find(|pi| fi.prop_id == pi.id) + .prop_infos.get(&fi.prop_id) { fields.push(EntityProp { field_info: fi, diff --git a/src/parser/propcontroller.rs b/src/parser/propcontroller.rs index a6f540f..c9697dd 100644 --- a/src/parser/propcontroller.rs +++ b/src/parser/propcontroller.rs @@ -72,7 +72,7 @@ pub const INPUT_HISTORY_PLAYER_TICK_COUNT_OFFSET: u32 = 5; pub const INPUT_HISTORY_PLAYER_TICK_FRACTION_OFFSET: u32 = 6; use super::sendtables::{Field, ValueField}; -use std::collections::HashMap; +use std::{collections::HashMap, sync::Arc}; #[derive(Debug, Clone)] pub struct PropController { @@ -81,7 +81,7 @@ pub struct PropController { pub name_to_id: HashMap, pub id_to_name: HashMap, pub path_to_name: HashMap<[i32; 7], String>, - pub prop_infos: Vec, + pub prop_infos: HashMap, } #[derive(Debug, Clone)] @@ -91,7 +91,7 @@ pub struct SpecialIDs {} pub struct PropInfo { pub id: u32, // pub prop_type: PropType, - pub prop_name: String, + pub prop_name: Arc, // pub prop_friendly_name: String, // pub is_player_prop: bool } @@ -104,7 +104,7 @@ impl PropController { name_to_id: HashMap::new(), id_to_name: HashMap::new(), path_to_name: HashMap::new(), - prop_infos: Vec::new(), + prop_infos: HashMap::new(), } } @@ -271,9 +271,9 @@ impl PropController { } fn insert_propinfo(&mut self, prop_name: &str, f: &mut ValueField) { - self.prop_infos.push(PropInfo { + self.prop_infos.insert(f.prop_id, PropInfo { id: f.prop_id as u32, - prop_name: prop_name.to_string(), + prop_name: prop_name.into(), }); }