perf(ast_tools): introduce Structs iterator and use it where possible#20943
Conversation
How to use the Graphite Merge QueueAdd either label to this PR to merge it via 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. |
There was a problem hiding this comment.
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
Structsiterator andSchema::structs()accessor inschema/mod.rs. - Replace
schema.typesfull scans + struct filtering withschema.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. |
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.
7463340 to
da91ccc
Compare
aa93f79 to
91098c4
Compare

Many generators in
ast_toolsloop 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 thetypesVec. 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
Structsiterator 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.