mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-08-23 22:27:28 +00:00
perf[protocol]: 统一反序列化为空的可变集合语义
This commit is contained in:
@@ -430,7 +430,7 @@ public abstract class ByteBufUtils {
|
||||
|
||||
public static Set<IPacket> readPacketSet(ByteBuf byteBuf, IProtocolRegistration protocolRegistration) {
|
||||
var length = readInt(byteBuf);
|
||||
var set = (Set<IPacket>) CollectionUtils.newSet(length);
|
||||
Set<IPacket> set = CollectionUtils.newSet(length);
|
||||
for (var i = 0; i < length; i++) {
|
||||
set.add((IPacket) protocolRegistration.read(byteBuf));
|
||||
}
|
||||
@@ -619,7 +619,7 @@ public abstract class ByteBufUtils {
|
||||
|
||||
public static Map<String, Integer> readStringIntMap(ByteBuf byteBuf) {
|
||||
var length = readInt(byteBuf);
|
||||
var map = (Map<String, Integer>) CollectionUtils.newMap(length);
|
||||
Map<String, Integer> map = CollectionUtils.newMap(length);
|
||||
for (var i = 0; i < length; i++) {
|
||||
map.put(readString(byteBuf), readIntBox(byteBuf));
|
||||
}
|
||||
@@ -640,7 +640,7 @@ public abstract class ByteBufUtils {
|
||||
|
||||
public static Map<String, Long> readStringLongMap(ByteBuf byteBuf) {
|
||||
var length = readInt(byteBuf);
|
||||
var map = (Map<String, Long>) CollectionUtils.newMap(length);
|
||||
Map<String, Long> map = CollectionUtils.newMap(length);
|
||||
for (var i = 0; i < length; i++) {
|
||||
map.put(readString(byteBuf), readLongBox(byteBuf));
|
||||
}
|
||||
@@ -661,7 +661,7 @@ public abstract class ByteBufUtils {
|
||||
|
||||
public static Map<String, String> readStringStringMap(ByteBuf byteBuf) {
|
||||
var length = readInt(byteBuf);
|
||||
var map = (Map<String, String>) CollectionUtils.newMap(length);
|
||||
Map<String, String> map = CollectionUtils.newMap(length);
|
||||
for (var i = 0; i < length; i++) {
|
||||
map.put(readString(byteBuf), readString(byteBuf));
|
||||
}
|
||||
@@ -682,7 +682,7 @@ public abstract class ByteBufUtils {
|
||||
|
||||
public static Map<String, IPacket> readStringPacketMap(ByteBuf byteBuf, IProtocolRegistration protocolRegistration) {
|
||||
var length = readInt(byteBuf);
|
||||
var map = (Map<String, IPacket>) CollectionUtils.newMap(length);
|
||||
Map<String, IPacket> map = CollectionUtils.newMap(length);
|
||||
for (var i = 0; i < length; i++) {
|
||||
map.put(readString(byteBuf), (IPacket) protocolRegistration.read(byteBuf));
|
||||
}
|
||||
@@ -760,7 +760,7 @@ public abstract class ByteBufUtils {
|
||||
|
||||
public static Set<Boolean> readBooleanSet(ByteBuf byteBuf) {
|
||||
var length = readInt(byteBuf);
|
||||
var set = (Set<Boolean>) CollectionUtils.newSet(length);
|
||||
Set<Boolean> set = CollectionUtils.newSet(length);
|
||||
for (var i = 0; i < length; i++) {
|
||||
set.add(readBooleanBox(byteBuf));
|
||||
}
|
||||
@@ -1143,7 +1143,7 @@ public abstract class ByteBufUtils {
|
||||
|
||||
public static Set<Float> readFloatSet(ByteBuf byteBuf) {
|
||||
var length = readInt(byteBuf);
|
||||
var set = (Set<Float>) CollectionUtils.newSet(length);
|
||||
Set<Float> set = CollectionUtils.newSet(length);
|
||||
for (var i = 0; i < length; i++) {
|
||||
set.add(readFloatBox(byteBuf));
|
||||
}
|
||||
@@ -1224,7 +1224,7 @@ public abstract class ByteBufUtils {
|
||||
|
||||
public static Set<Double> readDoubleSet(ByteBuf byteBuf) {
|
||||
var length = readInt(byteBuf);
|
||||
var set = (Set<Double>) CollectionUtils.newSet(length);
|
||||
Set<Double> set = CollectionUtils.newSet(length);
|
||||
for (var i = 0; i < length; i++) {
|
||||
set.add(readDoubleBox(byteBuf));
|
||||
}
|
||||
@@ -1282,7 +1282,7 @@ public abstract class ByteBufUtils {
|
||||
|
||||
public static Set<String> readStringSet(ByteBuf byteBuf) {
|
||||
var length = readInt(byteBuf);
|
||||
var set = (Set<String>) CollectionUtils.newSet(length);
|
||||
Set<String> set = CollectionUtils.newSet(length);
|
||||
for (var i = 0; i < length; i++) {
|
||||
set.add(readString(byteBuf));
|
||||
}
|
||||
|
||||
@@ -17,7 +17,6 @@ import com.zfoo.protocol.util.AssertionUtils;
|
||||
import java.lang.reflect.Array;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@@ -194,9 +193,6 @@ public abstract class ArrayUtils {
|
||||
* toList
|
||||
*/
|
||||
public static List<Boolean> toList(boolean[] array) {
|
||||
if (isEmpty(array)) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
var list = new ArrayList<Boolean>();
|
||||
for (var value : array) {
|
||||
list.add(value);
|
||||
@@ -205,9 +201,6 @@ public abstract class ArrayUtils {
|
||||
}
|
||||
|
||||
public static List<Byte> toList(byte[] array) {
|
||||
if (isEmpty(array)) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
var list = new ArrayList<Byte>();
|
||||
for (var value : array) {
|
||||
list.add(value);
|
||||
@@ -216,9 +209,6 @@ public abstract class ArrayUtils {
|
||||
}
|
||||
|
||||
public static List<Short> toList(short[] array) {
|
||||
if (isEmpty(array)) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
var list = new ArrayList<Short>();
|
||||
for (var value : array) {
|
||||
list.add(value);
|
||||
@@ -227,9 +217,6 @@ public abstract class ArrayUtils {
|
||||
}
|
||||
|
||||
public static List<Integer> toList(int[] array) {
|
||||
if (isEmpty(array)) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
var list = new ArrayList<Integer>();
|
||||
for (var j : array) {
|
||||
list.add(j);
|
||||
@@ -238,9 +225,6 @@ public abstract class ArrayUtils {
|
||||
}
|
||||
|
||||
public static List<Long> toList(long[] array) {
|
||||
if (isEmpty(array)) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
var list = new ArrayList<Long>();
|
||||
for (var j : array) {
|
||||
list.add(j);
|
||||
@@ -249,9 +233,6 @@ public abstract class ArrayUtils {
|
||||
}
|
||||
|
||||
public static List<Float> toList(float[] array) {
|
||||
if (isEmpty(array)) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
var list = new ArrayList<Float>();
|
||||
for (var j : array) {
|
||||
list.add(j);
|
||||
@@ -260,9 +241,6 @@ public abstract class ArrayUtils {
|
||||
}
|
||||
|
||||
public static List<Double> toList(double[] array) {
|
||||
if (isEmpty(array)) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
var list = new ArrayList<Double>();
|
||||
for (var j : array) {
|
||||
list.add(j);
|
||||
@@ -271,9 +249,6 @@ public abstract class ArrayUtils {
|
||||
}
|
||||
|
||||
public static List<Character> toList(char[] array) {
|
||||
if (isEmpty(array)) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
var list = new ArrayList<Character>();
|
||||
for (var j : array) {
|
||||
list.add(j);
|
||||
@@ -283,9 +258,9 @@ public abstract class ArrayUtils {
|
||||
|
||||
public static <T> List<T> toList(T[] array) {
|
||||
if (isEmpty(array)) {
|
||||
return Collections.emptyList();
|
||||
return CollectionUtils.emptyList();
|
||||
}
|
||||
return Arrays.asList(array);
|
||||
return new ArrayList<>(Arrays.asList(array));
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -74,16 +74,27 @@ public abstract class CollectionUtils {
|
||||
return isEmpty(map) ? Collections.emptyIterator() : map.entrySet().iterator();
|
||||
}
|
||||
|
||||
public static <T> List<T> emptyList() {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
public static List<?> newList(int size) {
|
||||
public static <T> Set<T> emptySet() {
|
||||
return new HashSet<>();
|
||||
}
|
||||
|
||||
public static <K, V> Map<K, V> emptyMap() {
|
||||
return new HashMap<>();
|
||||
}
|
||||
|
||||
public static <T> List<T> newList(int size) {
|
||||
return size <= 0 ? new ArrayList<>() : new ArrayList<>(comfortableLength(size));
|
||||
}
|
||||
|
||||
public static Set<?> newSet(int size) {
|
||||
public static <T> Set<T> newSet(int size) {
|
||||
return size <= 0 ? new HashSet<>() : new HashSet<>(comfortableCapacity(size));
|
||||
}
|
||||
|
||||
public static Map<?, ?> newMap(int size) {
|
||||
public static <K, V> Map<K, V> newMap(int size) {
|
||||
return size <= 0 ? new HashMap<>() : new HashMap<>(comfortableCapacity(size));
|
||||
}
|
||||
|
||||
@@ -277,7 +288,7 @@ public abstract class CollectionUtils {
|
||||
*/
|
||||
public static <T> List<T> subListLast(List<T> list, int num) {
|
||||
if (isEmpty(list)) {
|
||||
return Collections.emptyList();
|
||||
return emptyList();
|
||||
}
|
||||
|
||||
var startIndex = list.size() - num;
|
||||
|
||||
@@ -58,7 +58,6 @@ public abstract class EnhanceUtils {
|
||||
// 导入需要的包
|
||||
classPool.importPackage(IPacket.class.getCanonicalName());
|
||||
classPool.importPackage(ByteBufUtils.class.getCanonicalName());
|
||||
classPool.importPackage(Collections.class.getCanonicalName());
|
||||
classPool.importPackage(CollectionUtils.class.getCanonicalName());
|
||||
classPool.importPackage(ArrayUtils.class.getCanonicalName());
|
||||
classPool.importPackage(Iterator.class.getCanonicalName());
|
||||
|
||||
@@ -14,12 +14,11 @@
|
||||
package com.zfoo.protocol.serializer.reflect;
|
||||
|
||||
import com.zfoo.protocol.buffer.ByteBufUtils;
|
||||
import com.zfoo.protocol.collection.CollectionUtils;
|
||||
import com.zfoo.protocol.registration.field.IFieldRegistration;
|
||||
import com.zfoo.protocol.registration.field.ListField;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@@ -54,13 +53,9 @@ public class ListSerializer implements ISerializer {
|
||||
|
||||
@Override
|
||||
public Object readObject(ByteBuf buffer, IFieldRegistration fieldRegistration) {
|
||||
int size = ByteBufUtils.readInt(buffer);
|
||||
if (size <= 0) {
|
||||
return Collections.EMPTY_LIST;
|
||||
}
|
||||
ListField listField = (ListField) fieldRegistration;
|
||||
List<Object> list = new ArrayList<>(size);
|
||||
|
||||
var size = ByteBufUtils.readInt(buffer);
|
||||
var listField = (ListField) fieldRegistration;
|
||||
List<Object> list = CollectionUtils.newList(size);
|
||||
for (int i = 0; i < size; i++) {
|
||||
Object value = listField.getListElementRegistration().serializer().readObject(buffer, listField.getListElementRegistration());
|
||||
list.add(value);
|
||||
|
||||
@@ -19,8 +19,6 @@ import com.zfoo.protocol.registration.field.IFieldRegistration;
|
||||
import com.zfoo.protocol.registration.field.MapField;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
@@ -59,13 +57,9 @@ public class MapSerializer implements ISerializer {
|
||||
|
||||
@Override
|
||||
public Object readObject(ByteBuf buffer, IFieldRegistration fieldRegistration) {
|
||||
int size = ByteBufUtils.readInt(buffer);
|
||||
if (size <= 0) {
|
||||
return Collections.EMPTY_MAP;
|
||||
}
|
||||
|
||||
MapField mapField = (MapField) fieldRegistration;
|
||||
Map<Object, Object> map = new HashMap<>(CollectionUtils.comfortableCapacity(size));
|
||||
var size = ByteBufUtils.readInt(buffer);
|
||||
var mapField = (MapField) fieldRegistration;
|
||||
Map<Object, Object> map = CollectionUtils.newMap(size);
|
||||
|
||||
for (int i = 0; i < size; i++) {
|
||||
Object key = mapField.getMapKeyRegistration().serializer().readObject(buffer, mapField.getMapKeyRegistration());
|
||||
|
||||
@@ -19,8 +19,6 @@ import com.zfoo.protocol.registration.field.IFieldRegistration;
|
||||
import com.zfoo.protocol.registration.field.SetField;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
@@ -55,13 +53,9 @@ public class SetSerializer implements ISerializer {
|
||||
|
||||
@Override
|
||||
public Object readObject(ByteBuf buffer, IFieldRegistration fieldRegistration) {
|
||||
int size = ByteBufUtils.readInt(buffer);
|
||||
if (size <= 0) {
|
||||
return Collections.EMPTY_SET;
|
||||
}
|
||||
|
||||
SetField setField = (SetField) fieldRegistration;
|
||||
Set<Object> set = new HashSet<>(CollectionUtils.comfortableCapacity(size));
|
||||
var size = ByteBufUtils.readInt(buffer);
|
||||
var setField = (SetField) fieldRegistration;
|
||||
Set<Object> set = CollectionUtils.newSet(size);
|
||||
|
||||
for (int i = 0; i < size; i++) {
|
||||
Object value = setField.getSetElementRegistration().serializer().readObject(buffer, setField.getSetElementRegistration());
|
||||
|
||||
Reference in New Issue
Block a user