Use faster generator for link IDs #3845
Open
+10
−6
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Type of changes
Checklist
Description
I was profiling some code that uses
rich
and noticed that some unusual time was spent inrandom.getrandbits()
(which underliesrandom.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_id
s 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 saysrandom.randint(0, 999999)
tends to have a collision in the first 1,253 calls.Running
shows that the new generator is about 8 times faster than the old one (0.381s vs 3.056s).