Skip to content

Avoid redundant TokenValue drops in the lexer#25300

Merged
charliermarsh merged 1 commit into
mainfrom
charlie/codspeed
May 21, 2026
Merged

Avoid redundant TokenValue drops in the lexer#25300
charliermarsh merged 1 commit into
mainfrom
charlie/codspeed

Conversation

@charliermarsh

@charliermarsh charliermarsh commented May 21, 2026

Copy link
Copy Markdown
Member

Summary

If we avoid resetting the lexer value slot when it's already empty, this apparently improves performance by 1-2%. (In optimized builds, this keeps the common case to a discriminant branch instead of re-entering the TokenValue drop path.)

@astral-sh-bot

astral-sh-bot Bot commented May 21, 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.

@charliermarsh

Copy link
Copy Markdown
Member Author

Bizarrely, this does show a consistent 1-2% speed-up in the parser and lexer.

@MichaReiser

Copy link
Copy Markdown
Member

This is probably an instance where it's worth looking at the release assembly

@charliermarsh

Copy link
Copy Markdown
Member Author

Yeah, this branch includes an extra:

ldrb    w8, [x21, #-48]!
cbz     w8, LBB48_3

Which lets us skip the drop, which is apparently not zero-cost even when the variant is in fact None, since it's the drop impl for an arbitrary TokenValue.

@charliermarsh charliermarsh changed the title View CodSpeed benchmarks Avoid redundant TokenValue drops in the lexer May 21, 2026
@charliermarsh charliermarsh added the performance Potential performance improvement label May 21, 2026
@charliermarsh charliermarsh marked this pull request as ready for review May 21, 2026 18:19
@charliermarsh charliermarsh merged commit 2fd7ed0 into main May 21, 2026
51 checks passed
@charliermarsh charliermarsh deleted the charlie/codspeed branch May 21, 2026 18:30
thejchap pushed a commit to thejchap/ruff that referenced this pull request May 23, 2026
## Summary

If we avoid resetting the lexer value slot when it's already empty, this
apparently improves performance by 1-2%. (In optimized builds, this
keeps the common case to a discriminant branch instead of re-entering
the `TokenValue` drop path.)
anishgirianish pushed a commit to anishgirianish/ruff that referenced this pull request May 28, 2026
## Summary

If we avoid resetting the lexer value slot when it's already empty, this
apparently improves performance by 1-2%. (In optimized builds, this
keeps the common case to a discriminant branch instead of re-entering
the `TokenValue` drop path.)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance Potential performance improvement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants