You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by al...@apache.org on 2021/09/11 13:56:07 UTC

[dubbo] branch 3.0 updated: caculate capacity in the default constructor of InternalThreadLocalMap (#8697)

This is an automated email from the ASF dual-hosted git repository.

albumenj pushed a commit to branch 3.0
in repository https://gitbox.apache.org/repos/asf/dubbo.git


The following commit(s) were added to refs/heads/3.0 by this push:
     new d173d16  caculate capacity in the default constructor of InternalThreadLocalMap (#8697)
d173d16 is described below

commit d173d16b6e714e0b2bf4f941adc335f5c95fee7e
Author: kaikoo <42...@users.noreply.github.com>
AuthorDate: Sat Sep 11 21:55:59 2021 +0800

    caculate capacity in the default constructor of InternalThreadLocalMap (#8697)
---
 .../common/threadlocal/InternalThreadLocalMap.java | 23 +++++++++++++---------
 1 file changed, 14 insertions(+), 9 deletions(-)

diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/threadlocal/InternalThreadLocalMap.java b/dubbo-common/src/main/java/org/apache/dubbo/common/threadlocal/InternalThreadLocalMap.java
index 9b38eb4..54c3e60 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/common/threadlocal/InternalThreadLocalMap.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/threadlocal/InternalThreadLocalMap.java
@@ -125,11 +125,23 @@ public final class InternalThreadLocalMap {
     }
 
     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) {
@@ -151,14 +163,7 @@ public final class InternalThreadLocalMap {
     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;