Code Review Request, 8152237 Support BigInteger.TWO

Peter Levart peter.levart at
Wed Mar 23 18:01:09 UTC 2016

Hi Xuelei,

On 03/23/2016 04:26 AM, Xuelei Fan wrote:
> Hi,
> Please review the update for the supporting of BigInteger.TWO:
> BigInteger.valueOf(2) is a common BigInteger value used in binary and 
> cryptography operation calculation.  The BigInteger.TWO is not 
> exported, and hence BigInteger.valueOf(2) is used instead in 
> applications and JDK components.  The export of static BigInteger.TWO 
> can improve performance and simplify existing code.
> Thanks,
> Xuelei

I think (haven't tried, just speculate) you could achieve the same 
performance by:

- adding final qualifier to static BigInteger.[posConst|negConst] fields
- annotating those fields with @jdk.internal.vm.annotation.Stable annotation

This way BigInteger.valueOf(-MAX_CONSTANT <= i <= MAX_CONSTANT) when 
called with a constant argument should fold into a constant when 
compiled by JIT.

The same optimization could be performed for valueOf methods of 
java.lang.Byte, Character, Short, Integer & Long.

@Stable annotation was a package-private annotation in java.lang.invoke, 
reserved for method handles infrastructure, but has since been made 
public and moved to a concealed package of java.base. There is already a 
precedent for its use outside in java.lang.invoke: in java.lang.String. 
For example:

static final String s = ".....";

s.charAt(0); // is folded into a constant by JIT

Regards, Peter

More information about the core-libs-dev mailing list