From: Christopher Wellons <wellons@nullprogram.com>
To: Pip Cet <pipcet@gmail.com>
Cc: 38753@debbugs.gnu.org
Subject: bug#38753: 27.0.60; cl--random-state uncontrolled growth due to bignums
Date: Sun, 29 Dec 2019 09:27:18 -0500 [thread overview]
Message-ID: <20191229142718.q2vvumr6e7pbobdu@nullprogram.com> (raw)
In-Reply-To: <CAOqdjBcyunFpsHuCb3HWQFLUnS4uVUm4qr4iw0KcDe1Ese4agw@mail.gmail.com>
> 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.
next prev parent reply other threads:[~2019-12-29 14:27 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-12-26 17:12 bug#38753: 27.0.60; cl--random-state uncontrolled growth due to bignums Christopher Wellons
2019-12-29 12:59 ` Mattias Engdegård
2019-12-29 13:31 ` Pip Cet
2019-12-29 13:48 ` Philipp Stephani
2019-12-29 14:27 ` Christopher Wellons [this message]
2019-12-29 17:33 ` Pip Cet
2019-12-29 18:12 ` Eli Zaretskii
2019-12-29 14:09 ` Mattias Engdegård
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20191229142718.q2vvumr6e7pbobdu@nullprogram.com \
--to=wellons@nullprogram.com \
--cc=38753@debbugs.gnu.org \
--cc=pipcet@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.