Skip to content

Conversation

akx
Copy link

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

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 from the module's approximate import wallclock time). 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

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.84%. Comparing base (56855a6) to head (75a019e).
⚠️ Report is 87 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.84%   97.84%           
=======================================
  Files          74       74           
  Lines        8152     8155    +3     
=======================================
+ Hits         7976     7979    +3     
  Misses        176      176           
Flag Coverage Δ
unittests 97.84% <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.

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.

2 participants