提交 4d09055c 编写于 作者: B bpb

8017540: Improve multi-threaded contention behavior of radix conversion cache

Summary: Replace array of ArrayList of BigIntegers with a volatile two-dimensional BigInteger array eliminate the synchronization of getRadixConversionCache()
Reviewed-by: plevart, shade, bpb, alanb
Contributed-by: NPeter Levart &lt;peter.levart@gmail.com&gt;, Dmitry Nadezhin &lt;dmitry.nadezhin@oracle.com&gt;, Aleksey Shipilev <aleksey.shipilev@oracle.com>
上级 a53b2c9f
...@@ -1042,7 +1042,7 @@ public class BigInteger extends Number implements Comparable<BigInteger> { ...@@ -1042,7 +1042,7 @@ public class BigInteger extends Number implements Comparable<BigInteger> {
* recalculate powers of radix^(2^n) more than once. This speeds * recalculate powers of radix^(2^n) more than once. This speeds
* Schoenhage recursive base conversion significantly. * Schoenhage recursive base conversion significantly.
*/ */
private static ArrayList<BigInteger>[] powerCache; private static volatile BigInteger[][] powerCache;
/** The cache of logarithms of radices for base conversion. */ /** The cache of logarithms of radices for base conversion. */
private static final double[] logCache; private static final double[] logCache;
...@@ -1063,14 +1063,12 @@ public class BigInteger extends Number implements Comparable<BigInteger> { ...@@ -1063,14 +1063,12 @@ public class BigInteger extends Number implements Comparable<BigInteger> {
* with just the very first value. Additional values will be created * with just the very first value. Additional values will be created
* on demand. * on demand.
*/ */
powerCache = (ArrayList<BigInteger>[]) powerCache = new BigInteger[Character.MAX_RADIX+1][];
new ArrayList[Character.MAX_RADIX+1];
logCache = new double[Character.MAX_RADIX+1]; logCache = new double[Character.MAX_RADIX+1];
for (int i=Character.MIN_RADIX; i<=Character.MAX_RADIX; i++) for (int i=Character.MIN_RADIX; i<=Character.MAX_RADIX; i++)
{ {
powerCache[i] = new ArrayList<BigInteger>(1); powerCache[i] = new BigInteger[] { BigInteger.valueOf(i) };
powerCache[i].add(BigInteger.valueOf(i));
logCache[i] = Math.log(i); logCache[i] = Math.log(i);
} }
} }
...@@ -3454,22 +3452,25 @@ public class BigInteger extends Number implements Comparable<BigInteger> { ...@@ -3454,22 +3452,25 @@ public class BigInteger extends Number implements Comparable<BigInteger> {
* This could be changed to a more complicated caching method using * This could be changed to a more complicated caching method using
* <code>Future</code>. * <code>Future</code>.
*/ */
private static synchronized BigInteger getRadixConversionCache(int radix, private static BigInteger getRadixConversionCache(int radix, int exponent) {
int exponent) { BigInteger[] cacheLine = powerCache[radix]; // volatile read
BigInteger retVal = null; if (exponent < cacheLine.length) {
ArrayList<BigInteger> cacheLine = powerCache[radix]; return cacheLine[exponent];
int oldSize = cacheLine.size();
if (exponent >= oldSize) {
cacheLine.ensureCapacity(exponent+1);
for (int i=oldSize; i<=exponent; i++) {
retVal = cacheLine.get(i-1).square();
cacheLine.add(i, retVal);
} }
int oldLength = cacheLine.length;
cacheLine = Arrays.copyOf(cacheLine, exponent + 1);
for (int i = oldLength; i <= exponent; i++) {
cacheLine[i] = cacheLine[i - 1].pow(2);
} }
else
retVal = cacheLine.get(exponent);
return retVal; BigInteger[][] pc = powerCache; // volatile read again
if (exponent >= pc[radix].length) {
pc = pc.clone();
pc[radix] = cacheLine;
powerCache = pc; // volatile write, publish
}
return cacheLine[exponent];
} }
/* zero[i] is a string of i consecutive zeros. */ /* zero[i] is a string of i consecutive zeros. */
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册