From d5e2704b0abda19937ba7b44a6b1cab56df2f52b Mon Sep 17 00:00:00 2001 From: godotg Date: Sat, 24 Sep 2022 19:25:18 +0800 Subject: [PATCH] =?UTF-8?q?perf[map]:=20=E4=BC=98=E5=8C=96=E8=AE=A1?= =?UTF-8?q?=E7=AE=97HashMap=E5=88=9D=E5=A7=8B=E5=8C=96=E5=90=88=E9=80=82?= =?UTF-8?q?=E7=9A=84=E5=A4=A7=E5=B0=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../zfoo/protocol/buffer/ByteBufUtils.java | 24 +++++++++---------- .../protocol/collection/CollectionUtils.java | 17 +------------ 2 files changed, 13 insertions(+), 28 deletions(-) diff --git a/protocol/src/main/java/com/zfoo/protocol/buffer/ByteBufUtils.java b/protocol/src/main/java/com/zfoo/protocol/buffer/ByteBufUtils.java index 117f7515..9916aec3 100644 --- a/protocol/src/main/java/com/zfoo/protocol/buffer/ByteBufUtils.java +++ b/protocol/src/main/java/com/zfoo/protocol/buffer/ByteBufUtils.java @@ -454,7 +454,7 @@ public abstract class ByteBufUtils { public static Map readIntIntMap(ByteBuf byteBuf) { var length = readInt(byteBuf); - var map = new IntObjectHashMap(length); + var map = new IntObjectHashMap(CollectionUtils.comfortableCapacity(length)); for (var i = 0; i < length; i++) { map.put(readInt(byteBuf), readIntBox(byteBuf)); } @@ -475,7 +475,7 @@ public abstract class ByteBufUtils { public static Map readIntLongMap(ByteBuf byteBuf) { var length = readInt(byteBuf); - var map = new IntObjectHashMap(length); + var map = new IntObjectHashMap(CollectionUtils.comfortableCapacity(length)); for (var i = 0; i < length; i++) { map.put(readInt(byteBuf), readLongBox(byteBuf)); } @@ -496,7 +496,7 @@ public abstract class ByteBufUtils { public static Map readIntStringMap(ByteBuf byteBuf) { var length = readInt(byteBuf); - var map = new IntObjectHashMap(length); + var map = new IntObjectHashMap(CollectionUtils.comfortableCapacity(length)); for (var i = 0; i < length; i++) { map.put(readInt(byteBuf), readString(byteBuf)); } @@ -517,7 +517,7 @@ public abstract class ByteBufUtils { public static Map readIntPacketMap(ByteBuf byteBuf, IProtocolRegistration protocolRegistration) { var length = readInt(byteBuf); - var map = new IntObjectHashMap(length); + var map = new IntObjectHashMap(CollectionUtils.comfortableCapacity(length)); for (var i = 0; i < length; i++) { map.put(readInt(byteBuf), (IPacket) protocolRegistration.read(byteBuf)); } @@ -538,7 +538,7 @@ public abstract class ByteBufUtils { public static Map readLongIntMap(ByteBuf byteBuf) { var length = readInt(byteBuf); - var map = new LongObjectHashMap(length); + var map = new LongObjectHashMap(CollectionUtils.comfortableCapacity(length)); for (var i = 0; i < length; i++) { map.put(readLong(byteBuf), readIntBox(byteBuf)); } @@ -559,7 +559,7 @@ public abstract class ByteBufUtils { public static Map readLongLongMap(ByteBuf byteBuf) { var length = readInt(byteBuf); - var map = new LongObjectHashMap(length); + var map = new LongObjectHashMap(CollectionUtils.comfortableCapacity(length)); for (var i = 0; i < length; i++) { map.put(readLong(byteBuf), readLongBox(byteBuf)); } @@ -580,7 +580,7 @@ public abstract class ByteBufUtils { public static Map readLongStringMap(ByteBuf byteBuf) { var length = readInt(byteBuf); - var map = new LongObjectHashMap(length); + var map = new LongObjectHashMap(CollectionUtils.comfortableCapacity(length)); for (var i = 0; i < length; i++) { map.put(readLong(byteBuf), readString(byteBuf)); } @@ -601,7 +601,7 @@ public abstract class ByteBufUtils { public static Map readLongPacketMap(ByteBuf byteBuf, IProtocolRegistration protocolRegistration) { var length = readInt(byteBuf); - var map = new LongObjectHashMap(length); + var map = new LongObjectHashMap(CollectionUtils.comfortableCapacity(length)); for (var i = 0; i < length; i++) { map.put(readLong(byteBuf), (IPacket) protocolRegistration.read(byteBuf)); } @@ -846,7 +846,7 @@ public abstract class ByteBufUtils { public static Set readByteSet(ByteBuf byteBuf) { var length = readInt(byteBuf); - var set = new HashByteSet(length); + var set = new HashByteSet(CollectionUtils.comfortableCapacity(length)); for (var i = 0; i < length; i++) { set.add(readByte(byteBuf)); } @@ -932,7 +932,7 @@ public abstract class ByteBufUtils { public static Set readShortSet(ByteBuf byteBuf) { var length = readInt(byteBuf); - var set = new HashShortSet(length); + var set = new HashShortSet(CollectionUtils.comfortableCapacity(length)); for (var i = 0; i < length; i++) { set.add(readShort(byteBuf)); } @@ -1011,7 +1011,7 @@ public abstract class ByteBufUtils { public static Set readIntSet(ByteBuf byteBuf) { var length = readInt(byteBuf); - var set = new HashIntSet(length); + var set = new HashIntSet(CollectionUtils.comfortableCapacity(length)); for (var i = 0; i < length; i++) { set.add(readInt(byteBuf)); } @@ -1090,7 +1090,7 @@ public abstract class ByteBufUtils { public static Set readLongSet(ByteBuf byteBuf) { var length = readInt(byteBuf); - var set = new HashLongSet(length); + var set = new HashLongSet(CollectionUtils.comfortableCapacity(length)); for (var i = 0; i < length; i++) { set.add(readLong(byteBuf)); } diff --git a/protocol/src/main/java/com/zfoo/protocol/collection/CollectionUtils.java b/protocol/src/main/java/com/zfoo/protocol/collection/CollectionUtils.java index f73d1fec..4707e357 100644 --- a/protocol/src/main/java/com/zfoo/protocol/collection/CollectionUtils.java +++ b/protocol/src/main/java/com/zfoo/protocol/collection/CollectionUtils.java @@ -88,27 +88,12 @@ public abstract class CollectionUtils { return size <= 0 ? Collections.EMPTY_MAP : new HashMap<>(comfortableCapacity(size)); } - /** - * The largest power of two that can be represented as an {@code int}. - */ - public static final int MAX_POWER_OF_TWO = 1 << (Integer.SIZE - 2); /** * 计算HashMap初始化合适的大小 - *

- * from com.google.common.collect.Maps.capacity() */ public static int comfortableCapacity(int expectedSize) { - if (expectedSize < 3) { - return expectedSize + 1; - } - - if (expectedSize < MAX_POWER_OF_TWO) { - return (int) ((float) expectedSize / 0.75F + 1.0F); - } - - // any large value - return Integer.MAX_VALUE; + return expectedSize < 8 ? 16 : expectedSize << 1; } // ----------------------------------归并排序----------------------------------