Skip to content

Use faster generator for link IDs#3845

Merged
willmcgugan merged 1 commit intoTextualize:masterfrom
akx:faster-ids
Apr 11, 2026
Merged

Use faster generator for link IDs#3845
willmcgugan merged 1 commit intoTextualize:masterfrom
akx:faster-ids

Conversation

@akx
Copy link
Copy Markdown
Contributor

@akx akx commented Sep 10, 2025

Type of changes

  • Bug fix
  • New feature
  • Documentation / docstrings
  • Tests
  • Other

Checklist

  • I've run the latest black with default args on new code.
  • I've updated CHANGELOG.md and CONTRIBUTORS.md where appropriate.
  • I've added tests for new code.
  • I accept that @willmcgugan may be pedantic in the code review.

AI?

  • AI was used to generate this PR

AI was not used to generate this PR.

Description

I was profiling some code that uses rich and noticed that some unusual time was spent in random.getrandbits() (which underlies random.randint()). The default RNG in Python is not the fastest to begin with (which is why e.g. fastrand is a thing).

This PR switches the generator for _link_ids from a random number to a simple sequential counter (initialized once with randomness). This also has the happy side effect that link ID collisions are simply not possible anymore, whereas the birthday paradox says random.randint(0, 999999) tends to have a collision in the first 1,253 calls.

Running

print(timeit.timeit(lambda: next(_id_generator), number=10000000))
print(timeit.timeit(lambda: randint(0, 999999), number=10000000))

shows that the new generator is about 8 times faster than the old one (0.381s vs 3.056s).

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Sep 10, 2025

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.87%. Comparing base (efe8f61) to head (9ac7e79).
⚠️ Report is 47 commits behind head on master.
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #3845   +/-   ##
=======================================
  Coverage   97.87%   97.87%           
=======================================
  Files          96       96           
  Lines        8370     8372    +2     
=======================================
+ Hits         8192     8194    +2     
  Misses        178      178           
Flag Coverage Δ
unittests 97.87% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@akx
Copy link
Copy Markdown
Contributor Author

akx commented Oct 16, 2025

Rebased on 14.2. Gentle review request nudge, @willmcgugan?

Comment thread rich/style.py Outdated
@akx akx force-pushed the faster-ids branch 2 times, most recently from 37ac060 to fb43279 Compare January 23, 2026 16:31
@akx akx requested a review from willmcgugan January 23, 2026 16:32
@akx
Copy link
Copy Markdown
Contributor Author

akx commented Feb 26, 2026

Codespell being red is unrelated.

@akx
Copy link
Copy Markdown
Contributor Author

akx commented Mar 27, 2026

Gentle nudge, @willmcgugan? 😄
Would likely make Toad a tiny bit faster too 🐸

@willmcgugan willmcgugan merged commit a86dce0 into Textualize:master Apr 11, 2026
23 checks passed
@akx akx deleted the faster-ids branch April 11, 2026 15:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants