Skip to content

Apply minor optimizations to scope parsing - #1438

Merged
sirosen merged 2 commits into
globus:mainfrom
sirosen:optimize-scope-construction
Sep 25, 2026
Merged

sirosen merged 2 commits into
globus:mainfrom
sirosen:optimize-scope-construction

Conversation

@sirosen

@sirosen sirosen commented Sep 25, 2026

Copy link
Copy Markdown
Member
  1. Don't build methods we don't need on an internal dataclass (ScopeTreeNode) by setting repr=False, eq=False, order=False for that class

  2. Replace any(<containment checking generator>) with a regex search in Scope __post_init__.

(1) is barely measurable. I ran some timeit benchmarking on a simplified example and found that it improves import time by ~100μs (0.1ms) on python3.13.
It will take many import cycles for this to pay the time and energy cost of benchmarking it.
I share it partly as a bit of information learned: we can optimize dataclass definition time, but it's not going to offer a significant win today.

(2) should be noticeable. I observed improvements that were small but measurable in our existing benchmarks.
I also think the regex check is at least as readable as the generator expression it replaces.

@codspeed

codspeed Bot commented Sep 25, 2026 •

Copy link
Copy Markdown

Merging this PR will improve performance by 12.19%

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

⚡ 3 improved benchmarks
✅ 15 untouched benchmarks

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ test_deeply_nested_object_encoding[1w-5d] 177.1 µs 155.9 µs +13.58%
⚡ test_wide_scope_parsing[10000] 568.3 ms 509.6 ms +11.52%
⚡ test_wide_scope_parsing[5000] 283.4 ms 254.2 ms +11.48%

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:optimize-scope-construction (5920be6) with main (8ac1169)

Open in CodSpeed

@sirosen sirosen added the no-news-is-good-news This change does not require a news file label Sep 25, 2026
1.  Don't build methods we don't need on an internal dataclass
    (ScopeTreeNode) by setting `repr=False, eq=False, order=False` for
    that class

2.  Replace `any(<containment checking generator>)` with a regex search
    in Scope __post_init__.
@sirosen
sirosen force-pushed the optimize-scope-construction branch from 615b68d to 3bcd807 Compare September 25, 2026 19:56
Comment thread src/globus_sdk/scopes/representation.py Outdated
Co-authored-by: Kurt McKee <contactme@kurtmckee.org>
@sirosen
sirosen merged commit e18f538 into globus:main Sep 25, 2026
11 checks passed
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