Skip to content

Avoid per-token source index visitor calls - #26506

Merged
charliermarsh merged 1 commit into
mainfrom
charlie/codex-parser-throughput
Jul 1, 2026
Merged

Avoid per-token source index visitor calls#26506
charliermarsh merged 1 commit into
mainfrom
charlie/codex-parser-throughput

Conversation

@charliermarsh

@charliermarsh charliermarsh commented Jul 1, 2026

Copy link
Copy Markdown
Member

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

@codspeed-hq

codspeed-hq Bot commented Jul 1, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by 4.63%

⚡ 1 improved benchmark
✅ 59 untouched benchmarks
⏩ 93 skipped benchmarks1

Performance Changes

Mode Benchmark BASE HEAD Efficiency
Simulation linter/default-rules[large/dataset.py] 4.4 ms 4.2 ms +4.63%

Tip

Curious why this is faster? Use the CodSpeed MCP and ask your agent.


Comparing charlie/codex-parser-throughput (f673f29) with main (dc42d1d)

Open in CodSpeed

Footnotes

  1. 93 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 added the performance Potential performance improvement label Jul 1, 2026
@charliermarsh charliermarsh changed the title [codex] Reduce per-token source indexing overhead Reduce per-token source indexing overhead Jul 1, 2026
@charliermarsh
charliermarsh force-pushed the charlie/codex-parser-throughput branch from 0bcbd8b to f673f29 Compare July 1, 2026 14:17
@charliermarsh charliermarsh changed the title Reduce per-token source indexing overhead Avoid per-token source index visitor calls Jul 1, 2026
@charliermarsh
charliermarsh marked this pull request as ready for review July 1, 2026 15:15
@charliermarsh
charliermarsh merged commit d380534 into main Jul 1, 2026
47 checks passed
@charliermarsh
charliermarsh deleted the charlie/codex-parser-throughput branch July 1, 2026 15:18
charliermarsh added a commit that referenced this pull request Jul 1, 2026
## 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](https://app.codspeed.io/astral-sh/ruff/branches/charlie%2Fskip-empty-indexer-trivia)
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.
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