Skip to content

Speed up identifier and keyword lexing - #29463

Open
charliermarsh wants to merge 8 commits into
mainfrom
charlie/ruff-ascii-identifiers
Open

charliermarsh wants to merge 8 commits into
mainfrom
charlie/ruff-ascii-identifiers

Conversation

@charliermarsh

@charliermarsh charliermarsh commented Oct 18, 2026 •

Copy link
Copy Markdown
Member

Summary

We scan ASCII identifier continuations in sixteen-byte batches using a byte lookup table, avoiding per-character decoding. Keyword lookup filters by length and initial byte, then uses a compact hash table with a full-word comparison to reject collisions. Non-ASCII suffixes retain the existing Unicode path.

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

astral-sh-bot Bot commented Oct 18, 2026

Copy link
Copy Markdown

Memory usage report

Memory usage unchanged ✅

@astral-sh-bot

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

Copy link
Copy Markdown

ecosystem-analyzer results

No diagnostic changes detected ✅

Full report with detailed diff (timing results)

@charliermarsh charliermarsh changed the title Scan ASCII identifiers with byte lookups Batch ASCII identifier scanning Oct 18, 2026
@astral-sh-bot

astral-sh-bot Bot commented Oct 18, 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 4.7%

⚡ 3 improved benchmarks
✅ 241 untouched benchmarks
⏩ 10 skipped benchmarks1

Performance Changes

Mode Benchmark BASE HEAD Efficiency
⚡ Simulation lexer[pydantic/types.py] 682.8 µs 650 µs +5.04%
⚡ Simulation lexer[numpy/globals.py] 41.6 µs 39.6 µs +4.94%
⚡ Simulation lexer[numpy/ctypeslib.py] 302.1 µs 290.1 µs +4.13%

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-ascii-identifiers (5410284) with main (34266aa)2

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

  2. No successful run was found on main (355cda3) during the generation of this report, so 34266aa was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

@charliermarsh charliermarsh changed the title Batch ASCII identifier scanning Speed up identifier and keyword lexing Oct 19, 2026
@charliermarsh
charliermarsh changed the base branch from main to charlie/ruff-shared-postfix-atom October 19, 2026 14:02
@charliermarsh
charliermarsh force-pushed the charlie/ruff-ascii-identifiers branch from a1177bd to f91d049 Compare October 19, 2026 14:02
@charliermarsh
charliermarsh force-pushed the charlie/ruff-ascii-identifiers branch from ea02032 to d47bd15 Compare October 19, 2026 15:11
@charliermarsh
charliermarsh added this pull request to stack #29499 October 19, 2026 17:56
Base automatically changed from charlie/ruff-shared-postfix-atom to main October 19, 2026 20:35
@charliermarsh
charliermarsh force-pushed the charlie/ruff-ascii-identifiers branch from d47bd15 to 03bf69f Compare October 19, 2026 20:35
@charliermarsh
charliermarsh marked this pull request as ready for review October 19, 2026 21:05

@MichaReiser MichaReiser left a comment •

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 haven't looked at this in detail but it feels tricky to maintain (at least as it is now). I think we need some more documentation with guidance on how it works and what needs changing and how when adding/removing/changing keywords). Better isolation by e.g. encapsulating some into its own function might also help

Comment thread crates/ruff_python_parser/src/lexer.rs Outdated
Comment thread crates/ruff_python_parser/src/lexer.rs Outdated
Comment thread crates/ruff_python_parser/src/lexer.rs Outdated
Comment thread crates/ruff_python_parser/src/lexer.rs Outdated
Comment thread crates/ruff_python_parser/src/lexer.rs Outdated
Comment thread crates/ruff_python_parser/src/lexer.rs Outdated
@MichaReiser

Copy link
Copy Markdown
Member

why?

@charliermarsh

Copy link
Copy Markdown
Member Author

I got the vibe from your comments that you didn't want to proceed with it haha! Do you think it's salvageable? I'm happy to clean it up, I just thought you were left feeling like the tradeoffs weren't right.

@MichaReiser

MichaReiser commented Oct 19, 2026 •

Copy link
Copy Markdown
Member

I think it's a worthwhile change. I just think it requires some more explanation of what happens here and how to update that weird constant when adding/removing kewyords 😆

@charliermarsh

Copy link
Copy Markdown
Member Author

Haha ok. I will clean it up. I'll ask Codex to make it even weirder and more confusing.

This branch was previously deployed

1 inactive deployment
automations — 54102846 Deployed Oct 20, 2026 by charliermarsh via security-review / security review #92296
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