From 38155cd57f3c447870668e4c749c4fba11fd53f5 Mon Sep 17 00:00:00 2001 From: godotg Date: Fri, 23 Sep 2022 23:06:08 +0800 Subject: [PATCH] =?UTF-8?q?perf[set]:=20=E4=BD=BF=E7=94=A8=E6=80=A7?= =?UTF-8?q?=E8=83=BD=E6=9B=B4=E9=AB=98=E7=9A=84FixSet=E5=8F=8D=E5=BA=8F?= =?UTF-8?q?=E5=88=97=E5=8C=96Set=E9=9B=86=E5=90=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../zfoo/protocol/buffer/ByteBufUtils.java | 60 ++++--------------- 1 file changed, 10 insertions(+), 50 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 300d6d92..88f890f5 100644 --- a/protocol/src/main/java/com/zfoo/protocol/buffer/ByteBufUtils.java +++ b/protocol/src/main/java/com/zfoo/protocol/buffer/ByteBufUtils.java @@ -418,9 +418,9 @@ public abstract class ByteBufUtils { public static List readPacketList(ByteBuf byteBuf, IProtocolRegistration protocolRegistration) { var length = readInt(byteBuf); - var list = new FixedSizeList(length); + var list = new FixedSizeList(length); for (var i = 0; i < length; i++) { - list.set(i, protocolRegistration.read(byteBuf)); + list.set(i, (IPacket) protocolRegistration.read(byteBuf)); } return list; } @@ -430,12 +430,7 @@ public abstract class ByteBufUtils { } public static Set readPacketSet(ByteBuf byteBuf, IProtocolRegistration protocolRegistration) { - var length = readInt(byteBuf); - var set = (Set) CollectionUtils.newFixedSet(length); - for (var i = 0; i < length; i++) { - set.add((IPacket) protocolRegistration.read(byteBuf)); - } - return set; + return new FixedHashSet((FixedSizeList) readPacketList(byteBuf, protocolRegistration)); } public static void writeIntIntMap(ByteBuf byteBuf, Map map) { @@ -765,12 +760,7 @@ public abstract class ByteBufUtils { } public static Set readBooleanSet(ByteBuf byteBuf) { - var length = readInt(byteBuf); - var set = (Set) CollectionUtils.newFixedSet(length); - for (var i = 0; i < length; i++) { - set.add(readBooleanBox(byteBuf)); - } - return set; + return new FixedHashSetBoolean((FixedSizeListBoolean) readBooleanList(byteBuf)); } //---------------------------------byte-------------------------------------- @@ -843,12 +833,7 @@ public abstract class ByteBufUtils { } public static Set readByteSet(ByteBuf byteBuf) { - var length = readInt(byteBuf); - var set = (Set) CollectionUtils.newFixedSet(length); - for (var i = 0; i < length; i++) { - set.add(readByteBox(byteBuf)); - } - return set; + return new FixedHashSetByte((FixedSizeListByte) readByteList(byteBuf)); } //---------------------------------short-------------------------------------- @@ -929,12 +914,7 @@ public abstract class ByteBufUtils { } public static Set readShortSet(ByteBuf byteBuf) { - var length = readInt(byteBuf); - var set = (Set) CollectionUtils.newFixedSet(length); - for (var i = 0; i < length; i++) { - set.add(readShortBox(byteBuf)); - } - return set; + return new FixedHashSetShort((FixedSizeListShort) readShortList(byteBuf)); } @@ -1008,12 +988,7 @@ public abstract class ByteBufUtils { } public static Set readIntSet(ByteBuf byteBuf) { - var length = readInt(byteBuf); - var set = (Set) CollectionUtils.newFixedSet(length); - for (var i = 0; i < length; i++) { - set.add(readIntBox(byteBuf)); - } - return set; + return new FixedHashSetInt((FixedSizeListInt) readIntList(byteBuf)); } @@ -1087,12 +1062,7 @@ public abstract class ByteBufUtils { } public static Set readLongSet(ByteBuf byteBuf) { - var length = readInt(byteBuf); - var set = (Set) CollectionUtils.newFixedSet(length); - for (var i = 0; i < length; i++) { - set.add(readLongBox(byteBuf)); - } - return set; + return new FixedHashSetLong((FixedSizeListLong) readLongList(byteBuf)); } //---------------------------------float-------------------------------------- @@ -1173,12 +1143,7 @@ public abstract class ByteBufUtils { } public static Set readFloatSet(ByteBuf byteBuf) { - var length = readInt(byteBuf); - var set = (Set) CollectionUtils.newFixedSet(length); - for (var i = 0; i < length; i++) { - set.add(readFloatBox(byteBuf)); - } - return set; + return new FixedHashSetFloat((FixedSizeListFloat) readFloatList(byteBuf)); } //---------------------------------double-------------------------------------- @@ -1259,12 +1224,7 @@ public abstract class ByteBufUtils { } public static Set readDoubleSet(ByteBuf byteBuf) { - var length = readInt(byteBuf); - var set = (Set) CollectionUtils.newFixedSet(length); - for (var i = 0; i < length; i++) { - set.add(readDoubleBox(byteBuf)); - } - return set; + return new FixedHashSetDouble((FixedSizeListDouble) readDoubleList(byteBuf)); } //---------------------------------string--------------------------------------