mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-10-18 18:15:46 +00:00
perf[protocol]: prediction Length
This commit is contained in:
1 parent
72bb9c7813
commit
e06bd091cf
17 files changed
+83
-5
No files matched your search
@@ -102,10 +102,7 @@ public class ProtocolManager {
|
|||||||
.filter(Objects::nonNull)
|
.filter(Objects::nonNull)
|
||||||
.filter(it -> it.getName().equals(name))
|
.filter(it -> it.getName().equals(name))
|
||||||
.findFirst();
|
.findFirst();
|
||||||
if (moduleOptional.isEmpty()) {
|
return moduleOptional.orElse(null);
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return moduleOptional.get();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static short protocolId(Class<?> clazz) {
|
public static short protocolId(Class<?> clazz) {
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ public class ProtocolRegistration implements IProtocolRegistration {
|
|||||||
this.fieldRegistrations = fieldRegistrations;
|
this.fieldRegistrations = fieldRegistrations;
|
||||||
|
|
||||||
this.compatible = Arrays.stream(fields).anyMatch(it -> it.isAnnotationPresent(Compatible.class));
|
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
|
@Override
|
||||||
|
|||||||
@@ -26,6 +26,10 @@ public interface IFieldRegistration {
|
|||||||
return serializer().defaultValue(this);
|
return serializer().defaultValue(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
default int predictionLength() {
|
||||||
|
return serializer().predictionLength(this);
|
||||||
|
}
|
||||||
|
|
||||||
ISerializer serializer();
|
ISerializer serializer();
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -74,4 +74,11 @@ public class ArraySerializer implements ISerializer {
|
|||||||
return Array.newInstance(arrayField.getType(), 0);
|
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) {
|
public Object defaultValue(IFieldRegistration fieldRegistration) {
|
||||||
return Boolean.FALSE;
|
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);
|
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) {
|
public Object defaultValue(IFieldRegistration fieldRegistration) {
|
||||||
return ByteBufUtils.ZERO_DOUBLE;
|
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) {
|
public Object defaultValue(IFieldRegistration fieldRegistration) {
|
||||||
return ByteBufUtils.ZERO_FLOAT;
|
return ByteBufUtils.ZERO_FLOAT;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int predictionLength(IFieldRegistration fieldRegistration) {
|
||||||
|
return 4;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -34,4 +34,6 @@ public interface ISerializer {
|
|||||||
|
|
||||||
Object defaultValue(IFieldRegistration fieldRegistration);
|
Object defaultValue(IFieldRegistration fieldRegistration);
|
||||||
|
|
||||||
|
int predictionLength(IFieldRegistration fieldRegistration);
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -38,4 +38,9 @@ public class IntSerializer implements ISerializer {
|
|||||||
public Object defaultValue(IFieldRegistration fieldRegistration) {
|
public Object defaultValue(IFieldRegistration fieldRegistration) {
|
||||||
return Integer.valueOf(0);
|
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) {
|
public Object defaultValue(IFieldRegistration fieldRegistration) {
|
||||||
return new ArrayList<>();
|
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) {
|
public Object defaultValue(IFieldRegistration fieldRegistration) {
|
||||||
return Long.valueOf(0);
|
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) {
|
public Object defaultValue(IFieldRegistration fieldRegistration) {
|
||||||
return new HashMap<>();
|
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) {
|
public Object defaultValue(IFieldRegistration fieldRegistration) {
|
||||||
return null;
|
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.buffer.ByteBufUtils;
|
||||||
import com.zfoo.protocol.collection.CollectionUtils;
|
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.IFieldRegistration;
|
||||||
import com.zfoo.protocol.registration.field.SetField;
|
import com.zfoo.protocol.registration.field.SetField;
|
||||||
import io.netty.buffer.ByteBuf;
|
import io.netty.buffer.ByteBuf;
|
||||||
@@ -70,4 +71,11 @@ public class SetSerializer implements ISerializer {
|
|||||||
return new HashSet<>();
|
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) {
|
public Object defaultValue(IFieldRegistration fieldRegistration) {
|
||||||
return Short.valueOf((short) 0);
|
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) {
|
public Object defaultValue(IFieldRegistration fieldRegistration) {
|
||||||
return StringUtils.EMPTY;
|
return StringUtils.EMPTY;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int predictionLength(IFieldRegistration fieldRegistration) {
|
||||||
|
return 23;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user