Skip to content

Commit

Permalink
Skip extra settings resolution when namespace packages are empty (#9446)
Browse files Browse the repository at this point in the history
Saves 2% on Airflow:

```shell
❯ hyperfine --warmup 20 -i "./target/release/main format ../airflow" "./target/release/ruff format ../airflow"
Benchmark 1: ./target/release/main format ../airflow
  Time (mean ± σ):      72.7 ms ±   0.4 ms    [User: 48.7 ms, System: 75.5 ms]
  Range (min … max):    72.0 ms …  73.7 ms    40 runs

Benchmark 2: ./target/release/ruff format ../airflow
  Time (mean ± σ):      71.4 ms ±   0.6 ms    [User: 46.2 ms, System: 76.2 ms]
  Range (min … max):    70.3 ms …  73.8 ms    41 runs

Summary
  './target/release/ruff format ../airflow' ran
    1.02 ± 0.01 times faster than './target/release/main format ../airflow'
```
  • Loading branch information
charliermarsh committed Jan 9, 2024
1 parent 20af5a7 commit 381811b
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions crates/ruff_workspace/src/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,21 +139,35 @@ impl Resolver {
}
}

// Determine whether any of the settings require namespace packages. If not, we can save
// a lookup for every file.
let has_namespace_packages = self
.settings
.values()
.any(|settings| !settings.linter.namespace_packages.is_empty());

// Search for the package root for each file.
let mut package_roots: FxHashMap<&Path, Option<&Path>> = FxHashMap::default();
for file in files {
let namespace_packages = &self
.resolve(file, pyproject_config)
.linter
.namespace_packages;
if let Some(package) = file.parent() {
if package_roots.contains_key(package) {
continue;
match package_roots.entry(package) {
std::collections::hash_map::Entry::Occupied(_) => continue,
std::collections::hash_map::Entry::Vacant(entry) => {
let namespace_packages = if has_namespace_packages {
self.resolve(file, pyproject_config)
.linter
.namespace_packages
.as_slice()
} else {
&[]
};
entry.insert(detect_package_root_with_cache(
package,
namespace_packages,
&mut package_cache,
));
}
}
package_roots.insert(
package,
detect_package_root_with_cache(package, namespace_packages, &mut package_cache),
);
}
}

Expand Down

0 comments on commit 381811b

Please sign in to comment.