Skip to content

Commit 65847d6

Browse files
committed
Populate name_dependents during indexing in local graph
1 parent 746e90c commit 65847d6

File tree

2 files changed

+39
-28
lines changed

2 files changed

+39
-28
lines changed

rust/rubydex/src/indexing/local_graph.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::collections::hash_map::Entry;
33
use crate::diagnostic::{Diagnostic, Rule};
44
use crate::model::definitions::Definition;
55
use crate::model::document::Document;
6+
use crate::model::graph::NameDependent;
67
use crate::model::identity_maps::IdentityHashMap;
78
use crate::model::ids::{DefinitionId, NameId, ReferenceId, StringId, UriId};
89
use crate::model::name::{Name, NameRef};
@@ -18,6 +19,7 @@ type LocalGraphParts = (
1819
IdentityHashMap<NameId, NameRef>,
1920
IdentityHashMap<ReferenceId, ConstantReference>,
2021
IdentityHashMap<ReferenceId, MethodRef>,
22+
IdentityHashMap<NameId, Vec<NameDependent>>,
2123
);
2224

2325
#[derive(Debug)]
@@ -29,6 +31,7 @@ pub struct LocalGraph {
2931
names: IdentityHashMap<NameId, NameRef>,
3032
constant_references: IdentityHashMap<ReferenceId, ConstantReference>,
3133
method_references: IdentityHashMap<ReferenceId, MethodRef>,
34+
name_dependents: IdentityHashMap<NameId, Vec<NameDependent>>,
3235
}
3336

3437
impl LocalGraph {
@@ -42,6 +45,7 @@ impl LocalGraph {
4245
names: IdentityHashMap::default(),
4346
constant_references: IdentityHashMap::default(),
4447
method_references: IdentityHashMap::default(),
48+
name_dependents: IdentityHashMap::default(),
4549
}
4650
}
4751

@@ -70,6 +74,13 @@ impl LocalGraph {
7074
pub fn add_definition(&mut self, definition: Definition) -> DefinitionId {
7175
let definition_id = definition.id();
7276

77+
if let Some(name_id) = definition.name_id() {
78+
self.name_dependents
79+
.entry(*name_id)
80+
.or_default()
81+
.push(NameDependent::Definition(definition_id));
82+
}
83+
7384
if self.definitions.insert(definition_id, definition).is_some() {
7485
debug_assert!(false, "DefinitionId collision in local graph");
7586
}
@@ -110,6 +121,8 @@ impl LocalGraph {
110121

111122
pub fn add_name(&mut self, name: Name) -> NameId {
112123
let name_id = name.id();
124+
let nesting_id = name.nesting().as_ref().copied();
125+
let parent_scope_id = name.parent_scope().as_ref().copied();
113126

114127
match self.names.entry(name_id) {
115128
Entry::Occupied(mut entry) => {
@@ -118,6 +131,15 @@ impl LocalGraph {
118131
}
119132
Entry::Vacant(entry) => {
120133
entry.insert(NameRef::Unresolved(Box::new(name)));
134+
135+
// Track nesting and parent_scope dependents for new names
136+
let dep = NameDependent::Name(name_id);
137+
for dep_owner_id in [nesting_id, parent_scope_id].into_iter().flatten() {
138+
let deps = self.name_dependents.entry(dep_owner_id).or_default();
139+
if !deps.contains(&dep) {
140+
deps.push(dep);
141+
}
142+
}
121143
}
122144
}
123145

@@ -134,6 +156,11 @@ impl LocalGraph {
134156
pub fn add_constant_reference(&mut self, reference: ConstantReference) -> ReferenceId {
135157
let reference_id = reference.id();
136158

159+
self.name_dependents
160+
.entry(*reference.name_id())
161+
.or_default()
162+
.push(NameDependent::Reference(reference_id));
163+
137164
if self.constant_references.insert(reference_id, reference).is_some() {
138165
debug_assert!(false, "ReferenceId collision in local graph");
139166
}
@@ -184,6 +211,7 @@ impl LocalGraph {
184211
self.names,
185212
self.constant_references,
186213
self.method_references,
214+
self.name_dependents,
187215
)
188216
}
189217
}

rust/rubydex/src/model/graph.rs

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,7 @@ impl Graph {
730730
/// Panics if a name that was just inserted into the graph cannot be found when looking up
731731
/// its `parent_scope`.
732732
fn extend(&mut self, local_graph: LocalGraph, work: &mut Vec<Unit>) {
733-
let (uri_id, document, definitions, strings, names, constant_references, method_references) =
733+
let (uri_id, document, definitions, strings, names, constant_references, method_references, name_dependents) =
734734
local_graph.into_parts();
735735

736736
if self.documents.insert(uri_id, document).is_some() {
@@ -756,33 +756,12 @@ impl Graph {
756756
entry.get_mut().increment_ref_count(name_ref.ref_count());
757757
}
758758
Entry::Vacant(entry) => {
759-
// Track nesting and parent_scope dependents for new names only
760-
if let Some(nesting_id) = entry.insert(name_ref).nesting() {
761-
let deps = self.name_dependents.entry(*nesting_id).or_default();
762-
let dep = NameDependent::Name(name_id);
763-
if !deps.contains(&dep) {
764-
deps.push(dep);
765-
}
766-
}
767-
if let Some(parent_id) = self.names.get(&name_id).unwrap().parent_scope().as_ref() {
768-
let deps = self.name_dependents.entry(*parent_id).or_default();
769-
let dep = NameDependent::Name(name_id);
770-
if !deps.contains(&dep) {
771-
deps.push(dep);
772-
}
773-
}
759+
entry.insert(name_ref);
774760
}
775761
}
776762
}
777763

778764
for (definition_id, definition) in definitions {
779-
if let Some(name_id) = definition.name_id() {
780-
self.name_dependents
781-
.entry(*name_id)
782-
.or_default()
783-
.push(NameDependent::Definition(definition_id));
784-
}
785-
786765
if self.definitions.insert(definition_id, definition).is_some() {
787766
debug_assert!(false, "DefinitionId collision in global graph");
788767
}
@@ -791,11 +770,6 @@ impl Graph {
791770
}
792771

793772
for (constant_ref_id, constant_ref) in constant_references {
794-
self.name_dependents
795-
.entry(*constant_ref.name_id())
796-
.or_default()
797-
.push(NameDependent::Reference(constant_ref_id));
798-
799773
work.push(Unit::ConstantRef(constant_ref_id));
800774

801775
if self.constant_references.insert(constant_ref_id, constant_ref).is_some() {
@@ -808,6 +782,15 @@ impl Graph {
808782
debug_assert!(false, "Method ReferenceId collision in global graph");
809783
}
810784
}
785+
786+
for (name_id, deps) in name_dependents {
787+
let global_deps = self.name_dependents.entry(name_id).or_default();
788+
for dep in deps {
789+
if !global_deps.contains(&dep) {
790+
global_deps.push(dep);
791+
}
792+
}
793+
}
811794
}
812795

813796
/// Updates the global representation with the information contained in `other`, handling deletions, insertions and

0 commit comments

Comments
 (0)