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

zrlw pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/dubbo-hessian-lite.git


The following commit(s) were added to refs/heads/master by this push:
     new cc4fb8a9 Allocate init capacity directly according to the expected 
size to avoid resize. (#95)
cc4fb8a9 is described below

commit cc4fb8a91035e6410abf0820ff630082693dcb2b
Author: Ares_xue <[email protected]>
AuthorDate: Sat Sep 20 11:13:17 2025 +0800

    Allocate init capacity directly according to the expected size to avoid 
resize. (#95)
---
 .../caucho/hessian/io/CollectionDeserializer.java  | 14 +++----
 .../com/caucho/hessian/io/MapDeserializer.java     | 10 ++---
 .../com/alibaba/com/caucho/hessian/io/MapUtil.java | 46 ++++++++++++++++++++++
 3 files changed, 56 insertions(+), 14 deletions(-)

diff --git 
a/hessian-lite/src/main/java/com/alibaba/com/caucho/hessian/io/CollectionDeserializer.java
 
b/hessian-lite/src/main/java/com/alibaba/com/caucho/hessian/io/CollectionDeserializer.java
index 459435a4..3d1447cb 100644
--- 
a/hessian-lite/src/main/java/com/alibaba/com/caucho/hessian/io/CollectionDeserializer.java
+++ 
b/hessian-lite/src/main/java/com/alibaba/com/caucho/hessian/io/CollectionDeserializer.java
@@ -78,7 +78,7 @@ public class CollectionDeserializer extends 
AbstractListDeserializer {
 
     @Override
     public Object readList(AbstractHessianInput in, int length, Class<?> 
expectType) throws IOException {
-        Collection list = createList();
+        Collection list = createList(length);
 
         in.addRef(list);
 
@@ -106,7 +106,7 @@ public class CollectionDeserializer extends 
AbstractListDeserializer {
 
     @Override
     public Object readLengthList(AbstractHessianInput in, int length, Class<?> 
expectType) throws IOException {
-        Collection list = createList();
+        Collection list = createList(length);
 
         in.addRef(list);
 
@@ -125,12 +125,12 @@ public class CollectionDeserializer extends 
AbstractListDeserializer {
         return list;
     }
 
-    private Collection createList()
+    private Collection createList(int expectedSize)
             throws IOException {
         Collection list = null;
 
         if (_type == null)
-            list = new ArrayList();
+            list = new ArrayList(expectedSize);
         else if (!_type.isInterface()) {
             try {
                 list = (Collection) _type.newInstance();
@@ -142,11 +142,9 @@ public class CollectionDeserializer extends 
AbstractListDeserializer {
         } else if (SortedSet.class.isAssignableFrom(_type))
             list = new TreeSet();
         else if (Set.class.isAssignableFrom(_type))
-            list = new HashSet();
-        else if (List.class.isAssignableFrom(_type))
-            list = new ArrayList();
+            list = new HashSet(MapUtil.capacity(expectedSize));
         else if (Collection.class.isAssignableFrom(_type))
-            list = new ArrayList();
+            list = new ArrayList(expectedSize);
         else {
             try {
                 list = (Collection) _type.newInstance();
diff --git 
a/hessian-lite/src/main/java/com/alibaba/com/caucho/hessian/io/MapDeserializer.java
 
b/hessian-lite/src/main/java/com/alibaba/com/caucho/hessian/io/MapDeserializer.java
index f7a9a4e7..45f99d4c 100644
--- 
a/hessian-lite/src/main/java/com/alibaba/com/caucho/hessian/io/MapDeserializer.java
+++ 
b/hessian-lite/src/main/java/com/alibaba/com/caucho/hessian/io/MapDeserializer.java
@@ -150,7 +150,7 @@ public class MapDeserializer extends 
AbstractMapDeserializer {
                              Object[] fields)
             throws IOException {
         String[] fieldNames = (String[]) fields;
-        Map<Object, Object> map = createMap();
+        Map<Object, Object> map = createMap(fieldNames.length);
 
         int ref = in.addRef(map);
 
@@ -163,13 +163,11 @@ public class MapDeserializer extends 
AbstractMapDeserializer {
         return map;
     }
 
-    private Map createMap()
+    private Map createMap(int expectedSize)
             throws IOException {
 
-        if (_type == null)
-            return new HashMap();
-        else if (_type.equals(Map.class))
-            return new HashMap();
+        if (_type == null || _type.equals(Map.class) || 
_type.equals(HashMap.class))
+            return new HashMap(MapUtil.capacity(expectedSize));
         else if (_type.equals(SortedMap.class))
             return new TreeMap();
         else {
diff --git 
a/hessian-lite/src/main/java/com/alibaba/com/caucho/hessian/io/MapUtil.java 
b/hessian-lite/src/main/java/com/alibaba/com/caucho/hessian/io/MapUtil.java
new file mode 100644
index 00000000..185c922d
--- /dev/null
+++ b/hessian-lite/src/main/java/com/alibaba/com/caucho/hessian/io/MapUtil.java
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.alibaba.com.caucho.hessian.io;
+
+
+class MapUtil {
+
+  private static final int MAX_POWER_OF_TWO = 1 << (Integer.SIZE - 2);
+
+  /**
+   * @see java.util.HashMap#DEFAULT_INITIAL_CAPACITY
+   */
+  private static final int DEFAULT_INITIAL_CAPACITY = 1 << 4;
+
+  protected static int capacity(int expectedSize) {
+    if (expectedSize < 0) {
+      return DEFAULT_INITIAL_CAPACITY;
+    }
+
+    if (expectedSize < 3) {
+      return expectedSize + 1;
+    }
+    if (expectedSize < MAX_POWER_OF_TWO) {
+      // This is the calculation used in JDK8 to resize when a putAll
+      // happens; it seems to be the most conservative calculation we
+      // can make.  0.75 is the default load factor.
+      return (int) ((float) expectedSize / 0.75F + 1.0F);
+    }
+    return Integer.MAX_VALUE;
+  }
+
+}

Reply via email to