ruff/crates/ruff_python_formatter/src/expression
Ibraheem Ahmed c9dff5c7d5
[ty] AST garbage collection (#18482)
## Summary

Garbage collect ASTs once we are done checking a given file. Queries
with a cross-file dependency on the AST will reparse the file on demand.
This reduces ty's peak memory usage by ~20-30%.

The primary change of this PR is adding a `node_index` field to every
AST node, that is assigned by the parser. `ParsedModule` can use this to
create a flat index of AST nodes any time the file is parsed (or
reparsed). This allows `AstNodeRef` to simply index into the current
instance of the `ParsedModule`, instead of storing a pointer directly.

The indices are somewhat hackily (using an atomic integer) assigned by
the `parsed_module` query instead of by the parser directly. Assigning
the indices in source-order in the (recursive) parser turns out to be
difficult, and collecting the nodes during semantic indexing is
impossible as `SemanticIndex` does not hold onto a specific
`ParsedModuleRef`, which the pointers in the flat AST are tied to. This
means that we have to do an extra AST traversal to assign and collect
the nodes into a flat index, but the small performance impact (~3% on
cold runs) seems worth it for the memory savings.

Part of https://github.com/astral-sh/ty/issues/214.
2025-06-13 08:40:11 -04:00
..
binary_like.rs Switch to Rust 2024 edition (#18129) 2025-05-16 13:25:28 +02:00
expr_attribute.rs [ty] AST garbage collection (#18482) 2025-06-13 08:40:11 -04:00
expr_await.rs [ty] AST garbage collection (#18482) 2025-06-13 08:40:11 -04:00
expr_bin_op.rs Fix f-string formatting in assignment statement (#14454) 2024-11-26 15:07:18 +05:30
expr_bool_op.rs Move {AnyNodeRef, AstNode} to ruff_python_ast crate root (#8030) 2023-10-18 00:01:18 +00:00
expr_boolean_literal.rs Split `Constant` to individual literal nodes (#8064) 2023-10-30 12:13:23 +05:30
expr_bytes_literal.rs Switch to Rust 2024 edition (#18129) 2025-05-16 13:25:28 +02:00
expr_call.rs [ty] AST garbage collection (#18482) 2025-06-13 08:40:11 -04:00
expr_compare.rs Fix f-string formatting in assignment statement (#14454) 2024-11-26 15:07:18 +05:30
expr_dict.rs [ty] AST garbage collection (#18482) 2025-06-13 08:40:11 -04:00
expr_dict_comp.rs [ty] AST garbage collection (#18482) 2025-06-13 08:40:11 -04:00
expr_ellipsis_literal.rs Split `Constant` to individual literal nodes (#8064) 2023-10-30 12:13:23 +05:30
expr_f_string.rs Implement template strings (#17851) 2025-05-30 15:00:56 -05:00
expr_generator.rs [ty] AST garbage collection (#18482) 2025-06-13 08:40:11 -04:00
expr_if.rs [ty] AST garbage collection (#18482) 2025-06-13 08:40:11 -04:00
expr_ipy_escape_command.rs Formatter parentheses support for `IpyEscapeCommand` (#8207) 2023-10-25 14:01:50 +00:00
expr_lambda.rs [ty] AST garbage collection (#18482) 2025-06-13 08:40:11 -04:00
expr_list.rs [ty] AST garbage collection (#18482) 2025-06-13 08:40:11 -04:00
expr_list_comp.rs [ty] AST garbage collection (#18482) 2025-06-13 08:40:11 -04:00
expr_name.rs [ty] AST garbage collection (#18482) 2025-06-13 08:40:11 -04:00
expr_named.rs [ty] AST garbage collection (#18482) 2025-06-13 08:40:11 -04:00
expr_none_literal.rs Split `Constant` to individual literal nodes (#8064) 2023-10-30 12:13:23 +05:30
expr_number_literal.rs Extract `LineIndex` independent methods from `Locator` (#13938) 2024-10-28 07:53:41 +00:00
expr_set.rs [ty] AST garbage collection (#18482) 2025-06-13 08:40:11 -04:00
expr_set_comp.rs [ty] AST garbage collection (#18482) 2025-06-13 08:40:11 -04:00
expr_slice.rs [ty] AST garbage collection (#18482) 2025-06-13 08:40:11 -04:00
expr_starred.rs [ty] AST garbage collection (#18482) 2025-06-13 08:40:11 -04:00
expr_string_literal.rs Switch to Rust 2024 edition (#18129) 2025-05-16 13:25:28 +02:00
expr_subscript.rs [ty] AST garbage collection (#18482) 2025-06-13 08:40:11 -04:00
expr_t_string.rs Implement template strings (#17851) 2025-05-30 15:00:56 -05:00
expr_tuple.rs [ty] AST garbage collection (#18482) 2025-06-13 08:40:11 -04:00
expr_unary_op.rs [ty] AST garbage collection (#18482) 2025-06-13 08:40:11 -04:00
expr_yield.rs Switch to Rust 2024 edition (#18129) 2025-05-16 13:25:28 +02:00
expr_yield_from.rs Move {AnyNodeRef, AstNode} to ruff_python_ast crate root (#8030) 2023-10-18 00:01:18 +00:00
mod.rs [ty] AST garbage collection (#18482) 2025-06-13 08:40:11 -04:00
operator.rs Split implicit concatenated strings before binary expressions (#7145) 2023-09-08 06:51:26 +00:00
parentheses.rs Switch to Rust 2024 edition (#18129) 2025-05-16 13:25:28 +02:00