]> git.lizzy.rs Git - rust.git/commit
rand: inform the optimiser that indexing is never out-of-bounds.
authorHuon Wilson <dbau.pp+github@gmail.com>
Mon, 8 Sep 2014 09:33:37 +0000 (19:33 +1000)
committerHuon Wilson <dbau.pp+github@gmail.com>
Mon, 8 Sep 2014 09:33:37 +0000 (19:33 +1000)
commitcc6a4877a42b275b85d02ce96b8539e1d7a6f47c
tree6a6449a1b087ea4a8fb948ffe1bec266548c469d
parent5dfb7a6ec1c1b7827a1f019b276c4d959cf2d71e
rand: inform the optimiser that indexing is never out-of-bounds.

This uses a bitwise mask to ensure that there's no bounds checking for
the array accesses when generating the next random number. This isn't
costless, but the single instruction is nothing compared to the branch.

A `debug_assert` for "bounds check" is preserved to ensure that
refactoring doesn't accidentally break it (i.e. create values of `cnt`
that are out of bounds with the masking causing it to silently wrap-
around).

Before:

    test test::rand_isaac   ... bench: 990 ns/iter (+/- 24) = 808 MB/s
    test test::rand_isaac64 ... bench: 614 ns/iter (+/- 25) = 1302 MB/s

After:

    test test::rand_isaac   ... bench: 877 ns/iter (+/- 134) = 912 MB/s
    test test::rand_isaac64 ... bench: 470 ns/iter (+/- 30) = 1702 MB/s

(It also removes the unsafe code in Isaac64Rng.next_u64, with a *gain*
in performance; today is a good day.)
src/librand/isaac.rs