Skip to content

Commit

Permalink
caculate capacity in the default constructor of InternalThreadLocalMap (
Browse files Browse the repository at this point in the history
  • Loading branch information
kingkh1995 committed Sep 11, 2021
1 parent cfece57 commit d173d16
Showing 1 changed file with 14 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,23 @@ public int size() {
}

private static Object[] newIndexedVariableTable() {
Object[] array = new Object[32];
int variableIndex = NEXT_INDEX.get();
int newCapacity = variableIndex < 32 ? 32 : newCapacity(variableIndex);
Object[] array = new Object[newCapacity];
Arrays.fill(array, UNSET);
return array;
}

private static int newCapacity(int index) {
int newCapacity = index;
newCapacity |= newCapacity >>> 1;
newCapacity |= newCapacity >>> 2;
newCapacity |= newCapacity >>> 4;
newCapacity |= newCapacity >>> 8;
newCapacity |= newCapacity >>> 16;
return ++newCapacity;
}

private static InternalThreadLocalMap fastGet(InternalThread thread) {
InternalThreadLocalMap threadLocalMap = thread.threadLocalMap();
if (threadLocalMap == null) {
Expand All @@ -151,14 +163,7 @@ private static InternalThreadLocalMap slowGet() {
private void expandIndexedVariableTableAndSet(int index, Object value) {
Object[] oldArray = indexedVariables;
final int oldCapacity = oldArray.length;
int newCapacity = index;
newCapacity |= newCapacity >>> 1;
newCapacity |= newCapacity >>> 2;
newCapacity |= newCapacity >>> 4;
newCapacity |= newCapacity >>> 8;
newCapacity |= newCapacity >>> 16;
newCapacity++;

int newCapacity = newCapacity(index);
Object[] newArray = Arrays.copyOf(oldArray, newCapacity);
Arrays.fill(newArray, oldCapacity, newArray.length, UNSET);
newArray[index] = value;
Expand Down

0 comments on commit d173d16

Please sign in to comment.