mypy
4b8fdcaf - [mypyc] Add librt.random module (#21433)

Commit
49 days ago
[mypyc] Add librt.random module (#21433) 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.
Author
Committer
Parents
Loading