RFR: 8320759: Creation of random BigIntegers can be made faster [v3]

Brian Burkhalter bpb at openjdk.org
Mon Dec 4 22:29:43 UTC 2023


On Sat, 2 Dec 2023 15:42:18 GMT, fabioromano1 <duke at openjdk.org> wrote:

>> A faster and simpler way to generate random BigIntegers, avoiding eventually trimming of leading zeros in magnitude array.
>
> fabioromano1 has updated the pull request incrementally with one additional commit since the last revision:
> 
>   Create RandomBigIntegers.java
>   
>   Create a benchmark to measure the performance of BigInteger(int, Random) constructor implementation.

Relaying comments from a colleague:

1. Half of the random bits are wasted by the use of `nextInt()`, which by default invokes `nextLong()` and throws away the lower 32 bits. Not sure if complicating the code to use all the 64 bits of `nextLong()` is worthwhile, though.

2. The way the random bits fill the magnitude array is different. This might break existing reproducible tests with seeded random number generators and fixed seed. Not sure if this is a real problem in practice, though.

3. There seems to be no test coverage that ensures the `BigInteger` invariant has either `mag.length == 0` or `mag[0] != 0`. While the code obviously ensures it, future changes might not, so it might make sense to have this aspect covered by a test.

-------------

PR Comment: https://git.openjdk.org/jdk/pull/16817#issuecomment-1839599463


More information about the core-libs-dev mailing list