Skip to content

perf(ast): place NodeId field after Span in structs#20584

Merged
graphite-app[bot] merged 1 commit intomainfrom
om/02-15-perf_ast_place_nodeid_field_after_span_in_structs
Mar 20, 2026
Merged

perf(ast): place NodeId field after Span in structs#20584
graphite-app[bot] merged 1 commit intomainfrom
om/02-15-perf_ast_place_nodeid_field_after_span_in_structs

Conversation

@overlookmotel
Copy link
Copy Markdown
Member

@overlookmotel overlookmotel commented Mar 20, 2026

Alter the code in ast_tools which re-orders struct fields, to make NodeId always occupy a consistent "slot" in every AST node - at byte position 8, just after Span.

This will be a sizeable gain once we start utilizing the NodeId fields more. Because, for example, every variant of Expression has its NodeId stored in same location as all the other variants, Expression::node_id() is just 1 operation - a pointer read - rather than a nest of branches, or a lookup table.

Also alter the algorithm for ordering struct fields to fill in the 4-byte gap after NodeId field with other field(s), to avoid excess padding.

The new algorithm also prioritizes keeping fields in definition order as much as possible, rather than sorting them strictly in order of size and alignment. This is mildly advantageous because field definition order is the order the AST is walked in, so it avoids bouncing between cache lines while iterating through the fields of a struct when visiting the AST.

No types change size in the process. Fields remain packed to keep type sizes the minimum they can be - they just change order.

Copy link
Copy Markdown
Member Author

overlookmotel commented Mar 20, 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.

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq bot commented Mar 20, 2026

Merging this PR will not alter performance

✅ 53 untouched benchmarks
⏩ 3 skipped benchmarks1


Comparing om/02-15-perf_ast_place_nodeid_field_after_span_in_structs (2064ade) with om/02-15-refactor_ast_re-order_layout_assertions_in_memory_order (2dfbe32)2

Open in CodSpeed

Footnotes

  1. 3 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

  2. No successful run was found on om/02-15-refactor_ast_re-order_layout_assertions_in_memory_order (cc4867c) during the generation of this report, so 9405508 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@overlookmotel overlookmotel marked this pull request as ready for review March 20, 2026 21:28
@overlookmotel overlookmotel requested a review from camc314 as a code owner March 20, 2026 21:28
Copilot AI review requested due to automatic review settings March 20, 2026 21:28
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.

Copilot wasn't able to review any files in this pull request.

@overlookmotel overlookmotel self-assigned this Mar 20, 2026
@overlookmotel overlookmotel requested a review from Copilot March 20, 2026 21:54
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.

Copilot wasn't able to review any files in this pull request.

@overlookmotel overlookmotel force-pushed the om/02-15-refactor_ast_re-order_layout_assertions_in_memory_order branch from 2dfbe32 to cc4867c Compare March 20, 2026 22:02
@overlookmotel overlookmotel force-pushed the om/02-15-perf_ast_place_nodeid_field_after_span_in_structs branch from b258884 to 2064ade Compare March 20, 2026 22:02
@graphite-app graphite-app bot added the 0-merge Merge with Graphite Merge Queue label Mar 20, 2026
@graphite-app
Copy link
Copy Markdown
Contributor

graphite-app bot commented Mar 20, 2026

Merge activity

Alter the code in `ast_tools` which re-orders struct fields, to make `NodeId` always occupy a consistent "slot" in every AST node - at byte position 8, just after `Span`.

This will be a sizeable gain once we start utilizing the `NodeId` fields more. Because, for example, every variant of `Expression` has its `NodeId` stored in same location as all the other variants, `Expression::node_id()` is just 1 operation - a pointer read - rather than a nest of branches, or a lookup table.

Also alter the algorithm for ordering struct fields to fill in the 4-byte gap after `NodeId` field with other field(s), to avoid excess padding.

The new algorithm also prioritizes keeping fields in definition order as much as possible, rather than sorting them strictly in order of size and alignment. This is mildly advantageous because field definition order is the order the AST is walked in, so it avoids bouncing between cache lines while iterating through the fields of a struct when visiting the AST.

