Use faster generator for link IDs
Type of changes
- [ ] Bug fix
- [ ] New feature
- [ ] Documentation / docstrings
- [ ] Tests
- [x] Other
Checklist
- [x] 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.
- [x] 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).
:warning: Please install the to ensure uploads and comments are reliably processed by Codecov.
Codecov Report
:white_check_mark: All modified and coverable lines are covered by tests.
:white_check_mark: Project coverage is 97.84%. Comparing base (56855a6) to head (75a019e).
:warning: Report is 87 commits behind head on master.
:exclamation: 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%) |
:arrow_up: |
Flags with carried forward coverage won't be shown. Click here to find out more.
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.
:rocket: New features to boost your workflow:
- :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
Rebased on 14.2. Gentle review request nudge, @willmcgugan?