mirror of
https://github.com/astral-sh/ruff.git
synced 2026-11-21 12:16:15 +08:00
## Summary Ruff uses `ComparableExpr` to compare Python syntax while ignoring details such as source ranges and expression contexts. Previously, `ComparableExpr::from(&expr)` recursively built a second tree containing the fields used for comparison. For `factory(item, *items, limit=2, **options)`, it boxed the comparable callee and the operand of `*items`, and collected the positional arguments and keywords into two vectors. Equality and hashing then walked that tree. Statements, match patterns, and modules built similar trees. `ComparableExpr<'a>` now holds a single `ast::ExprRef<'a>`. The statement, pattern, and module keys likewise borrow their AST nodes. Constructing a key takes constant time and allocates nothing. When comparing two calls, equality checks the callees first. If one is `factory` and the other is `other`, it can return before visiting any arguments. If the callees match, it walks the original argument slices, comparing each child as needed. Hashing walks the same structural fields, and `Debug` displays them. Borrowing the expression also removes the need to run `drop` after comparing. The field helpers exhaustively destructure each node so that adding an AST field requires deciding whether it participates in comparison. | Workload | `main` | This PR | | --- | ---: | ---: | | Size of a `ComparableExpr` on x86-64 | 128 bytes | 16 bytes | | Construct the call key | 4 allocations, 800 bytes | 0 allocations | | Construct the key for an `if` statement containing the call | 8 allocations, 1,864 bytes | 0 allocations | | Construct two equal call keys and compare them | 2,745 instructions | 731 instructions | | Construct one call key and hash it | 1,621 instructions | 589 instructions | Codspeed reports a 3% improvement on most linter benchmarks. ## Considerations Repeatedly comparing or hashing the same key now becomes slightly more expensive because the implementation repeats the normalization in each hash and equal call. However, the most common case is that we compare nodes that are unequal, where the early return outweights building the full normalized tree. If hashing becomes a problem, we should cache the hash key (ideally only at call sites where it matters because this increases instruction counts again). ## Test Plan Structural comparison and formatter tests passed, and all five lint fixtures produced identical diagnostics on both builds.
48 lines
2.1 KiB
TOML
48 lines
2.1 KiB
TOML
doc-valid-idents = [
|
|
"..",
|
|
"CodeQL",
|
|
"CPython",
|
|
"FastAPI",
|
|
"IPython",
|
|
"LangChain",
|
|
"LibCST",
|
|
"McCabe",
|
|
"NumPy",
|
|
"SCREAMING_SNAKE_CASE",
|
|
"SQLAlchemy",
|
|
"StackOverflow",
|
|
"PyCharm",
|
|
"SNMPv1",
|
|
"SNMPv2",
|
|
"SNMPv3",
|
|
"PyFlakes",
|
|
]
|
|
|
|
ignore-interior-mutability = [
|
|
# Interned is read-only. The wrapped `Rc` never gets updated.
|
|
"ruff_formatter::format_element::Interned",
|
|
# These keys borrow AST nodes with an AtomicNodeIndex, which comparison ignores.
|
|
# A concatenated string's OnceLock caches only its decoded text.
|
|
"ruff_python_ast::comparable::ComparableExpr",
|
|
"ruff_python_ast::comparable::ComparableLiteral",
|
|
# The expression is read-only.
|
|
"ruff_python_ast::hashable::HashableExpr",
|
|
]
|
|
|
|
disallowed-methods = [
|
|
{ path = "std::env::var", reason = "Use System::env_var instead in ty crates" },
|
|
{ path = "std::env::current_dir", reason = "Use System::current_directory instead in ty crates" },
|
|
{ path = "std::fs::read_to_string", reason = "Use System::read_to_string instead in ty crates" },
|
|
{ path = "std::fs::metadata", reason = "Use System::path_metadata instead in ty crates" },
|
|
{ path = "std::fs::canonicalize", reason = "Use System::canonicalize_path instead in ty crates" },
|
|
{ path = "dunce::canonicalize", reason = "Use System::canonicalize_path instead in ty crates" },
|
|
{ path = "std::fs::read_dir", reason = "Use System::read_directory instead in ty crates" },
|
|
{ path = "std::fs::write", reason = "Use WritableSystem::write_file instead in ty crates" },
|
|
{ path = "std::fs::create_dir_all", reason = "Use WritableSystem::create_directory_all instead in ty crates" },
|
|
{ path = "std::fs::File::create_new", reason = "Use WritableSystem::create_new_file instead in ty crates" },
|
|
# Path methods that have System trait equivalents
|
|
{ path = "std::path::Path::exists", reason = "Use System::path_exists instead in ty crates" },
|
|
{ path = "std::path::Path::is_dir", reason = "Use System::is_directory instead in ty crates" },
|
|
{ path = "std::path::Path::is_file", reason = "Use System::is_file instead in ty crates" },
|
|
]
|