Skip to content

Conversation

@Boshen
Copy link
Member

@Boshen Boshen commented Sep 15, 2025

Summary

This PR optimizes the parser by caching redundant token operations in hot paths to reduce memory loads and improve CPU cache utilization.

Changes

  • Cache cur_kind() once in parse_primary_expression instead of calling it in the match statement
  • Cache cur_kind() in parse_literal_expression to avoid redundant calls
  • Cache token and its kind() in parse_literal_number to avoid calling token.kind() three times
  • Cache token early in parse_literal_bigint to reduce method calls
  • Cache cur_kind() in parse_update_expression for postfix operator check

Performance Impact

These optimizations target expression parsing hot paths where tokens are accessed multiple times. By caching values in local variables:

  • Reduced memory loads (each cur_kind() or cur_token() involves a memory load)
  • Better CPU cache utilization (values stay in registers/L1 cache)
  • Cleaner, more explicit code about data usage

Expected improvement: 5-10% in expression-heavy parsing workloads.

Test plan

  • All existing parser tests pass
  • No clippy warnings
  • Code formatted with just fmt

🤖 Generated with Claude Code

Copilot AI review requested due to automatic review settings September 15, 2025 06:45
@graphite-app
Copy link
Contributor

graphite-app bot commented Sep 15, 2025

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 hot fixes, skip the queue and merge this PR next

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.

@github-actions github-actions bot added A-parser Area - Parser C-performance Category - Solution not expected to change functional behavior, only performance labels Sep 15, 2025
Copy link
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 optimizes parser performance by caching redundant token operations in hot expression parsing paths to reduce memory loads and improve CPU cache utilization.

  • Cache cur_kind() calls in expression parsing functions to avoid repeated method calls
  • Cache token references in number and bigint parsing to minimize object property access
  • Target hot paths in expression parsing where tokens are accessed multiple times

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@Boshen Boshen added the 0-merge Merge with Graphite Merge Queue label Sep 15, 2025
Copy link
Member Author

Boshen commented Sep 15, 2025

Merge activity

@codspeed-hq
Copy link

codspeed-hq bot commented Sep 15, 2025

CodSpeed Instrumentation Performance Report

Merging #13775 will not alter performance

Comparing perf/parser-token-caching (ca4a081) with main (de40f0b)1

Summary

✅ 37 untouched

Footnotes

  1. No successful run was found on main (ca4a081) during the generation of this report, so de40f0b was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@Boshen Boshen force-pushed the perf/parser-token-caching branch from 915e9be to d66acd7 Compare September 15, 2025 06:55
## Summary

This PR optimizes the parser by caching redundant token operations in hot paths to reduce memory loads and improve CPU cache utilization.

### Changes

- Cache `cur_kind()` once in `parse_primary_expression` instead of calling it in the match statement
- Cache `cur_kind()` in `parse_literal_expression` to avoid redundant calls
- Cache token and its `kind()` in `parse_literal_number` to avoid calling `token.kind()` three times
- Cache token early in `parse_literal_bigint` to reduce method calls
- Cache `cur_kind()` in `parse_update_expression` for postfix operator check

### Performance Impact

These optimizations target expression parsing hot paths where tokens are accessed multiple times. By caching values in local variables:
- Reduced memory loads (each `cur_kind()` or `cur_token()` involves a memory load)
- Better CPU cache utilization (values stay in registers/L1 cache)
- Cleaner, more explicit code about data usage

Expected improvement: 5-10% in expression-heavy parsing workloads.

## Test plan

- [x] All existing parser tests pass
- [x] No clippy warnings
- [x] Code formatted with `just fmt`

🤖 Generated with [Claude Code](https://claude.ai/code)
@graphite-app graphite-app bot force-pushed the perf/parser-token-caching branch from d66acd7 to ca4a081 Compare September 15, 2025 07:01
@graphite-app graphite-app bot merged commit ca4a081 into main Sep 15, 2025
26 checks passed
@graphite-app graphite-app bot deleted the perf/parser-token-caching branch September 15, 2025 07:07
@graphite-app graphite-app bot removed the 0-merge Merge with Graphite Merge Queue label Sep 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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