Skip to content

perf(ast_tools): introduce Structs iterator and use it where possible#20943

Merged
graphite-app[bot] merged 1 commit intomainfrom
om/04-01-perf_ast_tools_introduce_structs_iterator_and_use_it_where_possible
Apr 1, 2026
Merged

perf(ast_tools): introduce Structs iterator and use it where possible#20943
graphite-app[bot] merged 1 commit intomainfrom
om/04-01-perf_ast_tools_introduce_structs_iterator_and_use_it_where_possible

Conversation

@overlookmotel
Copy link
Copy Markdown
Member

@overlookmotel overlookmotel commented Apr 1, 2026

Many generators in ast_tools loop over all structs in the AST. Previously they iterated over all types in the schema, and skipped types which aren't structs. This is inefficient, as all structs and enums are at the start of the types Vec. Once the loop finds any other type (e.g. Option, Box, primitive), it's not possible for there to be further structs after it.

Introduce a Structs iterator which iterates only over structs, and terminates iteration early as soon as a type which isn't a struct or enum is found. This shortens code in many places, and reduces pointless iteration.

Copy link
Copy Markdown
Member Author

overlookmotel commented Apr 1, 2026


How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • 0-merge - adds this PR to the back of the merge queue
  • hotfix - for urgent changes, fast-track this PR to the front of the merge queue

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

This stack of pull requests is managed by Graphite. Learn more about stacking.

@github-actions github-actions Bot added A-ast-tools Area - AST tools C-performance Category - Solution not expected to change functional behavior, only performance labels Apr 1, 2026
@overlookmotel overlookmotel marked this pull request as ready for review April 1, 2026 18:05
Copilot AI review requested due to automatic review settings April 1, 2026 18:05
@overlookmotel overlookmotel self-assigned this Apr 1, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces a dedicated Schema::structs() iterator to efficiently iterate only over struct type definitions, terminating early once schema types moves past the initial structs/enums segment. It then updates several generators to use this iterator instead of scanning all schema types and filtering.

Changes:

  • Add a Structs iterator and Schema::structs() accessor in schema/mod.rs.
  • Replace schema.types full scans + struct filtering with schema.structs() in multiple generators.
  • Preserve existing semantics while reducing unnecessary iteration over non-struct schema types.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.

Show a summary per file
File Description
tasks/ast_tools/src/schema/mod.rs Adds Schema::structs() and the Structs iterator with early termination behavior.
tasks/ast_tools/src/generators/utf8_to_utf16.rs Uses schema.structs() to generate visitor methods only from struct defs.
tasks/ast_tools/src/generators/traverse/ancestor.rs Iterates visited AST structs via schema.structs() instead of scanning all types.
tasks/ast_tools/src/generators/raw_transfer_lazy.rs Uses schema.structs() when computing cache key offsets for relevant structs.
tasks/ast_tools/src/generators/ast_kind.rs Uses schema.structs() when generating AstKind variants/methods.
tasks/ast_tools/src/generators/assert_layouts.rs Uses schema.structs() when generating struct field-order details.

@graphite-app graphite-app Bot added the 0-merge Merge with Graphite Merge Queue label Apr 1, 2026
@graphite-app
Copy link
Copy Markdown
Contributor

graphite-app Bot commented Apr 1, 2026

Merge activity

…le (#20943)

Many generators in `ast_tools` loop over all structs in the AST. Previously they iterated over all types in the schema, and skipped types which aren't structs. This is inefficient, as all structs and enums are at the start of the `types` `Vec`. Once the loop finds any other type (e.g. `Option`, `Box`, primitive), it's not possible for there to be further structs after it.

Introduce a `Structs` iterator which iterates only over structs, and terminates iteration early as soon as a type which isn't a struct or enum is found. This shortens code in many places, and reduces pointless iteration.
@graphite-app graphite-app Bot force-pushed the om/04-01-refactor_ast_tools_simplify_astkind_generator branch from 7463340 to da91ccc Compare April 1, 2026 18:14
@graphite-app graphite-app Bot force-pushed the om/04-01-perf_ast_tools_introduce_structs_iterator_and_use_it_where_possible branch from aa93f79 to 91098c4 Compare April 1, 2026 18:14
@graphite-app graphite-app Bot removed the 0-merge Merge with Graphite Merge Queue label Apr 1, 2026
Base automatically changed from om/04-01-refactor_ast_tools_simplify_astkind_generator to main April 1, 2026 18:18
@graphite-app graphite-app Bot merged commit 91098c4 into main Apr 1, 2026
25 checks passed
@graphite-app graphite-app Bot deleted the om/04-01-perf_ast_tools_introduce_structs_iterator_and_use_it_where_possible branch April 1, 2026 18:21
graphite-app Bot pushed a commit that referenced this pull request Apr 1, 2026
…sible (#20949)

Same as #20943. Introduce a `StructsMut` iterator, which is more efficient than iterating through all types.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-ast-tools Area - AST tools C-performance Category - Solution not expected to change functional behavior, only performance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants