mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-18 17:41:12 +00:00
Skip extra settings resolution when namespace packages are empty (#9446)
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' ```
This commit is contained in:
parent
20af5a774f
commit
381811b4a6
1 changed files with 24 additions and 10 deletions
|
@ -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.
|
// Search for the package root for each file.
|
||||||
let mut package_roots: FxHashMap<&Path, Option<&Path>> = FxHashMap::default();
|
let mut package_roots: FxHashMap<&Path, Option<&Path>> = FxHashMap::default();
|
||||||
for file in files {
|
for file in files {
|
||||||
let namespace_packages = &self
|
|
||||||
.resolve(file, pyproject_config)
|
|
||||||
.linter
|
|
||||||
.namespace_packages;
|
|
||||||
if let Some(package) = file.parent() {
|
if let Some(package) = file.parent() {
|
||||||
if package_roots.contains_key(package) {
|
match package_roots.entry(package) {
|
||||||
continue;
|
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),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue