> We could check for the existing cacheLine.length right before installing the new one
Do you mean something like this ? BigInteger getRadixConversionCache(int radix, int exponent) { BigInteger[] cacheLine = powerCache[radix]; // volatile read if (exponent < cacheLine.length) return cacheLine[exponent]; int oldLength = cacheLine.length; cacheLine = Arrays.copyOf(cacheLine, exponent + 1); for (int i = oldLength; i < exponent + 1; i++) cacheLine[i] = cacheLine[i - 1].square(); if (exponent >= powerCache[radix].length) { // volatile read again BigInteger[][] pc = Arrays.copyOf(powerCache); pc[radix] = cacheLine; powerCache = pc; // volatile write, publish } return cacheLine[exponent]; } On Wed, Jun 26, 2013 at 10:31 AM, Aleksey Shipilev < aleksey.shipi...@oracle.com> wrote: > On 26.06.2013, at 7:31, Dmitry Nadezhin <dmitry.nadez...@gmail.com> wrote: > > > We have two versions after private discussion with Aleksey. > > > > BigInteger getRadixConversionCache(int radix, int exponent) { > > BigInteger[][] pc = powerCache; // volatile read > > BigInteger[] cacheLine = pc[radix]; > > if (exponent < cacheLine.length) > > return cacheLine[exponent]; > > > > int oldSize = cacheLine.length; > > cacheLine = Arrays.copyOf(cacheLine, exponent + 1); > > for (int i = oldSize; i < exponent + 1; i++) > > cacheLine[i] = cacheLine[i - 1].square(); > > > > pc = Arrays.copyOf(powerCache); > > pc[radix] = cacheLine; > > powerCache = pc; // volatile write, publish > > return cacheLine[exponent]; > > } > > Thanks, I like this version a lot better. We could check for the existing > cacheLine.length right before installing the new one, so the opportunity to > overwrite larger cacheLine would be minimal, but I do think the probability > of unlucky timing is very low, and the argument is moot :) Let's keep it > simple. > > -Aleksey.