Skip to content

Commit f1dc88e

Browse files
committed
perf: minor adjustments to how ints and objects are read
1 parent 14f38bd commit f1dc88e

File tree

3 files changed

+28
-26
lines changed

3 files changed

+28
-26
lines changed

src/__benchmark__/benchmark.array.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { bp } from '../index.js';
22

33
const PointParser = bp.object('Point', {
44
x: bp.lu16,
5-
y: bp.lu16,
5+
y: bp.lu32,
66
});
77

88
const PointsParser = bp.object('SimpleObject', {
@@ -11,12 +11,12 @@ const PointsParser = bp.object('SimpleObject', {
1111
});
1212

1313
const n = 1000;
14-
const buf = Buffer.alloc(4 + n * 4);
14+
const buf = Buffer.alloc(4 + n * 6);
1515

1616
buf.writeUInt32LE(n, 0);
1717
for (let i = 0; i < n; i++) {
1818
buf.writeUInt16LE(123, 4 + i * 4);
19-
buf.writeUInt16LE(456, 4 + i * 4 + 2);
19+
buf.writeUInt32LE(456, 4 + i * 4 + 2);
2020
}
2121

2222
for (let i = 0; i < 1_000; i++) {

src/int.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,23 @@ export class UInt8 extends StrutBase<number> {
1313
export class LUInt16 extends StrutBase<number> {
1414
size = 2;
1515
parse(bytes: StrutParserInput, pkt: StrutParserContext): number {
16-
const offset = pkt.offset;
17-
const byteA = bytes[offset];
18-
const byteB = bytes[offset + 1] << 8;
19-
pkt.offset += this.size;
16+
let offset = pkt.offset;
17+
const byteA = bytes[offset++];
18+
const byteB = bytes[offset++] << 8;
19+
pkt.offset += 2;
2020
return byteA | byteB;
2121
}
2222
}
2323

2424
export class LUInt32 extends StrutBase<number> {
2525
size = 4;
2626
parse(bytes: StrutParserInput, pkt: StrutParserContext): number {
27-
const offset = pkt.offset;
28-
const byteA = bytes[offset];
29-
const byteB = bytes[offset + 1] << 8;
30-
const byteC = bytes[offset + 2] << 16;
31-
const byteD = bytes[offset + 3] * 0x1000000;
32-
pkt.offset += this.size;
27+
let offset = pkt.offset;
28+
const byteA = bytes[offset++];
29+
const byteB = bytes[offset++] << 8;
30+
const byteC = bytes[offset++] << 16;
31+
const byteD = bytes[offset++] * 0x1000000;
32+
pkt.offset = offset;
3333
return (byteA | byteB | byteC) + byteD;
3434
}
3535
}

src/object.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -80,33 +80,35 @@ export class StrutTypeArrayOffset<T> extends StrutBase<T[]> {
8080
}
8181
}
8282

83-
export class StrutTypeObject<T extends Record<string, StrutAny>> extends StrutBase<{
84-
[K in keyof T]: StrutInfer<T[K]>;
85-
}> {
83+
export type StrutReturnType<T> = { [K in keyof T]: StrutInfer<T[K]> };
84+
85+
export class StrutTypeObject<T extends Record<string, StrutAny>> extends StrutBase<StrutReturnType<T>> {
8686
type: StrutType<T>;
87-
fields: [string, StrutAny][];
87+
fields: { key: string; parser: StrutAny }[];
8888

8989
constructor(name: string, obj: T) {
9090
super(name);
91-
this.fields = Object.entries(obj);
91+
this.fields = [];
92+
for (const [key, parser] of Object.entries(obj)) {
93+
this.fields.push({ key, parser });
94+
}
9295
}
9396

9497
private _size = -1;
9598
get size(): number {
9699
if (this._size > -1) return this._size;
97100
let size = 0;
98-
for (const ctx of this.fields) size += ctx[1].size;
101+
for (const ctx of this.fields) size += ctx.parser.size;
99102
this._size = size;
100103
return this._size;
101104
}
102105

103-
parse(bytes: StrutParserInput, ctx: StrutParserContext): { [K in keyof T]: StrutInfer<T[K]> } {
104-
const value = {} as any;
105-
for (const [key, parser] of this.fields) {
106-
const res = parser.parse(bytes, ctx);
107-
if (res != null) value[key] = res;
108-
if (ctx.offset > bytes.length) throw new Error(`${this.name}: Buffer Overflow`);
106+
parse(bytes: StrutParserInput, ctx: StrutParserContext): StrutReturnType<T> {
107+
const value = {} as Record<string, unknown>;
108+
for (const kv of this.fields) {
109+
const res = kv.parser.parse(bytes, ctx);
110+
if (res != null) value[kv.key] = res;
109111
}
110-
return value;
112+
return value as StrutReturnType<T>;
111113
}
112114
}

0 commit comments

Comments
 (0)