-
Notifications
You must be signed in to change notification settings - Fork 2k
[aiohttp] - use lcg as random generator #9942
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
@Dreamsorcerer please take a look 🙏 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This kind of feels like cheating, this seems beyond benchmarking a realistic framework.
This feels more like a potential contribution to cpython to provide faster built-in random functions, thus benefiting everyone.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This kind of feels like cheating, this seems beyond benchmarking a realistic framework.
Agreed, but like many other things, module-level caching, execute queries without read actual results.
This looks more like a potential contribution to cpython to provide faster built-in random functions, which would benefit everyone.
This is certainly not intended as a contribution to Python itself, since LCGs are inferior to existing random number generators. They have significantly shorter periods, and their speed advantage comes only from the small space size and the simplicity of the algorithm. In our case, the short period and predictability of the sequence don’t matter—we’re not aiming for cryptographic or truly random values—but overall, LCGs are still a poor choice.
We are going back to the roots here 😀 Even Python 1.0 had improved version of LCG
https://github.com/nagayev/old-python/blob/a93727bb9eb40818ecaafb50e1e942ab75b3b6d3/Lib/whrandom.py#L73
@@ -4,7 +4,9 @@ ADD ./ /aiohttp | |||
|
|||
WORKDIR /aiohttp | |||
|
|||
RUN pip3 install -r /aiohttp/requirements-cpython.txt | |||
RUN pip3 install -r /aiohttp/requirements-cpython.txt && \ | |||
pip3 install cython==3.1.2 setuptools==80.9.0 && \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we do use this, we atleast want dependencies in the requirements files, so they can be updated with Dependabot.
We currently make a large number of randint calls, particularly within the update method — for example, each call to update may trigger random * 2 * number of queries.
The existing Python implementation uses the Mersenne Twister, which is a high-quality RNG with 53-bit precision and a very long period (2**19937−1). However, for our simplified use case, a classic LCG (Linear Congruential Generator) is more than sufficient and significantly faster, especially given the number of calls we make.
Benchmark code
Results
Updates
Before
After