Skip to content

Commit

Permalink
perf: Optimize Tuple Read & Write (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
MierenManz authored Jan 10, 2024
1 parent 38e95b9 commit 3ece55a
Showing 1 changed file with 28 additions and 13 deletions.
41 changes: 28 additions & 13 deletions src/compound/tuple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,32 @@ export class Tuple<
V extends [...unknown[]] = { [I in keyof T]: InnerType<T[I]> },
> extends UnsizedType<V> {
#tupleTypes: T;

#length: number;
constructor(types: T) {
super(getBiggestAlignment(types));
this.#tupleTypes = types;
this.#length = types.length;
}

readPacked(dt: DataView, options: Options = { byteOffset: 0 }): V {
if (this.#tupleTypes.length === 0) return [] as unknown as V;

const result: unknown[] = [];
result.length = this.#tupleTypes.length;
if (this.#length === 0) return [] as unknown as V;
const result: unknown[] = new Array(this.#length);

const tupleTypes = this.#tupleTypes;
for (let i = 0; i < result.length; i++) {
result[i] = this.#tupleTypes[i].readPacked(dt, options);
result[i] = tupleTypes[i].readPacked(dt, options);
}

return result as V;
}

read(dt: DataView, options: Options = { byteOffset: 0 }): V {
const result: unknown[] = [];
result.length = this.#tupleTypes.length;
if (this.#length === 0) return [] as unknown as V;
const result: unknown[] = new Array(this.#length);

const tupleTypes = this.#tupleTypes;
for (let i = 0; i < result.length; i++) {
result[i] = this.#tupleTypes[i].read(dt, options);
result[i] = tupleTypes[i].read(dt, options);
}

return result as V;
Expand All @@ -41,16 +42,30 @@ export class Tuple<
dt: DataView,
options: Options = { byteOffset: 0 },
): void {
if (value.length !== this.#length) {
throw new TypeError(
`value V has more entries than expected\nExpected:${this.#length} but got ${value.length}`,
);
}
if (value.length === 0) return;

for (let i = 0; i < this.#tupleTypes.length; i++) {
this.#tupleTypes[i].writePacked(value[i], dt, options);
const tupleTypes = this.#tupleTypes;
for (let i = 0; i < value.length; i++) {
tupleTypes[i].writePacked(value[i], dt, options);
}
}

write(value: V, dt: DataView, options: Options = { byteOffset: 0 }): void {
for (let i = 0; i < this.#tupleTypes.length; i++) {
this.#tupleTypes[i].write(value[i], dt, options);
if (value.length !== this.#length) {
throw new TypeError(
`value V has more entries than expected\nExpected:${this.#length} but got ${value.length}`,
);
}
if (value.length === 0) return;

const tupleTypes = this.#tupleTypes;
for (let i = 0; i < value.length; i++) {
tupleTypes[i].write(value[i], dt, options);
}
}
}

0 comments on commit 3ece55a

Please sign in to comment.