RFR: 8320759: Creation of random BigIntegers can be made faster

fabioromano1 duke at openjdk.org
Mon Nov 27 22:55:08 UTC 2023


On Mon, 27 Nov 2023 22:34:12 GMT, Brian Burkhalter <bpb at openjdk.org> wrote:

>> A faster and simpler way to generate random BigIntegers, avoiding eventually trimming of leading zeros in magnitude array.
>
> src/java.base/share/classes/java/math/BigInteger.java line 754:
> 
>> 752:         // numInts >= 1, since numBits > 0
>> 753: 
>> 754:         int firstInt = rnd.nextInt() & (-1 >>> -numBits); // Mask out any excess bits
> 
> Instead of `numBits`, shouldn't this use something like `excessBits` similar to line 760 in the old version?

It's equivalent. If you watch the implementations of other methods in the class where a mask out is made, this method is often used.

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

PR Review Comment: https://git.openjdk.org/jdk/pull/16817#discussion_r1406865830


More information about the core-libs-dev mailing list