Skip to content

Commit 66494eb

Browse files
committed
Reuse BTreeSets in module resolver
1 parent 17c4690 commit 66494eb

File tree

2 files changed

+16
-19
lines changed

2 files changed

+16
-19
lines changed

crates/ruff/src/commands/analyze_graph.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,8 @@ pub(crate) fn analyze_graph(
169169

170170
// Generate the import map.
171171
let import_map = match args.direction {
172-
Direction::Dependencies => ImportMap::from_iter(imports),
173-
Direction::Dependents => ImportMap::reverse(imports),
172+
Direction::Dependencies => ImportMap::dependencies(imports),
173+
Direction::Dependents => ImportMap::dependents(imports),
174174
};
175175

176176
// Print to JSON.

crates/ruff_graph/src/lib.rs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -91,17 +91,24 @@ impl ModuleImports {
9191

9292
#[derive(Debug, Default)]
9393
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
94-
pub struct ImportMap(BTreeMap<SystemPathBuf, ModuleImports>);
94+
pub struct ImportMap(BTreeMap<SystemPathBuf, BTreeSet<SystemPathBuf>>);
9595

9696
impl ImportMap {
97-
/// Insert a module's imports into the map.
98-
pub fn insert(&mut self, path: SystemPathBuf, imports: ModuleImports) {
99-
self.0.insert(path, imports);
97+
/// Create an [`ImportMap`] of file to its dependencies.
98+
///
99+
/// Assumes that the input is a collection of unique file paths and their imports.
100+
pub fn dependencies(imports: impl IntoIterator<Item = (SystemPathBuf, ModuleImports)>) -> Self {
101+
let mut map = ImportMap::default();
102+
for (path, imports) in imports {
103+
map.0.insert(path, imports.0);
104+
}
105+
map
100106
}
101107

102-
/// Reverse the [`ImportMap`], e.g., to convert from dependencies to dependents.
103-
#[must_use]
104-
pub fn reverse(imports: impl IntoIterator<Item = (SystemPathBuf, ModuleImports)>) -> Self {
108+
/// Create an [`ImportMap`] of file to its dependents.
109+
///
110+
/// Assumes that the input is a collection of unique file paths and their imports.
111+
pub fn dependents(imports: impl IntoIterator<Item = (SystemPathBuf, ModuleImports)>) -> Self {
105112
let mut reverse = ImportMap::default();
106113
for (path, imports) in imports {
107114
for import in imports.0 {
@@ -112,13 +119,3 @@ impl ImportMap {
112119
reverse
113120
}
114121
}
115-
116-
impl FromIterator<(SystemPathBuf, ModuleImports)> for ImportMap {
117-
fn from_iter<I: IntoIterator<Item = (SystemPathBuf, ModuleImports)>>(iter: I) -> Self {
118-
let mut map = ImportMap::default();
119-
for (path, imports) in iter {
120-
map.0.entry(path).or_default().0.extend(imports.0);
121-
}
122-
map
123-
}
124-
}

0 commit comments

Comments
 (0)