mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-05-20 07:27:17 +00:00
perf[protocol]: prediction Length
This commit is contained in:
@@ -102,10 +102,7 @@ public class ProtocolManager {
|
||||
.filter(Objects::nonNull)
|
||||
.filter(it -> it.getName().equals(name))
|
||||
.findFirst();
|
||||
if (moduleOptional.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
return moduleOptional.get();
|
||||
return moduleOptional.orElse(null);
|
||||
}
|
||||
|
||||
public static short protocolId(Class<?> clazz) {
|
||||
|
||||
@@ -52,7 +52,7 @@ public class ProtocolRegistration implements IProtocolRegistration {
|
||||
this.fieldRegistrations = fieldRegistrations;
|
||||
|
||||
this.compatible = Arrays.stream(fields).anyMatch(it -> it.isAnnotationPresent(Compatible.class));
|
||||
this.predictionLength = 128;
|
||||
this.predictionLength = Arrays.stream(fieldRegistrations).mapToInt(it -> it.predictionLength()).sum();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -26,6 +26,10 @@ public interface IFieldRegistration {
|
||||
return serializer().defaultValue(this);
|
||||
}
|
||||
|
||||
default int predictionLength() {
|
||||
return serializer().predictionLength(this);
|
||||
}
|
||||
|
||||
ISerializer serializer();
|
||||
|
||||
}
|
||||
|
||||
@@ -74,4 +74,11 @@ public class ArraySerializer implements ISerializer {
|
||||
return Array.newInstance(arrayField.getType(), 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int predictionLength(IFieldRegistration fieldRegistration) {
|
||||
ArrayField arrayField = (ArrayField) fieldRegistration;
|
||||
var length = arrayField.getArrayElementRegistration().serializer().predictionLength(arrayField.getArrayElementRegistration());
|
||||
return 9 * length;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -38,4 +38,9 @@ public class BooleanSerializer implements ISerializer {
|
||||
public Object defaultValue(IFieldRegistration fieldRegistration) {
|
||||
return Boolean.FALSE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int predictionLength(IFieldRegistration fieldRegistration) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,4 +39,9 @@ public class ByteSerializer implements ISerializer {
|
||||
return Byte.valueOf((byte) 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int predictionLength(IFieldRegistration fieldRegistration) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -39,4 +39,9 @@ public class DoubleSerializer implements ISerializer {
|
||||
public Object defaultValue(IFieldRegistration fieldRegistration) {
|
||||
return ByteBufUtils.ZERO_DOUBLE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int predictionLength(IFieldRegistration fieldRegistration) {
|
||||
return 8;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,4 +38,9 @@ public class FloatSerializer implements ISerializer {
|
||||
public Object defaultValue(IFieldRegistration fieldRegistration) {
|
||||
return ByteBufUtils.ZERO_FLOAT;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int predictionLength(IFieldRegistration fieldRegistration) {
|
||||
return 4;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,4 +34,6 @@ public interface ISerializer {
|
||||
|
||||
Object defaultValue(IFieldRegistration fieldRegistration);
|
||||
|
||||
int predictionLength(IFieldRegistration fieldRegistration);
|
||||
|
||||
}
|
||||
|
||||
@@ -38,4 +38,9 @@ public class IntSerializer implements ISerializer {
|
||||
public Object defaultValue(IFieldRegistration fieldRegistration) {
|
||||
return Integer.valueOf(0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int predictionLength(IFieldRegistration fieldRegistration) {
|
||||
return 3;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -68,4 +68,11 @@ public class ListSerializer implements ISerializer {
|
||||
public Object defaultValue(IFieldRegistration fieldRegistration) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int predictionLength(IFieldRegistration fieldRegistration) {
|
||||
ListField listField = (ListField) fieldRegistration;
|
||||
var length = listField.getListElementRegistration().serializer().predictionLength(listField.getListElementRegistration());
|
||||
return 9 * length;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,4 +38,9 @@ public class LongSerializer implements ISerializer {
|
||||
public Object defaultValue(IFieldRegistration fieldRegistration) {
|
||||
return Long.valueOf(0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int predictionLength(IFieldRegistration fieldRegistration) {
|
||||
return 5;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,4 +75,12 @@ public class MapSerializer implements ISerializer {
|
||||
public Object defaultValue(IFieldRegistration fieldRegistration) {
|
||||
return new HashMap<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int predictionLength(IFieldRegistration fieldRegistration) {
|
||||
var mapField = (MapField) fieldRegistration;
|
||||
var keyLength = mapField.getMapKeyRegistration().serializer().predictionLength(mapField.getMapKeyRegistration());
|
||||
var valueLength = mapField.getMapValueRegistration().serializer().predictionLength(mapField.getMapValueRegistration());
|
||||
return 9 * (keyLength + valueLength);
|
||||
}
|
||||
}
|
||||
|
||||
+5
@@ -49,4 +49,9 @@ public class ObjectProtocolSerializer implements ISerializer {
|
||||
public Object defaultValue(IFieldRegistration fieldRegistration) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int predictionLength(IFieldRegistration fieldRegistration) {
|
||||
return 13;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ package com.zfoo.protocol.serializer.reflect;
|
||||
|
||||
import com.zfoo.protocol.buffer.ByteBufUtils;
|
||||
import com.zfoo.protocol.collection.CollectionUtils;
|
||||
import com.zfoo.protocol.registration.field.ArrayField;
|
||||
import com.zfoo.protocol.registration.field.IFieldRegistration;
|
||||
import com.zfoo.protocol.registration.field.SetField;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
@@ -70,4 +71,11 @@ public class SetSerializer implements ISerializer {
|
||||
return new HashSet<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int predictionLength(IFieldRegistration fieldRegistration) {
|
||||
var setField = (SetField) fieldRegistration;
|
||||
var length = setField.getSetElementRegistration().serializer().predictionLength(setField.getSetElementRegistration());
|
||||
return 9 * length;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -38,4 +38,9 @@ public class ShortSerializer implements ISerializer {
|
||||
public Object defaultValue(IFieldRegistration fieldRegistration) {
|
||||
return Short.valueOf((short) 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int predictionLength(IFieldRegistration fieldRegistration) {
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,4 +39,9 @@ public class StringSerializer implements ISerializer {
|
||||
public Object defaultValue(IFieldRegistration fieldRegistration) {
|
||||
return StringUtils.EMPTY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int predictionLength(IFieldRegistration fieldRegistration) {
|
||||
return 23;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user