JDK 9 RFR of 6375303: Review use of caching in BigDecimal

Peter Levart peter.levart at gmail.com
Wed Mar 12 09:08:28 UTC 2014


On 03/11/2014 06:10 PM, Brian Burkhalter wrote:
> Sergey,
>
> On Mar 11, 2014, at 1:18 AM, Sergey Kuksenko wrote:
>
>> Could you share your benchmarks?
> Of course. Please see:
>
> benchmark source:	http://cr.openjdk.java.net/~bpb/6375303/Bench6375303.java

public class Bench6375303 {
*     static final Random rnd = ThreadLocalRandom.current();*
     static final BigDecimal BIG_D = new BigDecimal(new BigInteger(512, rnd));
     static final String BIG_D_STRING = BIG_D.toString();

     @GenerateMicroBenchmark
     @BenchmarkMode(Mode.AverageTime)
     @OutputTimeUnit(TimeUnit.NANOSECONDS)
     public void testFirstToString(BlackHole bh) {
         BigDecimal bigD = new BigDecimal(new BigInteger(512,*rnd*));
         bh.consume(bigD.toString());
     }

     @GenerateMicroBenchmark
     @BenchmarkMode(Mode.AverageTime)
     @OutputTimeUnit(TimeUnit.NANOSECONDS)
     public void testToString(BlackHole bh) {
         bh.consume(BIG_D.toString());
     }
}


Huh! This is a ThreadLocalRandom anti-pattern. Thou should not use a 
ThreadLocalRandom instance in a thread that did not obtain it via a call 
to ThreadLocalRandom.current()...

You could create a new BigInteger(512, *rnd*) in the static initializer 
and use it to create new BigDecima(bigInteger) in testFirstToStrin.

*
Regards, Peter

*

> benchmark results:	http://cr.openjdk.java.net/~bpb/6375303/6375303-bench.html
>
> Let us know should you find anything amiss.
>
> Thanks,
>
> Brian




More information about the core-libs-dev mailing list