From e06bd091cf24e2fc8a5b224ba48dd3413d500970 Mon Sep 17 00:00:00 2001 From: sun Date: Wed, 27 Sep 2023 15:34:27 +0800 Subject: [PATCH] perf[protocol]: prediction Length --- .../src/main/java/com/zfoo/protocol/ProtocolManager.java | 5 +---- .../zfoo/protocol/registration/ProtocolRegistration.java | 2 +- .../protocol/registration/field/IFieldRegistration.java | 4 ++++ .../zfoo/protocol/serializer/reflect/ArraySerializer.java | 7 +++++++ .../protocol/serializer/reflect/BooleanSerializer.java | 5 +++++ .../zfoo/protocol/serializer/reflect/ByteSerializer.java | 5 +++++ .../protocol/serializer/reflect/DoubleSerializer.java | 5 +++++ .../zfoo/protocol/serializer/reflect/FloatSerializer.java | 5 +++++ .../com/zfoo/protocol/serializer/reflect/ISerializer.java | 2 ++ .../zfoo/protocol/serializer/reflect/IntSerializer.java | 5 +++++ .../zfoo/protocol/serializer/reflect/ListSerializer.java | 7 +++++++ .../zfoo/protocol/serializer/reflect/LongSerializer.java | 5 +++++ .../zfoo/protocol/serializer/reflect/MapSerializer.java | 8 ++++++++ .../serializer/reflect/ObjectProtocolSerializer.java | 5 +++++ .../zfoo/protocol/serializer/reflect/SetSerializer.java | 8 ++++++++ .../zfoo/protocol/serializer/reflect/ShortSerializer.java | 5 +++++ .../protocol/serializer/reflect/StringSerializer.java | 5 +++++ 17 files changed, 83 insertions(+), 5 deletions(-) diff --git a/protocol/src/main/java/com/zfoo/protocol/ProtocolManager.java b/protocol/src/main/java/com/zfoo/protocol/ProtocolManager.java index 8e1c78f78..4a68b6fe3 100644 --- a/protocol/src/main/java/com/zfoo/protocol/ProtocolManager.java +++ b/protocol/src/main/java/com/zfoo/protocol/ProtocolManager.java @@ -102,10 +102,7 @@ public static ProtocolModule moduleByModuleName(String name) { .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) { diff --git a/protocol/src/main/java/com/zfoo/protocol/registration/ProtocolRegistration.java b/protocol/src/main/java/com/zfoo/protocol/registration/ProtocolRegistration.java index bfb09f84f..db8c241fc 100644 --- a/protocol/src/main/java/com/zfoo/protocol/registration/ProtocolRegistration.java +++ b/protocol/src/main/java/com/zfoo/protocol/registration/ProtocolRegistration.java @@ -52,7 +52,7 @@ public ProtocolRegistration(short id, byte module, Constructor constructor, F 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 diff --git a/protocol/src/main/java/com/zfoo/protocol/registration/field/IFieldRegistration.java b/protocol/src/main/java/com/zfoo/protocol/registration/field/IFieldRegistration.java index caca30321..e97d620e6 100644 --- a/protocol/src/main/java/com/zfoo/protocol/registration/field/IFieldRegistration.java +++ b/protocol/src/main/java/com/zfoo/protocol/registration/field/IFieldRegistration.java @@ -26,6 +26,10 @@ default Object defaultValue() { return serializer().defaultValue(this); } + default int predictionLength() { + return serializer().predictionLength(this); + } + ISerializer serializer(); } diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/reflect/ArraySerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/reflect/ArraySerializer.java index ef719e522..bf12bf7ce 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/reflect/ArraySerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/reflect/ArraySerializer.java @@ -74,4 +74,11 @@ public Object defaultValue(IFieldRegistration fieldRegistration) { 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; + } + } diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/reflect/BooleanSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/reflect/BooleanSerializer.java index 51a517b28..18611f175 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/reflect/BooleanSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/reflect/BooleanSerializer.java @@ -38,4 +38,9 @@ public Object readObject(ByteBuf buffer, IFieldRegistration fieldRegistration) { public Object defaultValue(IFieldRegistration fieldRegistration) { return Boolean.FALSE; } + + @Override + public int predictionLength(IFieldRegistration fieldRegistration) { + return 1; + } } diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/reflect/ByteSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/reflect/ByteSerializer.java index cddc43df6..97c6dfb68 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/reflect/ByteSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/reflect/ByteSerializer.java @@ -39,4 +39,9 @@ public Object defaultValue(IFieldRegistration fieldRegistration) { return Byte.valueOf((byte) 0); } + @Override + public int predictionLength(IFieldRegistration fieldRegistration) { + return 1; + } + } diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/reflect/DoubleSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/reflect/DoubleSerializer.java index 1eb1b7161..0d0cd67a2 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/reflect/DoubleSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/reflect/DoubleSerializer.java @@ -39,4 +39,9 @@ public Object readObject(ByteBuf buffer, IFieldRegistration fieldRegistration) { public Object defaultValue(IFieldRegistration fieldRegistration) { return ByteBufUtils.ZERO_DOUBLE; } + + @Override + public int predictionLength(IFieldRegistration fieldRegistration) { + return 8; + } } diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/reflect/FloatSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/reflect/FloatSerializer.java index 6993ae86b..97cf28136 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/reflect/FloatSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/reflect/FloatSerializer.java @@ -38,4 +38,9 @@ public Object readObject(ByteBuf buffer, IFieldRegistration fieldRegistration) { public Object defaultValue(IFieldRegistration fieldRegistration) { return ByteBufUtils.ZERO_FLOAT; } + + @Override + public int predictionLength(IFieldRegistration fieldRegistration) { + return 4; + } } diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/reflect/ISerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/reflect/ISerializer.java index 867d22733..bbb471c42 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/reflect/ISerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/reflect/ISerializer.java @@ -34,4 +34,6 @@ public interface ISerializer { Object defaultValue(IFieldRegistration fieldRegistration); + int predictionLength(IFieldRegistration fieldRegistration); + } diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/reflect/IntSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/reflect/IntSerializer.java index bd8cfc46c..91c12ef7f 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/reflect/IntSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/reflect/IntSerializer.java @@ -38,4 +38,9 @@ public Object readObject(ByteBuf buffer, IFieldRegistration fieldRegistration) { public Object defaultValue(IFieldRegistration fieldRegistration) { return Integer.valueOf(0); } + + @Override + public int predictionLength(IFieldRegistration fieldRegistration) { + return 3; + } } diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/reflect/ListSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/reflect/ListSerializer.java index 4f462d1da..a205b685c 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/reflect/ListSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/reflect/ListSerializer.java @@ -68,4 +68,11 @@ public Object readObject(ByteBuf buffer, IFieldRegistration fieldRegistration) { 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; + } } diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/reflect/LongSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/reflect/LongSerializer.java index 80af2444c..7739dda23 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/reflect/LongSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/reflect/LongSerializer.java @@ -38,4 +38,9 @@ public Object readObject(ByteBuf buffer, IFieldRegistration fieldRegistration) { public Object defaultValue(IFieldRegistration fieldRegistration) { return Long.valueOf(0); } + + @Override + public int predictionLength(IFieldRegistration fieldRegistration) { + return 5; + } } diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/reflect/MapSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/reflect/MapSerializer.java index 7397fdb06..dfe9ea29e 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/reflect/MapSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/reflect/MapSerializer.java @@ -75,4 +75,12 @@ public Object readObject(ByteBuf buffer, IFieldRegistration fieldRegistration) { 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); + } } diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/reflect/ObjectProtocolSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/reflect/ObjectProtocolSerializer.java index b4ecad7f8..2323d88ea 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/reflect/ObjectProtocolSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/reflect/ObjectProtocolSerializer.java @@ -49,4 +49,9 @@ public Object readObject(ByteBuf buffer, IFieldRegistration fieldRegistration) { public Object defaultValue(IFieldRegistration fieldRegistration) { return null; } + + @Override + public int predictionLength(IFieldRegistration fieldRegistration) { + return 13; + } } diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/reflect/SetSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/reflect/SetSerializer.java index b7ccfcaa7..457aea16d 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/reflect/SetSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/reflect/SetSerializer.java @@ -15,6 +15,7 @@ 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 Object defaultValue(IFieldRegistration fieldRegistration) { return new HashSet<>(); } + @Override + public int predictionLength(IFieldRegistration fieldRegistration) { + var setField = (SetField) fieldRegistration; + var length = setField.getSetElementRegistration().serializer().predictionLength(setField.getSetElementRegistration()); + return 9 * length; + } + } diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/reflect/ShortSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/reflect/ShortSerializer.java index 0f2556109..d795ffece 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/reflect/ShortSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/reflect/ShortSerializer.java @@ -38,4 +38,9 @@ public Object readObject(ByteBuf buffer, IFieldRegistration fieldRegistration) { public Object defaultValue(IFieldRegistration fieldRegistration) { return Short.valueOf((short) 0); } + + @Override + public int predictionLength(IFieldRegistration fieldRegistration) { + return 2; + } } diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/reflect/StringSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/reflect/StringSerializer.java index 87f76b85b..29e20a8ca 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/reflect/StringSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/reflect/StringSerializer.java @@ -39,4 +39,9 @@ public Object readObject(ByteBuf buffer, IFieldRegistration fieldRegistration) { public Object defaultValue(IFieldRegistration fieldRegistration) { return StringUtils.EMPTY; } + + @Override + public int predictionLength(IFieldRegistration fieldRegistration) { + return 23; + } }