Skip to content

Skip empty trivia scans in the source indexer - #26507

Merged
charliermarsh merged 1 commit into
mainfrom
charlie/skip-empty-indexer-trivia
Jul 1, 2026
Merged

Skip empty trivia scans in the source indexer#26507
charliermarsh merged 1 commit into
mainfrom
charlie/skip-empty-indexer-trivia

Conversation

@charliermarsh

@charliermarsh charliermarsh commented Jul 1, 2026

Copy link
Copy Markdown
Member

Summary

Indexer::from_tokens scans the trivia between consecutive tokens to find continuation newlines. Most tokens are adjacent, but we still construct an empty slice and run match_indices for those pairs.

Skip that scan when the previous token ends where the current token starts. Non-empty gaps continue through the existing newline logic, preserving the indexed continuation lines.

This is split from #26506 so CodSpeed can measure the empty-trivia fast path independently.

Performance

The full CodSpeed report compares 779b0c2 with dc42d1d. All 15 linter CPU-simulation measurements improve:

Configuration Gain across the five workloads
Default rules 1.06-2.72%
All rules 0.30-0.64%
All rules with preview 0.25-0.53%

Summing the five default-rules head and base measurements gives a 2.38% aggregate improvement. No individual benchmark crosses CodSpeed's significance threshold, which is why the GitHub bot does not list these gains even though they appear in the full report.

@charliermarsh
charliermarsh marked this pull request as ready for review July 1, 2026 15:15
@charliermarsh charliermarsh added the performance Potential performance improvement label Jul 1, 2026
charliermarsh added a commit that referenced this pull request Jul 1, 2026
## Summary

`Indexer::from_tokens` previously called both range builders and checked
string flags for every token, even though only string tokens can affect
those indexes or span multiple lines.

Dispatch the existing builders from the token-kind match instead.
Interpolated-string boundaries visit the interpolated-range builder,
regular strings and f-string middles visit the multiline-range builder,
and t-string middles only update the current line start. This preserves
the indexed ranges while removing visitor calls and flag lookup from the
common non-string path.

The empty-trivia fast path was split into #26507 so CodSpeed can measure
the two optimizations independently.

## Performance

The [full CodSpeed
report](https://app.codspeed.io/astral-sh/ruff/branches/charlie%2Fcodex-parser-throughput)
compares `f673f29` with `dc42d1d`. All 15 linter CPU-simulation
measurements improve:

| Configuration | Gain across the five workloads |
| --- | ---: |
| Default rules | 1.52-4.63% |
| All rules | 0.36-1.09% |
| All rules with preview | 0.31-0.89% |

Summing the five default-rules head and base measurements gives a 4.00%
aggregate improvement. CodSpeed classifies
`linter/default-rules[large/dataset.py]` at 4.63% as a significant
improvement; the other measured gains are below its reporting threshold.
@charliermarsh
charliermarsh merged commit e6856de into main Jul 1, 2026
47 checks passed
@charliermarsh
charliermarsh deleted the charlie/skip-empty-indexer-trivia branch July 1, 2026 15:18
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.

1 participant