GNU bug report logs -
#38753
27.0.60; cl--random-state uncontrolled growth due to bignums
Previous Next
Full log
Message #22 received at 38753 <at> debbugs.gnu.org (full text, mbox):
> 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.
This bug report was last modified 5 years and 141 days ago.
Previous Next
GNU bug tracking system
Copyright (C) 1999 Darren O. Benham,
1997,2003 nCipher Corporation Ltd,
1994-97 Ian Jackson.