mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-03 18:28:24 +00:00
Reuse BTreeSets
in module resolver (#13440)
## Summary For dependencies, there's no reason to re-allocate here, since we know the paths are unique.
This commit is contained in:
parent
17c4690b5e
commit
c2a5179d75
2 changed files with 15 additions and 18 deletions
|
@ -94,14 +94,21 @@ impl ModuleImports {
|
|||
pub struct ImportMap(BTreeMap<SystemPathBuf, ModuleImports>);
|
||||
|
||||
impl ImportMap {
|
||||
/// Insert a module's imports into the map.
|
||||
pub fn insert(&mut self, path: SystemPathBuf, imports: ModuleImports) {
|
||||
self.0.insert(path, imports);
|
||||
/// Create an [`ImportMap`] of file to its dependencies.
|
||||
///
|
||||
/// Assumes that the input is a collection of unique file paths and their imports.
|
||||
pub fn dependencies(imports: impl IntoIterator<Item = (SystemPathBuf, ModuleImports)>) -> Self {
|
||||
let mut map = ImportMap::default();
|
||||
for (path, imports) in imports {
|
||||
map.0.insert(path, imports);
|
||||
}
|
||||
map
|
||||
}
|
||||
|
||||
/// Reverse the [`ImportMap`], e.g., to convert from dependencies to dependents.
|
||||
#[must_use]
|
||||
pub fn reverse(imports: impl IntoIterator<Item = (SystemPathBuf, ModuleImports)>) -> Self {
|
||||
/// Create an [`ImportMap`] of file to its dependents.
|
||||
///
|
||||
/// Assumes that the input is a collection of unique file paths and their imports.
|
||||
pub fn dependents(imports: impl IntoIterator<Item = (SystemPathBuf, ModuleImports)>) -> Self {
|
||||
let mut reverse = ImportMap::default();
|
||||
for (path, imports) in imports {
|
||||
for import in imports.0 {
|
||||
|
@ -112,13 +119,3 @@ impl ImportMap {
|
|||
reverse
|
||||
}
|
||||
}
|
||||
|
||||
impl FromIterator<(SystemPathBuf, ModuleImports)> for ImportMap {
|
||||
fn from_iter<I: IntoIterator<Item = (SystemPathBuf, ModuleImports)>>(iter: I) -> Self {
|
||||
let mut map = ImportMap::default();
|
||||
for (path, imports) in iter {
|
||||
map.0.entry(path).or_default().0.extend(imports.0);
|
||||
}
|
||||
map
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue