From e60c0a236376126464b40eee9620bdfddbee1cc5 Mon Sep 17 00:00:00 2001 From: mgros Date: Sat, 26 Sep 2026 04:28:24 +0200 Subject: [PATCH 1/2] simplify definitions.rs --- src/definitions.rs | 54 ++++++++++++++-------------------------------- 1 file changed, 16 insertions(+), 38 deletions(-) diff --git a/src/definitions.rs b/src/definitions.rs index 33b83391..2519b6b6 100644 --- a/src/definitions.rs +++ b/src/definitions.rs @@ -48,55 +48,39 @@ pub enum DefinitionCollection { /// /// There should only be one instance of this structure ([`DEFINITIONS`]) // FIX: this probably can done with a macro to remove all the repetition +#[derive(Default)] pub struct Definitions { pub name_to_var_mapping: HashMap, } -impl Default for Definitions { - fn default() -> Self { - Definitions { - name_to_var_mapping: HashMap::with_capacity(30), - } - } -} - impl Definitions { - fn new() -> Self { - Definitions { - name_to_var_mapping: HashMap::with_capacity(30), - } - } - pub fn get_hashset(&self, name: &str) -> Option>> { - let names = self.name_to_var_mapping.get(name); - if let Some(DefinitionCollection::Set(set)) = names { - return Some(set.borrow()); + match self.name_to_var_mapping.get(name)? { + DefinitionCollection::Set(set) => Some(set.borrow()), + _ => None, } - return None; } pub fn get_hashmap(&self, name: &str) -> Option>> { - let names = self.name_to_var_mapping.get(name); - if let Some(DefinitionCollection::Map(map)) = names { - return Some(map.borrow()); + match self.name_to_var_mapping.get(name)? { + DefinitionCollection::Map(map) => Some(map.borrow()), + _ => None, } - return None; } pub fn get_vec(&self, name: &str) -> Option>> { - let names = self.name_to_var_mapping.get(name); - if let Some(DefinitionCollection::Vec(vec)) = names { - return Some(vec.borrow()); + match self.name_to_var_mapping.get(name)? { + DefinitionCollection::Vec(vec) => Some(vec.borrow()), + _ => None, } - return None; } } thread_local!{ /// Global variable containing all of the definitions. /// See [`Definitions`] for more details. - pub static SPEECH_DEFINITIONS: RefCell = RefCell::new( Definitions::new() ); - pub static BRAILLE_DEFINITIONS: RefCell = RefCell::new( Definitions::new() ); + pub static SPEECH_DEFINITIONS: RefCell = RefCell::new( Definitions::default() ); + pub static BRAILLE_DEFINITIONS: RefCell = RefCell::new( Definitions::default() ); pub static DEFINITIONS: &'static std::thread::LocalKey> = const { &SPEECH_DEFINITIONS }; } @@ -110,15 +94,12 @@ pub fn read_definitions_file(use_speech_defs: bool) -> Result> { let file_path = pref_manager.get_definitions_file(use_speech_defs); let definitions = if use_speech_defs {&SPEECH_DEFINITIONS} else {&BRAILLE_DEFINITIONS}; definitions.with( |defs| defs.borrow_mut().name_to_var_mapping.clear() ); - let mut files_read = read_one_definitions_file(use_speech_defs, file_path) + let files_read = read_one_definitions_file(use_speech_defs, file_path) .with_context(|| format!("in file '{}'", file_path.to_string_lossy()))?; let mut seen: HashSet = HashSet::with_capacity(files_read.len()); let mut new_files: Vec = Vec::with_capacity(files_read.len()); - for p in files_read.drain(..) { - let canon = match crate::shim_filesystem::canonicalize_shim(&p) { - Ok(c) => c, - Err(_) => p, - }; + for p in files_read { + let canon = crate::shim_filesystem::canonicalize_shim(&p).unwrap_or(p); let key = canon.to_string_lossy().to_string(); if seen.insert(key) { new_files.push(canon); @@ -152,11 +133,8 @@ pub fn read_definitions_file(use_speech_defs: bool) -> Result> { /// merge "TrigFunctions" and "AdditionalFunctionNames" into a new set named "FunctionNames" fn build_all_functions_set(defs: &mut RefMut) -> HashSet { - let trig_functions = defs.get_hashset("TrigFunctionNames").unwrap(); let mut all_functions = defs.get_hashset("AdditionalFunctionNames").unwrap().clone(); - for trig_name in trig_functions.iter() { - all_functions.insert(trig_name.clone()); - } + all_functions.extend(defs.get_hashset("TrigFunctionNames").unwrap().iter().cloned()); return all_functions; } } From 7230acd13da3560ef1db0a6ff0e378da8f9bc20a Mon Sep 17 00:00:00 2001 From: mgros Date: Sat, 26 Sep 2026 17:12:27 +0200 Subject: [PATCH 2/2] reintroduce manual tweaking of hashmap capacity --- src/definitions.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/definitions.rs b/src/definitions.rs index 2519b6b6..171c17c4 100644 --- a/src/definitions.rs +++ b/src/definitions.rs @@ -48,11 +48,18 @@ pub enum DefinitionCollection { /// /// There should only be one instance of this structure ([`DEFINITIONS`]) // FIX: this probably can done with a macro to remove all the repetition -#[derive(Default)] pub struct Definitions { pub name_to_var_mapping: HashMap, } +impl Default for Definitions { + fn default() -> Self { + Definitions { + name_to_var_mapping: HashMap::with_capacity(30), + } + } +} + impl Definitions { pub fn get_hashset(&self, name: &str) -> Option>> { match self.name_to_var_mapping.get(name)? {