mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-05-19 21:26:08 +00:00
perf[protocol]: javascript使用set代替简化的array序列化
This commit is contained in:
@@ -12,13 +12,13 @@ const maxInt = 2147483647;
|
||||
const minInt = -2147483648;
|
||||
|
||||
// UTF-8编码与解码
|
||||
// const encoder = new TextEncoder('utf-8');
|
||||
// const decoder = new TextDecoder('utf-8');
|
||||
const encoder = new TextEncoder('utf-8');
|
||||
const decoder = new TextDecoder('utf-8');
|
||||
|
||||
// nodejs的测试环境需要用以下方式特殊处理
|
||||
const util = require('util');
|
||||
const encoder = new util.TextEncoder('utf-8');
|
||||
const decoder = new util.TextDecoder('utf-8');
|
||||
// const util = require('util');
|
||||
// const encoder = new util.TextEncoder('utf-8');
|
||||
// const decoder = new util.TextDecoder('utf-8');
|
||||
|
||||
// 在js中long可以支持的最大值
|
||||
// const maxLong = 9007199254740992;
|
||||
@@ -112,6 +112,12 @@ const ByteBuffer = function() {
|
||||
this.writeOffset += length;
|
||||
};
|
||||
|
||||
this.toBytes = function() {
|
||||
const result = new ArrayBuffer(this.writeOffset);
|
||||
new Uint8Array(result).set(new Uint8Array(this.buffer.slice(0, this.writeOffset)));
|
||||
return result;
|
||||
};
|
||||
|
||||
this.writeByte = function(value) {
|
||||
this.ensureCapacity(1);
|
||||
this.bufferView.setInt8(this.writeOffset, value);
|
||||
@@ -336,12 +342,6 @@ const ByteBuffer = function() {
|
||||
return value;
|
||||
};
|
||||
|
||||
this.toBytes = function() {
|
||||
const result = new ArrayBuffer(this.writeOffset);
|
||||
new Uint8Array(result).set(new Uint8Array(this.buffer.slice(0, this.writeOffset)));
|
||||
return result;
|
||||
};
|
||||
|
||||
this.writePacketFlag = function(value) {
|
||||
const flag = (value === null) || (value === undefined);
|
||||
this.writeBoolean(!flag);
|
||||
@@ -580,6 +580,240 @@ const ByteBuffer = function() {
|
||||
return array;
|
||||
};
|
||||
|
||||
// ---------------------------------------------list-------------------------------------------
|
||||
this.writeBooleanList = function(list) {
|
||||
this.writeBooleanArray(list);
|
||||
};
|
||||
|
||||
this.readBooleanList = function() {
|
||||
return this.readBooleanArray();
|
||||
};
|
||||
|
||||
this.writeByteList = function(list) {
|
||||
this.writeByteArray(list);
|
||||
};
|
||||
|
||||
this.readByteList = function() {
|
||||
return this.readByteArray();
|
||||
};
|
||||
|
||||
this.writeShortList = function(list) {
|
||||
this.writeShortArray(list);
|
||||
};
|
||||
|
||||
this.readShortList = function() {
|
||||
return this.readShortArray();
|
||||
};
|
||||
|
||||
this.writeIntList = function(list) {
|
||||
this.writeIntArray(list);
|
||||
};
|
||||
|
||||
this.readIntList = function() {
|
||||
return this.readIntArray();
|
||||
};
|
||||
|
||||
this.writeLongList = function(list) {
|
||||
this.writeLongArray(list);
|
||||
};
|
||||
|
||||
this.readLongList = function() {
|
||||
return this.readLongArray();
|
||||
};
|
||||
|
||||
this.writeFloatList = function(list) {
|
||||
this.writeFloatArray(list);
|
||||
};
|
||||
|
||||
this.readFloatList = function() {
|
||||
return this.readFloatArray();
|
||||
};
|
||||
|
||||
this.writeDoubleList = function(list) {
|
||||
this.writeDoubleArray(list);
|
||||
};
|
||||
|
||||
this.readDoubleList = function() {
|
||||
return this.readDoubleArray();
|
||||
};
|
||||
|
||||
this.writeStringList = function(list) {
|
||||
this.writeStringArray(list);
|
||||
};
|
||||
|
||||
this.readStringList = function() {
|
||||
return this.readStringArray();
|
||||
};
|
||||
|
||||
this.writeCharList = function(list) {
|
||||
this.writeCharArray(list);
|
||||
};
|
||||
|
||||
this.readCharList = function() {
|
||||
return this.readCharArray();
|
||||
};
|
||||
|
||||
this.writePacketList = function(list, protocolId) {
|
||||
this.writePacketArray(list, protocolId);
|
||||
};
|
||||
|
||||
this.readPacketList = function(protocolId) {
|
||||
return this.readPacketArray(protocolId);
|
||||
};
|
||||
|
||||
// ---------------------------------------------set-------------------------------------------
|
||||
this.writeBooleanSet = function(set) {
|
||||
if (set === null) {
|
||||
this.writeInt(0);
|
||||
} else {
|
||||
this.writeInt(set.size);
|
||||
set.forEach(element => {
|
||||
this.writeBoolean(element);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
this.readBooleanSet = function() {
|
||||
return new Set(this.readBooleanArray());
|
||||
};
|
||||
|
||||
this.writeByteSet = function(set) {
|
||||
if (set === null) {
|
||||
this.writeInt(0);
|
||||
} else {
|
||||
this.writeInt(set.size);
|
||||
set.forEach(element => {
|
||||
this.writeByte(element);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
this.readByteSet = function() {
|
||||
return new Set(this.readByteArray());
|
||||
};
|
||||
|
||||
this.writeShortSet = function(set) {
|
||||
if (set === null) {
|
||||
this.writeInt(0);
|
||||
} else {
|
||||
this.writeInt(set.size);
|
||||
set.forEach(element => {
|
||||
this.writeShort(element);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
this.readShortSet = function() {
|
||||
return new Set(this.readShortArray());
|
||||
};
|
||||
|
||||
this.writeIntSet = function(set) {
|
||||
if (set === null) {
|
||||
this.writeInt(0);
|
||||
} else {
|
||||
this.writeInt(set.size);
|
||||
set.forEach(element => {
|
||||
this.writeInt(element);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
this.readIntSet = function() {
|
||||
return new Set(this.readIntArray());
|
||||
};
|
||||
|
||||
this.writeLongSet = function(set) {
|
||||
if (array === null) {
|
||||
set.writeInt(0);
|
||||
} else {
|
||||
this.writeInt(set.size);
|
||||
set.forEach(element => {
|
||||
this.writeLong(element);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
this.readLongSet = function() {
|
||||
return new Set(this.readLongArray());
|
||||
};
|
||||
|
||||
this.writeFloatSet = function(set) {
|
||||
if (set === null) {
|
||||
this.writeInt(0);
|
||||
} else {
|
||||
this.writeInt(set.size);
|
||||
set.forEach(element => {
|
||||
this.writeFloat(element);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
this.readFloatSet = function() {
|
||||
return new Set(this.readFloatArray());
|
||||
};
|
||||
|
||||
this.writeDoubleSet = function(set) {
|
||||
if (set === null) {
|
||||
this.writeInt(0);
|
||||
} else {
|
||||
this.writeInt(set.size);
|
||||
set.forEach(element => {
|
||||
this.writeDouble(element);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
this.readDoubleSet = function() {
|
||||
return new Set(this.readDoubleArray());
|
||||
};
|
||||
|
||||
this.writeStringSet = function(set) {
|
||||
if (set === null) {
|
||||
this.writeInt(0);
|
||||
} else {
|
||||
this.writeInt(set.size);
|
||||
set.forEach(element => {
|
||||
this.writeString(element);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
this.readStringSet = function() {
|
||||
return new Set(this.readStringArray());
|
||||
};
|
||||
|
||||
this.writeCharSet = function(set) {
|
||||
if (set === null) {
|
||||
this.writeInt(0);
|
||||
} else {
|
||||
this.writeInt(set.size);
|
||||
set.forEach(element => {
|
||||
this.writeChar(element);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
this.readCharSet = function() {
|
||||
return new Set(this.readCharArray());
|
||||
};
|
||||
|
||||
this.writePacketSet = function(set, protocolId) {
|
||||
if (set === null) {
|
||||
this.writeInt(0);
|
||||
} else {
|
||||
const protocolRegistration = ProtocolManager.getProtocol(protocolId);
|
||||
this.writeInt(set.size);
|
||||
set.forEach(element => {
|
||||
protocolRegistration.write(this, element);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
this.readPacketSet = function(protocolId) {
|
||||
return new Set(this.readPacketArray(protocolId));
|
||||
};
|
||||
|
||||
// ---------------------------------------------map-------------------------------------------
|
||||
this.writeIntIntMap = function(map) {
|
||||
if (map === null) {
|
||||
this.writeInt(0);
|
||||
|
||||
@@ -108,7 +108,7 @@ ComplexObject.write = function(buffer, packet) {
|
||||
buffer.writeStringArray(packet.jjj);
|
||||
buffer.writePacket(packet.kk, 102);
|
||||
buffer.writePacketArray(packet.kkk, 102);
|
||||
buffer.writeIntArray(packet.l);
|
||||
buffer.writeIntList(packet.l);
|
||||
if (packet.ll === null) {
|
||||
buffer.writeInt(0);
|
||||
} else {
|
||||
@@ -119,7 +119,7 @@ ComplexObject.write = function(buffer, packet) {
|
||||
} else {
|
||||
buffer.writeInt(element0.length);
|
||||
element0.forEach(element1 => {
|
||||
buffer.writeIntArray(element1);
|
||||
buffer.writeIntList(element1);
|
||||
});
|
||||
}
|
||||
});
|
||||
@@ -129,10 +129,10 @@ ComplexObject.write = function(buffer, packet) {
|
||||
} else {
|
||||
buffer.writeInt(packet.lll.length);
|
||||
packet.lll.forEach(element2 => {
|
||||
buffer.writePacketArray(element2, 102);
|
||||
buffer.writePacketList(element2, 102);
|
||||
});
|
||||
}
|
||||
buffer.writeStringArray(packet.llll);
|
||||
buffer.writeStringList(packet.llll);
|
||||
if (packet.lllll === null) {
|
||||
buffer.writeInt(0);
|
||||
} else {
|
||||
@@ -149,7 +149,7 @@ ComplexObject.write = function(buffer, packet) {
|
||||
buffer.writeInt(packet.mmm.size);
|
||||
packet.mmm.forEach((value5, key4) => {
|
||||
buffer.writePacket(key4, 102);
|
||||
buffer.writeIntArray(value5);
|
||||
buffer.writeIntList(value5);
|
||||
});
|
||||
}
|
||||
if (packet.mmmm === null) {
|
||||
@@ -162,7 +162,7 @@ ComplexObject.write = function(buffer, packet) {
|
||||
} else {
|
||||
buffer.writeInt(key6.length);
|
||||
key6.forEach(element8 => {
|
||||
buffer.writePacketArray(element8, 102);
|
||||
buffer.writePacketList(element8, 102);
|
||||
});
|
||||
}
|
||||
if (value7 === null) {
|
||||
@@ -175,7 +175,7 @@ ComplexObject.write = function(buffer, packet) {
|
||||
} else {
|
||||
buffer.writeInt(element9.length);
|
||||
element9.forEach(element10 => {
|
||||
buffer.writeIntArray(element10);
|
||||
buffer.writeIntList(element10);
|
||||
});
|
||||
}
|
||||
});
|
||||
@@ -205,7 +205,7 @@ ComplexObject.write = function(buffer, packet) {
|
||||
}
|
||||
});
|
||||
}
|
||||
buffer.writeIntArray(packet.s);
|
||||
buffer.writeIntSet(packet.s);
|
||||
if (packet.ss === null) {
|
||||
buffer.writeInt(0);
|
||||
} else {
|
||||
@@ -216,7 +216,7 @@ ComplexObject.write = function(buffer, packet) {
|
||||
} else {
|
||||
buffer.writeInt(element15.size);
|
||||
element15.forEach(element16 => {
|
||||
buffer.writeIntArray(element16);
|
||||
buffer.writeIntList(element16);
|
||||
});
|
||||
}
|
||||
});
|
||||
@@ -226,10 +226,10 @@ ComplexObject.write = function(buffer, packet) {
|
||||
} else {
|
||||
buffer.writeInt(packet.sss.size);
|
||||
packet.sss.forEach(element17 => {
|
||||
buffer.writePacketArray(element17, 102);
|
||||
buffer.writePacketSet(element17, 102);
|
||||
});
|
||||
}
|
||||
buffer.writeStringArray(packet.ssss);
|
||||
buffer.writeStringSet(packet.ssss);
|
||||
if (packet.sssss === null) {
|
||||
buffer.writeInt(0);
|
||||
} else {
|
||||
@@ -319,7 +319,7 @@ ComplexObject.read = function(buffer) {
|
||||
packet.kk = result53;
|
||||
const array54 = buffer.readPacketArray(102);
|
||||
packet.kkk = array54;
|
||||
const list55 = buffer.readIntArray();
|
||||
const list55 = buffer.readIntList();
|
||||
packet.l = list55;
|
||||
const result56 = [];
|
||||
const size57 = buffer.readInt();
|
||||
@@ -329,7 +329,7 @@ ComplexObject.read = function(buffer) {
|
||||
const size60 = buffer.readInt();
|
||||
if (size60 > 0) {
|
||||
for (let index61 = 0; index61 < size60; index61++) {
|
||||
const list62 = buffer.readIntArray();
|
||||
const list62 = buffer.readIntList();
|
||||
result59.push(list62);
|
||||
}
|
||||
}
|
||||
@@ -341,12 +341,12 @@ ComplexObject.read = function(buffer) {
|
||||
const size64 = buffer.readInt();
|
||||
if (size64 > 0) {
|
||||
for (let index65 = 0; index65 < size64; index65++) {
|
||||
const list66 = buffer.readPacketArray(102);
|
||||
const list66 = buffer.readPacketList(102);
|
||||
result63.push(list66);
|
||||
}
|
||||
}
|
||||
packet.lll = result63;
|
||||
const list67 = buffer.readStringArray();
|
||||
const list67 = buffer.readStringList();
|
||||
packet.llll = list67;
|
||||
const result68 = [];
|
||||
const size69 = buffer.readInt();
|
||||
@@ -366,7 +366,7 @@ ComplexObject.read = function(buffer) {
|
||||
if (size75 > 0) {
|
||||
for (let index76 = 0; index76 < size75; index76++) {
|
||||
const result77 = buffer.readPacket(102);
|
||||
const list78 = buffer.readIntArray();
|
||||
const list78 = buffer.readIntList();
|
||||
result74.set(result77, list78);
|
||||
}
|
||||
}
|
||||
@@ -379,7 +379,7 @@ ComplexObject.read = function(buffer) {
|
||||
const size83 = buffer.readInt();
|
||||
if (size83 > 0) {
|
||||
for (let index84 = 0; index84 < size83; index84++) {
|
||||
const list85 = buffer.readPacketArray(102);
|
||||
const list85 = buffer.readPacketList(102);
|
||||
result82.push(list85);
|
||||
}
|
||||
}
|
||||
@@ -391,7 +391,7 @@ ComplexObject.read = function(buffer) {
|
||||
const size90 = buffer.readInt();
|
||||
if (size90 > 0) {
|
||||
for (let index91 = 0; index91 < size90; index91++) {
|
||||
const list92 = buffer.readIntArray();
|
||||
const list92 = buffer.readIntList();
|
||||
result89.push(list92);
|
||||
}
|
||||
}
|
||||
@@ -426,7 +426,7 @@ ComplexObject.read = function(buffer) {
|
||||
}
|
||||
}
|
||||
packet.mmmmm = result93;
|
||||
const set104 = buffer.readIntArray();
|
||||
const set104 = buffer.readIntSet();
|
||||
packet.s = set104;
|
||||
const result105 = new Set();
|
||||
const size106 = buffer.readInt();
|
||||
@@ -436,7 +436,7 @@ ComplexObject.read = function(buffer) {
|
||||
const size109 = buffer.readInt();
|
||||
if (size109 > 0) {
|
||||
for (let index110 = 0; index110 < size109; index110++) {
|
||||
const list111 = buffer.readIntArray();
|
||||
const list111 = buffer.readIntList();
|
||||
result108.add(list111);
|
||||
}
|
||||
}
|
||||
@@ -448,12 +448,12 @@ ComplexObject.read = function(buffer) {
|
||||
const size113 = buffer.readInt();
|
||||
if (size113 > 0) {
|
||||
for (let index114 = 0; index114 < size113; index114++) {
|
||||
const set115 = buffer.readPacketArray(102);
|
||||
const set115 = buffer.readPacketSet(102);
|
||||
result112.add(set115);
|
||||
}
|
||||
}
|
||||
packet.sss = result112;
|
||||
const set116 = buffer.readStringArray();
|
||||
const set116 = buffer.readStringSet();
|
||||
packet.ssss = set116;
|
||||
const result117 = new Set();
|
||||
const size118 = buffer.readInt();
|
||||
|
||||
@@ -39,14 +39,14 @@ NormalObject.write = function(buffer, packet) {
|
||||
buffer.writeBoolean(packet.g);
|
||||
buffer.writeString(packet.jj);
|
||||
buffer.writePacket(packet.kk, 102);
|
||||
buffer.writeIntArray(packet.l);
|
||||
buffer.writeLongArray(packet.ll);
|
||||
buffer.writePacketArray(packet.lll, 102);
|
||||
buffer.writeStringArray(packet.llll);
|
||||
buffer.writeIntList(packet.l);
|
||||
buffer.writeLongList(packet.ll);
|
||||
buffer.writePacketList(packet.lll, 102);
|
||||
buffer.writeStringList(packet.llll);
|
||||
buffer.writeIntStringMap(packet.m);
|
||||
buffer.writeIntPacketMap(packet.mm, 102);
|
||||
buffer.writeIntArray(packet.s);
|
||||
buffer.writeStringArray(packet.ssss);
|
||||
buffer.writeIntSet(packet.s);
|
||||
buffer.writeStringSet(packet.ssss);
|
||||
};
|
||||
|
||||
NormalObject.read = function(buffer) {
|
||||
@@ -74,21 +74,21 @@ NormalObject.read = function(buffer) {
|
||||
packet.jj = result8;
|
||||
const result9 = buffer.readPacket(102);
|
||||
packet.kk = result9;
|
||||
const list10 = buffer.readIntArray();
|
||||
const list10 = buffer.readIntList();
|
||||
packet.l = list10;
|
||||
const list11 = buffer.readLongArray();
|
||||
const list11 = buffer.readLongList();
|
||||
packet.ll = list11;
|
||||
const list12 = buffer.readPacketArray(102);
|
||||
const list12 = buffer.readPacketList(102);
|
||||
packet.lll = list12;
|
||||
const list13 = buffer.readStringArray();
|
||||
const list13 = buffer.readStringList();
|
||||
packet.llll = list13;
|
||||
const map14 = buffer.readIntStringMap();
|
||||
packet.m = map14;
|
||||
const map15 = buffer.readIntPacketMap(102);
|
||||
packet.mm = map15;
|
||||
const set16 = buffer.readIntArray();
|
||||
const set16 = buffer.readIntSet();
|
||||
packet.s = set16;
|
||||
const set17 = buffer.readStringArray();
|
||||
const set17 = buffer.readStringSet();
|
||||
packet.ssss = set17;
|
||||
return packet;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user