[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
bug#38753: 27.0.60; cl--random-state uncontrolled growth due to bignums
From: |
Christopher Wellons |
Subject: |
bug#38753: 27.0.60; cl--random-state uncontrolled growth due to bignums |
Date: |
Sun, 29 Dec 2019 09:27:18 -0500 |
User-agent: |
NeoMutt/20170113 (1.7.2) |
> Any idea how easy it would be to fix either of them, or both?
The reason I found that bug is because I was experimenting with
addressing exactly that problem:
https://github.com/skeeto/lcg128
It's got the right features, including support for arbitrary limits (per
your example), but I'm not satisfied with the performance. For arbitrary
limits it uses the same rejection algorithm as Python — generate (logb
lim) bits and reject if out of range — and cl-random could be updated to
use the same technique. The LCG around 6x slower than cl-random in the
common case (generating small numbers). Bignums aren't ideal for this
since every operation requires an allocation, and the LCG throws away
half of the work (the upper half of the multiplication result).
The lagged Fibonacci generator really does hit a sweet spot for Emacs
Lisp, where, before bignums, even 32-bit integers weren't a reliable
option. So it fits within Emacs' worst fixnum limitations and requires
few bytecode instructions to generate a number.
The random built-in ought to simply be pcg32 across all platforms:
http://www.pcg-random.org/download.html
Currently it's whatever crummy host-provided PRNG it happens to find at
compile time. It's certainly possible to implement pcg32 using Emacs 27
bignums, but it would be even slower than my LCG, especially on 32-bit
platforms. In C it's very, very fast.
If the pcg32 state, just one 64-bit integer, was an optional parameter,
then cl-random could build on it. However, the state would nearly always
be a bignum, even on 64-bit platforms, and that would really slow it
down. I haven't yet thought of any obviously good options for this.