Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/globus_sdk/scopes/_graph_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,10 @@ def _peek_enumerate(data: str | list[str]) -> t.Iterator[tuple[int, str, str | N
if not data:
return

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

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.

yield (idx, prev, c)
prev = c

Expand Down
7 changes: 6 additions & 1 deletion tests/benchmark/test_scope_parser.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest

from globus_sdk.scopes import ScopeParser
from globus_sdk.scopes import ScopeParser, TransferScopes


def _make_deep_scope(depth):
Expand All @@ -20,6 +20,11 @@ def _make_wide_scope(width):
return big_scope


def test_ordinary_scope_parsing(benchmark):
scope_string = f"openid profile {TransferScopes.all}"
benchmark(ScopeParser.parse, scope_string)


@pytest.mark.parametrize("depth", (10, 100, 1000, 2000, 3000, 4000, 5000))
def test_deep_scope_parsing(benchmark, depth):
scope_string = _make_deep_scope(depth)
Expand Down
Loading