No types change size in the process. Fields remain packed to keep type sizes the minimum they can be - they just change order.
graphite-app bot pushed a commit that referenced this pull request Mar 20, 2026
…ods (#20582)

Pure refactor.

Memory layout calculation was implemented as a series of free functions. Convert them to a `struct` with methods, so they can pass data between them in `self` rather than many function params.

This prepares the way for the changes in #20584.
@graphite-app graphite-app bot force-pushed the om/02-15-refactor_ast_re-order_layout_assertions_in_memory_order branch from cc4867c to e984666 Compare March 20, 2026 22:19
@graphite-app graphite-app bot requested a review from leaysgur as a code owner March 20, 2026 22:19
@graphite-app graphite-app bot force-pushed the om/02-15-perf_ast_place_nodeid_field_after_span_in_structs branch from 2064ade to c6ea0a0 Compare March 20, 2026 22:20
graphite-app bot pushed a commit that referenced this pull request Mar 20, 2026
Now that `AstKind`'s variants are all structs (no enums any more), and #20584 has ensured that `Span` is in a consistent position in all structs, `AstKind::span` should boil down to a single operation - a pointer read. Therefore, mark it `#[inline]`.

Note: `Span` was already in a consistent position prior to #20584, but that was more by accident than anything else. Now it's guaranteed. But this change won't make a difference to perf, it just guards against a perf regression in future.
graphite-app bot pushed a commit that referenced this pull request Mar 20, 2026
Follow-on after #20584. Simplify implementation of struct field ordering in `ast_tools`. This change does not alter any layouts, just shortens the code by taking a simpler approach to handling ZST fields.
@graphite-app graphite-app bot removed the 0-merge Merge with Graphite Merge Queue label Mar 20, 2026
Base automatically changed from om/02-15-refactor_ast_re-order_layout_assertions_in_memory_order to main March 20, 2026 22:28
@graphite-app graphite-app bot merged commit c6ea0a0 into main Mar 20, 2026
26 checks passed
@graphite-app graphite-app bot deleted the om/02-15-perf_ast_place_nodeid_field_after_span_in_structs branch March 20, 2026 22:29
costajohnt pushed a commit to costajohnt/oxc that referenced this pull request Mar 22, 2026
…ods (oxc-project#20582)

Pure refactor.

Memory layout calculation was implemented as a series of free functions. Convert them to a `struct` with methods, so they can pass data between them in `self` rather than many function params.

This prepares the way for the changes in oxc-project#20584.
costajohnt pushed a commit to costajohnt/oxc that referenced this pull request Mar 22, 2026
…20584)

Alter the code in `ast_tools` which re-orders struct fields, to make `NodeId` always occupy a consistent "slot" in every AST node - at byte position 8, just after `Span`.

This will be a sizeable gain once we start utilizing the `NodeId` fields more. Because, for example, every variant of `Expression` has its `NodeId` stored in same location as all the other variants, `Expression::node_id()` is just 1 operation - a pointer read - rather than a nest of branches, or a lookup table.

Also alter the algorithm for ordering struct fields to fill in the 4-byte gap after `NodeId` field with other field(s), to avoid excess padding.

The new algorithm also prioritizes keeping fields in definition order as much as possible, rather than sorting them strictly in order of size and alignment. This is mildly advantageous because field definition order is the order the AST is walked in, so it avoids bouncing between cache lines while iterating through the fields of a struct when visiting the AST.

No types change size in the process. Fields remain packed to keep type sizes the minimum they can be - they just change order.
costajohnt pushed a commit to costajohnt/oxc that referenced this pull request Mar 22, 2026
Now that `AstKind`'s variants are all structs (no enums any more), and oxc-project#20584 has ensured that `Span` is in a consistent position in all structs, `AstKind::span` should boil down to a single operation - a pointer read. Therefore, mark it `#[inline]`.

Note: `Span` was already in a consistent position prior to oxc-project#20584, but that was more by accident than anything else. Now it's guaranteed. But this change won't make a difference to perf, it just guards against a perf regression in future.
costajohnt pushed a commit to costajohnt/oxc that referenced this pull request Mar 22, 2026
)

Follow-on after oxc-project#20584. Simplify implementation of struct field ordering in `ast_tools`. This change does not alter any layouts, just shortens the code by taking a simpler approach to handling ZST fields.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-ast Area - AST A-ast-tools Area - AST tools A-cli Area - CLI A-linter Area - Linter A-linter-plugins Area - Linter JS plugins A-parser Area - Parser 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