Skip to content

Make scope parsing a little bit faster + add "normal case" benchmark - #1434

Closed
sirosen wants to merge 2 commits into
globus:mainfrom
sirosen:make-scope-parsing-even-faster
Closed

sirosen wants to merge 2 commits into
globus:mainfrom
sirosen:make-scope-parsing-even-faster

Conversation

@sirosen

@sirosen sirosen commented Sep 17, 2026

Copy link
Copy Markdown
Member

I noticed that we took a string slice (data[1:]) in a place where it was avoidable.
A new benchmark showed me in local runs that avoiding the slice could save a little bit of time in realistic cases, although it's not much. I'm curious if codspeed benchmarks will show a clearer win for this change.

  • Add a benchmark for "ordinary" scope parsing
  • Avoid unnecessary str slice (copy) in scope parse

Using a very simple non-nested scope string, how long does it take to
parse?
Using `data[1:]` (where `data` is a string) produces a new string, which
necessitates a copy operation. For scope parsing, this is an avoidable
cost, since the new string is only used for iteration under `enumerate()`
anyway. Replace it with direct use of the enumerator, with an appropriate
arithmetic tweak, and we can skip the copy.
@sirosen sirosen added the no-news-is-good-news This change does not require a news file label Sep 17, 2026
@codspeed

codspeed Bot commented Sep 17, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by 22.68%

⚠️ 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

⚡ 1 improved benchmark
✅ 17 untouched benchmarks
🆕 1 new benchmark

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ test_deeply_nested_object_encoding[1w-1d] 143.4 µs 116.9 µs +22.68%
🆕 test_ordinary_scope_parsing N/A 376.6 µs N/A

Tip

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


Comparing sirosen:make-scope-parsing-even-faster (47cc42b) with main (993fe55)

Open in CodSpeed

Comment on lines +240 to +243
iterator = enumerate(data, start=-1)
prev: str
_, prev = next(iterator)
for idx, c in iterator:

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.

For this kind of micro-optimization, I recommend an explanation like:

Suggested change
iterator = enumerate(data, start=-1)
prev: str
_, prev = next(iterator)
for idx, c in iterator:
# Equivalent to `for idx, c in enumerate(data[1:])`, but slightly faster.
iterator = enumerate(data, start=-1)
prev: str
_, prev = next(iterator)
for idx, c in iterator:

Also, is it necessary to state that prev is a string? Its type seems clear, just as c is clear in the for loop.

@sirosen

sirosen commented Sep 18, 2026

Copy link
Copy Markdown
Member Author

I agree pretty well with both bits of feedback. A comment does seem worthwhile, and maybe we don't need to write down the type of the string we get from iteration.

However, I'm self-closing because I think the CI benchmark run shows that I was not accurately measuring things when I tried this locally. I saw a speedup that looked like it was in the 5-10% range on some benchmarks, and it just doesn't reproduce.

Kurt and I had a brief conversation about this and I plan to pull out separate, dedicated benchmark changes. Not only do I like my new benchmark, but I also think the one which shows change here is too fragile.

I still feel instinctively like this bit of parsing code could be made faster, but short of busting out re (which I really don't want for this), I'm not finding it.

@sirosen sirosen closed this Sep 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

no-news-is-good-news This change does not require a news file

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants