Commit 4b8fdca
committed
[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.1 parent 675b6bf commit 4b8fdca
16 files changed
Lines changed: 1470 additions & 1 deletion
File tree
- mypyc
- codegen
- ir
- lib-rt
- random
- primitives
- test-data
- test
- mypy/typeshed/stubs/librt/librt
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
121 | 121 | | |
122 | 122 | | |
123 | 123 | | |
| 124 | + | |
124 | 125 | | |
125 | 126 | | |
126 | 127 | | |
| |||
631 | 632 | | |
632 | 633 | | |
633 | 634 | | |
| 635 | + | |
| 636 | + | |
| 637 | + | |
634 | 638 | | |
635 | 639 | | |
636 | 640 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
59 | 59 | | |
60 | 60 | | |
61 | 61 | | |
| 62 | + | |
62 | 63 | | |
63 | 64 | | |
64 | 65 | | |
| |||
1224 | 1225 | | |
1225 | 1226 | | |
1226 | 1227 | | |
| 1228 | + | |
| 1229 | + | |
| 1230 | + | |
| 1231 | + | |
1227 | 1232 | | |
1228 | 1233 | | |
1229 | 1234 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
109 | 109 | | |
110 | 110 | | |
111 | 111 | | |
| 112 | + | |
112 | 113 | | |
113 | 114 | | |
114 | 115 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
41 | 41 | | |
42 | 42 | | |
43 | 43 | | |
44 | | - | |
| 44 | + | |
45 | 45 | | |
46 | 46 | | |
47 | 47 | | |
| |||
544 | 544 | | |
545 | 545 | | |
546 | 546 | | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
547 | 551 | | |
548 | 552 | | |
549 | 553 | | |
550 | 554 | | |
| 555 | + | |
551 | 556 | | |
552 | 557 | | |
553 | 558 | | |
| |||
0 commit comments