| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Sorry, something went wrong.
| ModDesc( | ||
| "librt.random", | ||
| ["random/librt_random.c"], | ||
| ["random/librt_random.h", "random/librt_random_api.h", "random/librt_random_api.c"], |
There was a problem hiding this comment.
the _api files shouldn't be necessary when building the module as a shared library.
Sorry, something went wrong.
for more information, see https://pre-commit.ci
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
Sorry, something went wrong.
The stdlib `random` module is fairly often used in performance critical
code, and it's not super efficient. Add `librt.random` with a subset of
the stdlib module interface that is optimized for performance when
compiled.
Use ChaCha8 as the algorithm. Based on some research, this is a modern,
high-quality PRNG algorithm. It's used by Go `math/rand/v2`, among
others.
This is a non-cryptographic PRNG, similar to the stdlib `random` module
(but this uses a different algorithm).
I used Claude Code and Codex to write all the code, but I iterated on it
quite a lot and did a bunch of manual validation and code review. I also
asked Codex to explicitly check that the ChaCha8 implementation is
correct by comparing it to a reference implementation.
Use thread-local RNG state for module-level functions to enable good
scaling in free-threaded builds. There's some extra complexity from
having to free the state at thread exit.
I asked a LLM to generate and run a benchmark, and here are the results
on 3.14:
```
│ Function │ vs stdlib(compiled) │ vs stdlib(interpreted) │
│ random() │ 3.2x │ 4.8x │
│ randint() │ 16.6x │ 18.0x │
│ randrange() │ 14.5x │ 16.2x │
│ choice() │ 12.9x │ 10.3x │
```
`choice()` was replaced with `randrange` when using `librt`, since we don't provide it as part of this fairly minimal API.
| Back | FazBrowse Home | New Git URL |
The stdlib random module is fairly often used in performance critical code, and it's not super efficient. Add librt.random with a subset of the stdlib module interface that is optimized for performance when compiled.
Use ChaCha8 as the algorithm. Based on some research, this is a modern, high-quality PRNG algorithm. It's used by Go math/rand/v2, among others.
This is a non-cryptographic PRNG, similar to the stdlib random module (but this uses a different algorithm).
I used Claude Code and Codex to write all the code, but I iterated on it quite a lot and did a bunch of manual validation and code review. I also asked Codex to explicitly check that the ChaCha8 implementation is correct by comparing it to a reference implementation.
Use thread-local RNG state for module-level functions to enable good scaling in free-threaded builds. There's some extra complexity from having to free the state at thread exit.
I asked a LLM to generate and run a benchmark, and here are the results on 3.14:
choice() was replaced with randrange when using librt, since we don't provide it as part of this fairly minimal API.