summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBen Bridle <ben@derelict.engineering>2025-03-11 16:13:23 +1300
committerBen Bridle <ben@derelict.engineering>2025-03-11 16:13:23 +1300
commitbd7955cbc1c1746f37dcb0742603994fd2e04c90 (patch)
tree00bd1f0e6478aa4ea90de5db991a5bb9348f99c2 /src
parent6fb6f2836232fe78dce507fa899629bbefb7b449 (diff)
downloadassembler-bd7955cbc1c1746f37dcb0742603994fd2e04c90.zip
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.
Diffstat (limited to 'src')
-rw-r--r--src/resolver.rs18
1 files changed, 10 insertions, 8 deletions
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<usize>,
+ pub child_ids: HashSet<usize>,
/// Pointers to source units that must be included before this unit.
- pub parent_ids: Vec<usize>,
+ pub parent_ids: HashSet<usize>,
}
pub struct TrackedDefinition {