From bd7955cbc1c1746f37dcb0742603994fd2e04c90 Mon Sep 17 00:00:00 2001 From: Ben Bridle Date: Tue, 11 Mar 2025 16:13:23 +1300 Subject: Prevent duplicate parent and child entries for source units The parent and child collections for hierarchical source units were previously vecs, and would receive one entry for every symbol matched. This was easily leading to many dozens of duplicate entries per unit, which, while not impacting performance, seemed wasteful. The collection types were changed to HashSets to prevent the collection of duplicate entries. --- src/resolver.rs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) (limited to 'src/resolver.rs') diff --git a/src/resolver.rs b/src/resolver.rs index d02d80f..1f6e0a2 100644 --- a/src/resolver.rs +++ b/src/resolver.rs @@ -2,6 +2,8 @@ use crate::*; use log::{info, error}; +use std::collections::HashSet; + type PushFn = fn(&mut String, &SourceFile); @@ -60,16 +62,16 @@ impl Resolver { match parent_id { Some(parent_id) => match self.source_units.get_mut(parent_id) { - Some(parent) => parent.child_ids.push(source_id), - None => error!("Could not find parent (#{parent_id}) of source unit #{source_id}"), + Some(parent) => { parent.child_ids.insert(source_id); } + None => { error!("Could not find parent (#{parent_id}) of source unit #{source_id}"); } } None => self.root_unit_ids.push(source_id), } self.source_units.push( HeirarchicalSourceUnit { source_unit, - child_ids: Vec::new(), - parent_ids: Vec::new(), + child_ids: HashSet::new(), + parent_ids: HashSet::new(), } ); } @@ -213,9 +215,9 @@ impl Resolver { // A unit cannot be its own parent. let is_self = reference.tracked.source_id == definition.tracked.source_id; let must_precede = SymbolRole::Definition(DefinitionType::MustPrecedeReference); - if is_self || definition.tracked.symbol.role != must_precede { continue; } + if is_self || definition.tracked.symbol.role != must_precede { continue; } let referencing_unit = &mut self.source_units[reference.tracked.source_id]; - referencing_unit.parent_ids.push(definition.tracked.source_id); + referencing_unit.parent_ids.insert(definition.tracked.source_id); }; } } @@ -285,9 +287,9 @@ impl Resolver { pub struct HeirarchicalSourceUnit { pub source_unit: SourceUnit, /// Pointers to source units that resolve references this unit. - pub child_ids: Vec, + pub child_ids: HashSet, /// Pointers to source units that must be included before this unit. - pub parent_ids: Vec, + pub parent_ids: HashSet, } pub struct TrackedDefinition { -- cgit v1.2.3-70-g09d2