@colyseus/schema 2.0.35 → 2.0.37
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/cjs/index.js +6 -14
- package/build/cjs/index.js.map +1 -1
- package/build/esm/index.mjs +6 -14
- package/build/esm/index.mjs.map +1 -1
- package/build/umd/index.js +6 -14
- package/lib/Schema.d.ts +1 -1
- package/lib/annotations.js +8 -8
- package/lib/annotations.js.map +1 -1
- package/lib/bench_encode.d.ts +1 -0
- package/lib/bench_encode.js +145 -0
- package/lib/bench_encode.js.map +1 -0
- package/lib/codegen/api.js +1 -2
- package/lib/codegen/api.js.map +1 -1
- package/lib/codegen/languages/cpp.js +1 -2
- package/lib/codegen/languages/cpp.js.map +1 -1
- package/lib/codegen/languages/csharp.js +1 -2
- package/lib/codegen/languages/csharp.js.map +1 -1
- package/lib/codegen/languages/haxe.js +1 -2
- package/lib/codegen/languages/haxe.js.map +1 -1
- package/lib/codegen/languages/java.js +1 -2
- package/lib/codegen/languages/java.js.map +1 -1
- package/lib/codegen/languages/js.js +1 -2
- package/lib/codegen/languages/js.js.map +1 -1
- package/lib/codegen/languages/lua.js +1 -2
- package/lib/codegen/languages/lua.js.map +1 -1
- package/lib/codegen/languages/ts.js +1 -2
- package/lib/codegen/languages/ts.js.map +1 -1
- package/lib/codegen/parser.js +2 -3
- package/lib/codegen/parser.js.map +1 -1
- package/lib/codegen/types.js +3 -3
- package/lib/codegen/types.js.map +1 -1
- package/lib/encoding/decode.js +19 -20
- package/lib/encoding/decode.js.map +1 -1
- package/lib/encoding/encode.js +16 -17
- package/lib/encoding/encode.js.map +1 -1
- package/lib/types/ArraySchema.d.ts +3 -2
- package/lib/types/ArraySchema.js +2 -10
- package/lib/types/ArraySchema.js.map +1 -1
- package/lib/types/CollectionSchema.d.ts +2 -2
- package/lib/types/MapSchema.d.ts +3 -3
- package/lib/types/MapSchema.js +2 -2
- package/lib/types/MapSchema.js.map +1 -1
- package/lib/types/SetSchema.d.ts +2 -2
- package/lib/types/typeRegistry.js +2 -3
- package/lib/types/typeRegistry.js.map +1 -1
- package/lib/types/utils.js +3 -4
- package/lib/types/utils.js.map +1 -1
- package/lib/utils.js +1 -2
- package/lib/utils.js.map +1 -1
- package/lib/v2.d.ts +1 -0
- package/lib/v2.js +141 -0
- package/lib/v2.js.map +1 -0
- package/lib/v2_bench.d.ts +1 -0
- package/lib/v2_bench.js +141 -0
- package/lib/v2_bench.js.map +1 -0
- package/package.json +1 -1
- package/src/annotations.ts +1 -1
- package/src/bench_encode.ts +108 -0
- package/src/types/ArraySchema.ts +4 -2
package/lib/v2_bench.js
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
const index_1 = require("./index");
|
|
13
|
+
// globalThis.interval = setInterval(() => {}, 1000);
|
|
14
|
+
// class Item extends Schema {
|
|
15
|
+
// @type("string") name: string;
|
|
16
|
+
// }
|
|
17
|
+
// class RootState extends Schema {
|
|
18
|
+
// @type([Item]) items = new ArraySchema<Item>();
|
|
19
|
+
// }
|
|
20
|
+
// const state = new RootState();
|
|
21
|
+
// state.items.push(new Item().assign({ name: "hello" }));
|
|
22
|
+
// console.log("Encoded:", state.encode());
|
|
23
|
+
class Vec3 extends index_1.Schema {
|
|
24
|
+
}
|
|
25
|
+
__decorate([
|
|
26
|
+
(0, index_1.type)("number"),
|
|
27
|
+
__metadata("design:type", Number)
|
|
28
|
+
], Vec3.prototype, "x", void 0);
|
|
29
|
+
__decorate([
|
|
30
|
+
(0, index_1.type)("number"),
|
|
31
|
+
__metadata("design:type", Number)
|
|
32
|
+
], Vec3.prototype, "y", void 0);
|
|
33
|
+
__decorate([
|
|
34
|
+
(0, index_1.type)("number"),
|
|
35
|
+
__metadata("design:type", Number)
|
|
36
|
+
], Vec3.prototype, "z", void 0);
|
|
37
|
+
class Base extends index_1.Schema {
|
|
38
|
+
}
|
|
39
|
+
class Entity extends index_1.Schema {
|
|
40
|
+
constructor() {
|
|
41
|
+
super(...arguments);
|
|
42
|
+
this.position = new Vec3().assign({ x: 0, y: 0, z: 0 });
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
__decorate([
|
|
46
|
+
(0, index_1.type)(Vec3),
|
|
47
|
+
__metadata("design:type", Object)
|
|
48
|
+
], Entity.prototype, "position", void 0);
|
|
49
|
+
class Player extends Entity {
|
|
50
|
+
constructor() {
|
|
51
|
+
super(...arguments);
|
|
52
|
+
this.rotation = new Vec3().assign({ x: 0, y: 0, z: 0 });
|
|
53
|
+
this.secret = "private info only for this player";
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
__decorate([
|
|
57
|
+
(0, index_1.type)(Vec3),
|
|
58
|
+
__metadata("design:type", Object)
|
|
59
|
+
], Player.prototype, "rotation", void 0);
|
|
60
|
+
__decorate([
|
|
61
|
+
(0, index_1.type)("string"),
|
|
62
|
+
__metadata("design:type", String)
|
|
63
|
+
], Player.prototype, "secret", void 0);
|
|
64
|
+
class State extends index_1.Schema {
|
|
65
|
+
constructor() {
|
|
66
|
+
super(...arguments);
|
|
67
|
+
// @type({ map: Base }) players = new MapSchema<Entity>();
|
|
68
|
+
this.num = 0;
|
|
69
|
+
this.str = "Hello world!";
|
|
70
|
+
// @type(Entity) entity = new Player().assign({
|
|
71
|
+
// position: new Vec3().assign({ x: 1, y: 2, z: 3 }),
|
|
72
|
+
// rotation: new Vec3().assign({ x: 4, y: 5, z: 6 }),
|
|
73
|
+
// });
|
|
74
|
+
this.entities = new index_1.MapSchema();
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
__decorate([
|
|
78
|
+
(0, index_1.type)("number"),
|
|
79
|
+
__metadata("design:type", Number)
|
|
80
|
+
], State.prototype, "num", void 0);
|
|
81
|
+
__decorate([
|
|
82
|
+
(0, index_1.type)("string"),
|
|
83
|
+
__metadata("design:type", Object)
|
|
84
|
+
], State.prototype, "str", void 0);
|
|
85
|
+
__decorate([
|
|
86
|
+
(0, index_1.type)({ map: Entity }),
|
|
87
|
+
__metadata("design:type", Object)
|
|
88
|
+
], State.prototype, "entities", void 0);
|
|
89
|
+
const state = new State();
|
|
90
|
+
state.entities.set("one", new Player().assign({
|
|
91
|
+
position: new Vec3().assign({ x: 1, y: 2, z: 3 }),
|
|
92
|
+
rotation: new Vec3().assign({ x: 1, y: 2, z: 3 }),
|
|
93
|
+
}));
|
|
94
|
+
state.entities.set("two", new Player().assign({
|
|
95
|
+
position: new Vec3().assign({ x: 4, y: 5, z: 6 }),
|
|
96
|
+
rotation: new Vec3().assign({ x: 7, y: 8, z: 9 }),
|
|
97
|
+
}));
|
|
98
|
+
let encoded = state.encode();
|
|
99
|
+
console.log(`(${encoded.length})`, encoded);
|
|
100
|
+
globalThis.perform = function () {
|
|
101
|
+
for (let i = 0; i < 500000; i++) {
|
|
102
|
+
state.encodeAll();
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
function logTime(label, callback) {
|
|
106
|
+
const time = Date.now();
|
|
107
|
+
for (let i = 0; i < 500000; i++) {
|
|
108
|
+
callback();
|
|
109
|
+
}
|
|
110
|
+
console.log(`${label}:`, Date.now() - time);
|
|
111
|
+
}
|
|
112
|
+
logTime("encode time", () => state.encodeAll());
|
|
113
|
+
const decoded = new State();
|
|
114
|
+
logTime("decode time", () => decoded.decode(encoded));
|
|
115
|
+
// const time = Date.now();
|
|
116
|
+
// console.profile();
|
|
117
|
+
// for (let i = 0; i < 300000; i++) {
|
|
118
|
+
// state.encodeAll();
|
|
119
|
+
// }
|
|
120
|
+
// console.profileEnd();
|
|
121
|
+
// console.log("encode time:", Date.now() - time);
|
|
122
|
+
// const decoded = Reflection.decode(Reflection.encode(state));
|
|
123
|
+
// decoded.decode(encoded);
|
|
124
|
+
//
|
|
125
|
+
// console.log(decoded.toJSON());
|
|
126
|
+
//
|
|
127
|
+
// const rotation = state.entity.rotation;
|
|
128
|
+
// rotation.x = 100;
|
|
129
|
+
//
|
|
130
|
+
// encoded = state.encode();
|
|
131
|
+
// console.log({encoded});
|
|
132
|
+
//
|
|
133
|
+
// decoded.decode(encoded);
|
|
134
|
+
// console.log(decoded.toJSON());
|
|
135
|
+
// const time = Date.now();
|
|
136
|
+
// for (let i = 0; i < 300000; i++) {
|
|
137
|
+
// const state = new State();
|
|
138
|
+
// state.encode();
|
|
139
|
+
// }
|
|
140
|
+
// console.log("encode time:", Date.now() - time);
|
|
141
|
+
//# sourceMappingURL=v2_bench.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"v2_bench.js","sourceRoot":"","sources":["../src/v2_bench.ts"],"names":[],"mappings":";;;;;;;;;;;AAAA,mCAA2E;AAE3E,qDAAqD;AAErD,8BAA8B;AAC9B,oCAAoC;AACpC,IAAI;AAEJ,mCAAmC;AACnC,qDAAqD;AACrD,IAAI;AACJ,iCAAiC;AACjC,0DAA0D;AAE1D,2CAA2C;AAE3C,MAAM,IAAK,SAAQ,cAAM;CAIxB;AAHmB;IAAf,IAAA,YAAI,EAAC,QAAQ,CAAC;;+BAAW;AACV;IAAf,IAAA,YAAI,EAAC,QAAQ,CAAC;;+BAAW;AACV;IAAf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QAAQ,EAAE,CAAC;IACf,CAAC;IACD,OAAO,CAAC,GAAG,CAAC,GAAG,KAAK,GAAG,EAAE,IAAI,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC,CAAC;AAChD,CAAC;AACD,OAAO,CAAC,aAAa,EAAE,GAAG,EAAE,CAAC,KAAK,CAAC,SAAS,EAAE,CAAC,CAAC;AAEhD,MAAM,OAAO,GAAG,IAAI,KAAK,EAAE,CAAC;AAC5B,OAAO,CAAC,aAAa,EAAE,GAAG,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC;AAEtD,2BAA2B;AAC3B,qBAAqB;AACrB,qCAAqC;AACrC,uBAAuB;AACvB,IAAI;AACJ,wBAAwB;AACxB,kDAAkD;AAElD,+DAA+D;AAC/D,2BAA2B;AAC3B,EAAE;AACF,iCAAiC;AACjC,EAAE;AACF,0CAA0C;AAC1C,oBAAoB;AACpB,EAAE;AACF,4BAA4B;AAC5B,0BAA0B;AAC1B,EAAE;AACF,2BAA2B;AAC3B,iCAAiC;AAEjC,2BAA2B;AAC3B,qCAAqC;AACrC,+BAA+B;AAC/B,oBAAoB;AACpB,IAAI;AACJ,kDAAkD","sourcesContent":["import { Schema, type, MapSchema, ArraySchema, Reflection } from \"./index\";\n\n// globalThis.interval = setInterval(() => {}, 1000);\n\n// class Item extends Schema {\n// @type(\"string\") name: string;\n// }\n\n// class RootState extends Schema {\n// @type([Item]) items = new ArraySchema<Item>();\n// }\n// const state = new RootState();\n// state.items.push(new Item().assign({ name: \"hello\" }));\n\n// console.log(\"Encoded:\", state.encode());\n\nclass Vec3 extends Schema {\n @type(\"number\") x: number;\n @type(\"number\") y: number;\n @type(\"number\") z: number;\n}\n\nclass Base extends Schema {}\n\nclass Entity extends Schema {\n @type(Vec3) position = new Vec3().assign({ x: 0, y: 0, z: 0 });\n}\n\nclass Player extends Entity {\n @type(Vec3) rotation = new Vec3().assign({ x: 0, y: 0, z: 0 });\n @type(\"string\") secret: string = \"private info only for this player\";\n}\n\nclass State extends Schema {\n // @type({ map: Base }) players = new MapSchema<Entity>();\n @type(\"number\") num: number = 0;\n @type(\"string\") str = \"Hello world!\";\n // @type(Entity) entity = new Player().assign({\n // position: new Vec3().assign({ x: 1, y: 2, z: 3 }),\n // rotation: new Vec3().assign({ x: 4, y: 5, z: 6 }),\n // });\n @type({ map: Entity }) entities = new MapSchema();\n}\n\nconst state = new State();\n\nstate.entities.set(\"one\", new Player().assign({\n position: new Vec3().assign({ x: 1, y: 2, z: 3 }),\n rotation: new Vec3().assign({ x: 1, y: 2, z: 3 }),\n}));\n\nstate.entities.set(\"two\", new Player().assign({\n position: new Vec3().assign({ x: 4, y: 5, z: 6 }),\n rotation: new Vec3().assign({ x: 7, y: 8, z: 9 }),\n}));\n\nlet encoded = state.encode();\nconsole.log(`(${encoded.length})`, encoded);\n\nglobalThis.perform = function() {\n for (let i = 0; i < 500000; i++) {\n state.encodeAll();\n }\n}\n\nfunction logTime(label: string, callback: Function) {\n const time = Date.now();\n for (let i = 0; i < 500000; i++) {\n callback();\n }\n console.log(`${label}:`, Date.now() - time);\n}\nlogTime(\"encode time\", () => state.encodeAll());\n\nconst decoded = new State();\nlogTime(\"decode time\", () => decoded.decode(encoded));\n\n// const time = Date.now();\n// console.profile();\n// for (let i = 0; i < 300000; i++) {\n// state.encodeAll();\n// }\n// console.profileEnd();\n// console.log(\"encode time:\", Date.now() - time);\n\n// const decoded = Reflection.decode(Reflection.encode(state));\n// decoded.decode(encoded);\n//\n// console.log(decoded.toJSON());\n//\n// const rotation = state.entity.rotation;\n// rotation.x = 100;\n//\n// encoded = state.encode();\n// console.log({encoded});\n//\n// decoded.decode(encoded);\n// console.log(decoded.toJSON());\n\n// const time = Date.now();\n// for (let i = 0; i < 300000; i++) {\n// const state = new State();\n// state.encode();\n// }\n// console.log(\"encode time:\", Date.now() - time);\n\n"]}
|
package/package.json
CHANGED
package/src/annotations.ts
CHANGED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { nanoid } from "nanoid";
|
|
2
|
+
import { Schema, type, MapSchema, ArraySchema } from ".";
|
|
3
|
+
// import * as benchmark from "benchmark";
|
|
4
|
+
|
|
5
|
+
// const suite = new benchmark.Suite();
|
|
6
|
+
|
|
7
|
+
class Attribute extends Schema {
|
|
8
|
+
@type("string") name: string;
|
|
9
|
+
@type("number") value: number;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
class Item extends Schema {
|
|
13
|
+
@type("number") price: number;
|
|
14
|
+
@type([ Attribute ]) attributes = new ArraySchema<Attribute>();
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
class Position extends Schema {
|
|
18
|
+
@type("number") x: number;
|
|
19
|
+
@type("number") y: number;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
class Player extends Schema {
|
|
23
|
+
@type(Position) position = new Position();
|
|
24
|
+
@type({ map: Item }) items = new MapSchema<Item>();
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
class State extends Schema {
|
|
28
|
+
@type({ map: Player }) players = new MapSchema<Player>();
|
|
29
|
+
@type("string") currentTurn;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const state = new State();
|
|
33
|
+
|
|
34
|
+
let now = Date.now();
|
|
35
|
+
|
|
36
|
+
// for (let i = 0; i < 10000; i++) {
|
|
37
|
+
// const player = new Player();
|
|
38
|
+
// state.players.set(`p-${nanoid()}`, player);
|
|
39
|
+
//
|
|
40
|
+
// player.position.x = (i + 1) * 100;
|
|
41
|
+
// player.position.y = (i + 1) * 100;
|
|
42
|
+
// for (let j = 0; j < 10; j++) {
|
|
43
|
+
// const item = new Item();
|
|
44
|
+
// item.price = (i + 1) * 50;
|
|
45
|
+
// for (let k = 0; k < 5; k++) {
|
|
46
|
+
// const attr = new Attribute();
|
|
47
|
+
// attr.name = `Attribute ${k}`;
|
|
48
|
+
// attr.value = k;
|
|
49
|
+
// item.attributes.push(attr);
|
|
50
|
+
//
|
|
51
|
+
// }
|
|
52
|
+
// player.items.set(`item-${j}`, item);
|
|
53
|
+
// }
|
|
54
|
+
// }
|
|
55
|
+
// console.log("time to make changes:", Date.now() - now);
|
|
56
|
+
//
|
|
57
|
+
// process.exit();
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
// measure time to .encodeAll()
|
|
61
|
+
|
|
62
|
+
now = Date.now();
|
|
63
|
+
for (let i = 0; i < 1000; i++) {
|
|
64
|
+
state.encodeAll();
|
|
65
|
+
}
|
|
66
|
+
console.log(Date.now() - now);
|
|
67
|
+
|
|
68
|
+
let avgTimeToEncode = 0;
|
|
69
|
+
let avgTimeToMakeChanges = 0;
|
|
70
|
+
const total = 100;
|
|
71
|
+
|
|
72
|
+
const allEncodes = Date.now();
|
|
73
|
+
for (let i = 0; i < total; i++) {
|
|
74
|
+
now = Date.now();
|
|
75
|
+
for (let j = 0; j < 50; j++) {
|
|
76
|
+
const player = new Player();
|
|
77
|
+
state.players.set(`p-${nanoid()}`, player);
|
|
78
|
+
|
|
79
|
+
player.position.x = (j + 1) * 100;
|
|
80
|
+
player.position.y = (j + 1) * 100;
|
|
81
|
+
for (let k = 0; k < 10; k++) {
|
|
82
|
+
const item = new Item();
|
|
83
|
+
item.price = (j + 1) * 50;
|
|
84
|
+
for (let l = 0; l < 5; l++) {
|
|
85
|
+
const attr = new Attribute();
|
|
86
|
+
attr.name = `Attribute ${l}`;
|
|
87
|
+
attr.value = l;
|
|
88
|
+
item.attributes.push(attr);
|
|
89
|
+
}
|
|
90
|
+
player.items.set(`item-${k}`, item);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
const timeToMakeChanges = Date.now() - now;
|
|
94
|
+
console.log("time to make changes:", timeToMakeChanges);
|
|
95
|
+
avgTimeToMakeChanges += timeToMakeChanges;
|
|
96
|
+
|
|
97
|
+
now = Date.now();
|
|
98
|
+
state.encode();
|
|
99
|
+
const timeToEncode = Date.now() - now;
|
|
100
|
+
console.log("time to encode:", timeToEncode);
|
|
101
|
+
avgTimeToEncode += timeToEncode;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
console.log("avg time to encode:", (avgTimeToEncode) / total);
|
|
105
|
+
console.log("avg time to make changes:", (avgTimeToMakeChanges) / total);
|
|
106
|
+
console.log("time for all encodes:", Date.now() - allEncodes);
|
|
107
|
+
|
|
108
|
+
console.log(Array.from(state.encodeAll()).length, "bytes");
|
package/src/types/ArraySchema.ts
CHANGED
|
@@ -88,7 +88,7 @@ export class ArraySchema<V = any> implements Array<V>, SchemaDecoderCallbacks {
|
|
|
88
88
|
|
|
89
89
|
protected $refId: number = 0;
|
|
90
90
|
|
|
91
|
-
[n: number]: V;
|
|
91
|
+
[n: number]: V | undefined;
|
|
92
92
|
|
|
93
93
|
//
|
|
94
94
|
// Decoding callbacks
|
|
@@ -163,7 +163,7 @@ export class ArraySchema<V = any> implements Array<V>, SchemaDecoderCallbacks {
|
|
|
163
163
|
return value;
|
|
164
164
|
}
|
|
165
165
|
|
|
166
|
-
at(index: number) {
|
|
166
|
+
at(index: number): V | undefined {
|
|
167
167
|
//
|
|
168
168
|
// FIXME: this should be O(1)
|
|
169
169
|
//
|
|
@@ -395,6 +395,8 @@ export class ArraySchema<V = any> implements Array<V>, SchemaDecoderCallbacks {
|
|
|
395
395
|
* @param thisArg An object to which the this keyword can refer in the callbackfn function.
|
|
396
396
|
* If thisArg is omitted, undefined is used as the this value.
|
|
397
397
|
*/
|
|
398
|
+
every<S extends V>(predicate: (value: V, index: number, array: V[]) => value is S, thisArg?: any): this is S[];
|
|
399
|
+
every(callbackfn: (value: V, index: number, array: V[]) => unknown, thisArg?: any): boolean;
|
|
398
400
|
every(callbackfn: (value: V, index: number, array: V[]) => unknown, thisArg?: any): boolean {
|
|
399
401
|
return Array.from(this.$items.values()).every(callbackfn, thisArg);
|
|
400
402
|
}
|