Skip to content

Share postfix parsing and reduce expression copies - #29480

Merged
charliermarsh merged 3 commits into
mainfrom
charlie/ruff-shared-postfix-atom
Oct 19, 2026
Merged

charliermarsh merged 3 commits into
mainfrom
charlie/ruff-shared-postfix-atom

Conversation

@charliermarsh

@charliermarsh charliermarsh commented Oct 19, 2026 •

Copy link
Copy Markdown
Member

Summary

We skip prefix-operator dispatch for identifiers and parse atoms together with their postfix chains. Keeping this work in parse_primary_expression makes the grouping-parenthesis flag local and avoids passing an intermediate ParsedExpr between atom and postfix parsing.

@charliermarsh charliermarsh added performance Potential performance improvement parser Related to the parser labels Oct 19, 2026
@astral-sh-bot

astral-sh-bot Bot commented Oct 19, 2026

Copy link
Copy Markdown

Memory usage report

Memory usage unchanged ✅

@astral-sh-bot

astral-sh-bot Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

ecosystem-analyzer results

No diagnostic changes detected ✅

Full report with detailed diff (timing results)

@astral-sh-bot

astral-sh-bot Bot commented Oct 19, 2026

Copy link
Copy Markdown

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

@codspeed

codspeed Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

Merging this PR will improve performance by 6.32%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 4 improved benchmarks
✅ 240 untouched benchmarks
⏩ 10 skipped benchmarks1

Performance Changes

Mode Benchmark BASE HEAD Efficiency
⚡ Simulation parser[large/dataset.py] 6.8 ms 6.3 ms +7.78%
⚡ Simulation parser[pydantic/types.py] 3 ms 2.8 ms +6.32%
⚡ Simulation parser[numpy/ctypeslib.py] 1.3 ms 1.3 ms +5.97%
⚡ Simulation parser[unicode/pypinyin.py] 516.5 µs 490.7 µs +5.24%

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing charlie/ruff-shared-postfix-atom (7b27b5d) with main (0a977a8)

Open in CodSpeed

Footnotes

  1. 10 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. ↩

@charliermarsh
charliermarsh force-pushed the charlie/ruff-shared-postfix-atom branch from cd6f40b to 9743630 Compare October 19, 2026 15:11
@charliermarsh
charliermarsh added this pull request to stack #29499 October 19, 2026 17:56
///
/// See: <https://docs.python.org/3/reference/expressions.html#atoms>
fn parse_atom(&mut self, context: ExpressionContext) -> ParsedExpr {
fn parse_atom(&mut self, context: ExpressionContext, is_parenthesized: &mut bool) -> Expr {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I always find &mut arguments hard to understand and try to avoid them if i can. But it's probably fine, given that this is local.

An alternative is to widen parse_atom and make it into parse_primary_expression (primary expression is a Python grammar). This way, is_parenthesized could remain entirely local to parse_primary_expression.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done!

## Summary

We parse atoms and their postfix chains together in `parse_primary_expression`, keeping grouping-parenthesis metadata local and removing the `&mut bool` output parameter.

Implements [Micha's suggestion](#29480 (comment)) on top of #29480 so CodSpeed compares the two implementations directly. [CodSpeed reports no performance changes](https://github.com/astral-sh/ruff/pull/29507/checks?check_run_id=118282844884): 244 benchmarks unchanged and 10 skipped.
@charliermarsh
charliermarsh merged commit 85430a9 into main Oct 19, 2026
74 checks passed
@charliermarsh
charliermarsh deleted the charlie/ruff-shared-postfix-atom branch October 19, 2026 20:35

This branch was successfully deployed

1 active deployment
automations — 7b27b5d4 Deployed Oct 19, 2026 by charliermarsh via security-review / security review #92245
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

parser Related to the parser performance Potential performance improvement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants