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; } // ----------------------------------归并排序----------------------------------