Skip to content

Commit

Permalink
perf[protocol]: typescript generate
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysunxiao committed Sep 27, 2023
1 parent d1b6c2d commit 0d051e7
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,6 @@ private static String readObject(ProtocolRegistration registration) {
var fieldRegistration = fieldRegistrations[i];
if (field.isAnnotationPresent(Compatible.class)) {

tsBuilder.append(TAB + TAB).append("if (length !== -1 && buffer.getReadOffset() - beforeReadIndex < length) {").append(LS);
tsBuilder.append(TAB + TAB).append("if (buffer.compatibleRead(beforeReadIndex, length)) {").append(LS);
var compatibleReadObject = tsSerializer(fieldRegistration.serializer()).readObject(tsBuilder, 3, field, fieldRegistration);
tsBuilder.append(TAB + TAB+ TAB).append(StringUtils.format("packet.{} = {};", field.getName(), compatibleReadObject)).append(LS);
Expand Down
12 changes: 6 additions & 6 deletions protocol/src/test/typescript/zfoo/packet/ComplexObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class ComplexObject {
return;
}
const beforeWriteIndex = buffer.getWriteOffset();
buffer.writeInt(1);
buffer.writeInt(36962);
buffer.writeByte(packet.a);
buffer.writeByte(packet.aa);
buffer.writeByteArray(packet.aaa);
Expand Down Expand Up @@ -234,15 +234,15 @@ class ComplexObject {
}
buffer.writeInt(packet.myCompatible);
buffer.writePacket(packet.myObject, 102);
buffer.adjustPadding(1, beforeWriteIndex);
buffer.adjustPadding(36962, beforeWriteIndex);
}

static read(buffer: any): ComplexObject | null {
const length = buffer.readInt();
if (length === 0) {
return null;
}
const readIndex = buffer.getReadOffset();
const beforeReadIndex = buffer.getReadOffset();
const packet = new ComplexObject();
const result19 = buffer.readByte();
packet.a = result19;
Expand Down Expand Up @@ -453,16 +453,16 @@ class ComplexObject {
}
}
packet.sssss = result113;
if (length !== -1 && buffer.getReadOffset() - readIndex < length) {
if (buffer.compatibleRead(beforeReadIndex, length)) {
const result117 = buffer.readInt();
packet.myCompatible = result117;
}
if (length !== -1 && buffer.getReadOffset() - readIndex < length) {
if (buffer.compatibleRead(beforeReadIndex, length)) {
const result118 = buffer.readPacket(102);
packet.myObject = result118;
}
if (length > 0) {
buffer.setReadOffset(readIndex + length);
buffer.setReadOffset(beforeReadIndex + length);
}
return packet;
}
Expand Down
4 changes: 2 additions & 2 deletions protocol/src/test/typescript/zfoo/packet/EmptyObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ class EmptyObject {
if (length === 0) {
return null;
}
const readIndex = buffer.getReadOffset();
const beforeReadIndex = buffer.getReadOffset();
const packet = new EmptyObject();

if (length > 0) {
buffer.setReadOffset(readIndex + length);
buffer.setReadOffset(beforeReadIndex + length);
}
return packet;
}
Expand Down
12 changes: 6 additions & 6 deletions protocol/src/test/typescript/zfoo/packet/NormalObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class NormalObject {
return;
}
const beforeWriteIndex = buffer.getWriteOffset();
buffer.writeInt(1);
buffer.writeInt(857);
buffer.writeByte(packet.a);
buffer.writeByteArray(packet.aaa);
buffer.writeShort(packet.b);
Expand All @@ -57,15 +57,15 @@ class NormalObject {
buffer.writeStringSet(packet.ssss);
buffer.writeInt(packet.outCompatibleValue);
buffer.writeInt(packet.outCompatibleValue2);
buffer.adjustPadding(1, beforeWriteIndex);
buffer.adjustPadding(857, beforeWriteIndex);
}

static read(buffer: any): NormalObject | null {
const length = buffer.readInt();
if (length === 0) {
return null;
}
const readIndex = buffer.getReadOffset();
const beforeReadIndex = buffer.getReadOffset();
const packet = new NormalObject();
const result0 = buffer.readByte();
packet.a = result0;
Expand Down Expand Up @@ -103,16 +103,16 @@ class NormalObject {
packet.s = set16;
const set17 = buffer.readStringSet();
packet.ssss = set17;
if (length !== -1 && buffer.getReadOffset() - readIndex < length) {
if (buffer.compatibleRead(beforeReadIndex, length)) {
const result18 = buffer.readInt();
packet.outCompatibleValue = result18;
}
if (length !== -1 && buffer.getReadOffset() - readIndex < length) {
if (buffer.compatibleRead(beforeReadIndex, length)) {
const result19 = buffer.readInt();
packet.outCompatibleValue2 = result19;
}
if (length > 0) {
buffer.setReadOffset(readIndex + length);
buffer.setReadOffset(beforeReadIndex + length);
}
return packet;
}
Expand Down
14 changes: 3 additions & 11 deletions protocol/src/test/typescript/zfoo/packet/ObjectA.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ class ObjectA {
a: number = 0;
m: Map<number, string> = new Map();
objectB: ObjectB | null = null;
innerCompatibleValue: number = 0;

static PROTOCOL_ID: number = 102;

Expand All @@ -19,34 +18,27 @@ class ObjectA {
buffer.writeInt(0);
return;
}
const beforeWriteIndex = buffer.getWriteOffset();
buffer.writeInt(1);
buffer.writeInt(-1);
buffer.writeInt(packet.a);
buffer.writeIntStringMap(packet.m);
buffer.writePacket(packet.objectB, 103);
buffer.writeInt(packet.innerCompatibleValue);
buffer.adjustPadding(1, beforeWriteIndex);
}

static read(buffer: any): ObjectA | null {
const length = buffer.readInt();
if (length === 0) {
return null;
}
const readIndex = buffer.getReadOffset();
const beforeReadIndex = buffer.getReadOffset();
const packet = new ObjectA();
const result0 = buffer.readInt();
packet.a = result0;
const map1 = buffer.readIntStringMap();
packet.m = map1;
const result2 = buffer.readPacket(103);
packet.objectB = result2;
if (length !== -1 && buffer.getReadOffset() - readIndex < length) {
const result3 = buffer.readInt();
packet.innerCompatibleValue = result3;
}
if (length > 0) {
buffer.setReadOffset(readIndex + length);
buffer.setReadOffset(beforeReadIndex + length);
}
return packet;
}
Expand Down
14 changes: 3 additions & 11 deletions protocol/src/test/typescript/zfoo/packet/ObjectB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
class ObjectB {

flag: boolean = false;
innerCompatibleValue: number = 0;

static PROTOCOL_ID: number = 103;

Expand All @@ -16,28 +15,21 @@ class ObjectB {
buffer.writeInt(0);
return;
}
const beforeWriteIndex = buffer.getWriteOffset();
buffer.writeInt(1);
buffer.writeInt(-1);
buffer.writeBoolean(packet.flag);
buffer.writeInt(packet.innerCompatibleValue);
buffer.adjustPadding(1, beforeWriteIndex);
}

static read(buffer: any): ObjectB | null {
const length = buffer.readInt();
if (length === 0) {
return null;
}
const readIndex = buffer.getReadOffset();
const beforeReadIndex = buffer.getReadOffset();
const packet = new ObjectB();
const result0 = buffer.readBoolean();
packet.flag = result0;
if (length !== -1 && buffer.getReadOffset() - readIndex < length) {
const result1 = buffer.readInt();
packet.innerCompatibleValue = result1;
}
if (length > 0) {
buffer.setReadOffset(readIndex + length);
buffer.setReadOffset(beforeReadIndex + length);
}
return packet;
}
Expand Down
4 changes: 2 additions & 2 deletions protocol/src/test/typescript/zfoo/packet/SimpleObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ class SimpleObject {
if (length === 0) {
return null;
}
const readIndex = buffer.getReadOffset();
const beforeReadIndex = buffer.getReadOffset();
const packet = new SimpleObject();
const result0 = buffer.readInt();
packet.c = result0;
const result1 = buffer.readBoolean();
packet.g = result1;
if (length > 0) {
buffer.setReadOffset(readIndex + length);
buffer.setReadOffset(beforeReadIndex + length);
}
return packet;
}
Expand Down
4 changes: 2 additions & 2 deletions protocol/src/test/typescript/zfoo/packet/VeryBigObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6711,7 +6711,7 @@ class VeryBigObject {
if (length === 0) {
return null;
}
const readIndex = buffer.getReadOffset();
const beforeReadIndex = buffer.getReadOffset();
const packet = new VeryBigObject();
const result0 = buffer.readByte();
packet.a1 = result0;
Expand Down Expand Up @@ -13402,7 +13402,7 @@ class VeryBigObject {
const set3343 = buffer.readStringSet();
packet.ssss9 = set3343;
if (length > 0) {
buffer.setReadOffset(readIndex + length);
buffer.setReadOffset(beforeReadIndex + length);
}
return packet;
}
Expand Down

0 comments on commit 0d051e7

Please sign in to comment.