Skip to content

Commit

Permalink
perf[protocol]: prediction Length
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysunxiao committed Sep 27, 2023
1 parent 72bb9c7 commit e06bd09
Show file tree
Hide file tree
Showing 17 changed files with 83 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ default Object defaultValue() {
return serializer().defaultValue(this);
}

default int predictionLength() {
return serializer().predictionLength(this);
}

ISerializer serializer();

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,9 @@ public Object defaultValue(IFieldRegistration fieldRegistration) {
return Byte.valueOf((byte) 0);
}

@Override
public int predictionLength(IFieldRegistration fieldRegistration) {
return 1;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,6 @@ public interface ISerializer {

Object defaultValue(IFieldRegistration fieldRegistration);

int predictionLength(IFieldRegistration fieldRegistration);

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit e06bd09

Please sign in to comment.