Skip to content

Commit

Permalink
refactor: major refactor of byte_type (#21)
Browse files Browse the repository at this point in the history
Co-authored-by: Elias Sjögreen <eliassjogreen1@gmail.com>
  • Loading branch information
MierenManz and eliassjogreen authored Jan 5, 2024
1 parent 9e6e56d commit 796f19a
Show file tree
Hide file tree
Showing 138 changed files with 2,156 additions and 2,339 deletions.
115 changes: 115 additions & 0 deletions benchmarks/popular_encodings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import { InnerType, Strings, Struct, u32, u8 } from "../mod.ts";
import {
decode as msgpackRead,
encode as msgpackWrite,
} from "https://deno.land/std@0.208.0/msgpack/mod.ts";

const descriptor = {
handIndex: u8,
fieldIndex: u8,
card: new Struct({
name: new Strings.FixedLength(11),
hp: u8,
damage: u8,
shield: u32,
}),
};

const codec = new Struct(descriptor);

const data: InnerType<typeof codec> = {
handIndex: 255,
fieldIndex: 255,
card: {
name: "InvalidCard",
hp: 255,
damage: 255,
shield: 255,
},
};

const jsonString = JSON.stringify(data);
const msgPackBuff = msgpackWrite(data);

const ARRAY_BUFFER = new ArrayBuffer(20);
const DATA_VIEW = new DataView(ARRAY_BUFFER);

Deno.bench("nop", () => {});

Deno.bench({
name: "JSON (Write)",
group: "write",
baseline: true,
fn: () => {
JSON.stringify(data);
},
});

Deno.bench({
name: "JSON (Read)",
group: "read",
baseline: true,
fn: () => {
JSON.parse(jsonString);
},
});

Deno.bench({
name: "JSON (Roundtrip)",
group: "roundtrip",
baseline: true,
fn: () => {
JSON.stringify(data);
JSON.parse(jsonString);
},
});

Deno.bench({
name: "Struct (Write)",
group: "write",
fn: () => {
codec.writeUnaligned(data, DATA_VIEW);
},
});

Deno.bench({
name: "Struct (Read)",
group: "read",
fn: () => {
codec.readUnaligned(DATA_VIEW);
},
});

Deno.bench({
name: "Struct (Roundtrip)",
group: "roundtrip",
fn: () => {
codec.writeUnaligned(data, DATA_VIEW);
codec.readUnaligned(DATA_VIEW);
},
});

Deno.bench({
name: "MsgPack (Write)",
group: "write",
fn: () => {
msgpackWrite(data);
},
});

Deno.bench({
name: "MsgPack (Read)",
group: "read",
fn: () => {
msgpackRead(msgPackBuff);
},
});

Deno.bench({
name: "MsgPack (Roundtrip)",
group: "roundtrip",
fn: () => {
msgpackWrite(data);
msgpackRead(msgPackBuff);
},
});
4 changes: 2 additions & 2 deletions benchmarks/string.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FixedLengthString } from "../mod.ts";
import { Strings } from "../mod.ts";

const stringThing = new FixedLengthString(12);
const stringThing = new Strings.FixedLength(12);

const ab = new TextEncoder().encode("Hello World!").buffer;
const dt = new DataView(ab);
Expand Down
23 changes: 2 additions & 21 deletions benchmarks/struct.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { AlignedStruct, u32 } from "../mod.ts";
import { Struct, u32 } from "../mod.ts";

const data = new DataView(new ArrayBuffer(8));

const object = { a: 123, b: 456 };
const struct = new AlignedStruct({
const struct = new Struct({
a: u32,
b: u32,
});
const view = struct.view(data);

Deno.bench("no-op", () => {});

Expand All @@ -29,15 +28,6 @@ Deno.bench({
},
});

Deno.bench({
name: "view",
group: "read",
fn: () => {
view.a;
view.b;
},
});

Deno.bench({
name: "DataView",
group: "read",
Expand Down Expand Up @@ -68,15 +58,6 @@ Deno.bench({
},
});

Deno.bench({
name: "view",
group: "write",
fn: () => {
view.a = 0xffff;
view.b = 0xffff;
},
});

Deno.bench({
name: "DataView",
group: "write",
Expand Down
10 changes: 0 additions & 10 deletions deno.json

This file was deleted.

21 changes: 0 additions & 21 deletions deno.lock

This file was deleted.

13 changes: 7 additions & 6 deletions examples/basic.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { PackedStruct, u32, u8 } from "../types/mod.ts";
import { Struct, u32, u8 } from "../src/mod.ts";

const o = new PackedStruct({ "b": u8, "a": u32 }).view(
new DataView(new ArrayBuffer(5)),
0,
);
const buffer = new ArrayBuffer(8);
const dt = new DataView(buffer);

console.log(o.valueOf());
const o = new Struct({ "b": u8, "a": u32 });

o.write({ b: 8, a: 32 }, dt);
console.log(o.read(dt));
3 changes: 1 addition & 2 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from "./types/mod.ts";
export * from "./utils.ts";
export * from "./src/mod.ts";
43 changes: 0 additions & 43 deletions scripts/build_i64leb128.ts

This file was deleted.

65 changes: 65 additions & 0 deletions src/array/array.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { type Options, UnsizedType } from "../types/mod.ts";

export class ArrayType<T> extends UnsizedType<T[]> {
constructor(readonly type: UnsizedType<T>, readonly length: number) {
super(type.byteAlignment);
}

readPacked(dt: DataView, options: Options = { byteOffset: 0 }): T[] {
if (this.length === 0) return [];
const result = [];
result.length = this.length;

for (let i = 0; i < this.length; i++) {
result[i] = this.type.readPacked(dt, options);
// No need for the increment offset. This is handled by the `type.readPacked` function
}

return result;
}

read(dt: DataView, options: Options = { byteOffset: 0 }): T[] {
if (this.length === 0) return [];
const result: unknown[] = [];
result.length = this.length;

for (let i = 0; i < this.length; i++) {
result[i] = this.type.read(dt, options);
// No need for the increment offset. This is handled by the `type.read` function
}

return result as T[];
}

writePacked(
value: T[],
dt: DataView,
options: Options = { byteOffset: 0 },
): void {
if (value.length !== this.length) {
throw new TypeError("T[].length !== ArrayType<T>.length");
}
if (value.length === 0) return;

for (let i = 0; i < this.length; i++) {
this.type.writePacked(value[i], dt, options);
// No need for the increment offset. This is handled by the `type.writePacked` function
}
}

write(
value: T[],
dt: DataView,
options: Options = { byteOffset: 0 },
): void {
if (value.length !== this.length) {
throw new TypeError("T[].length !== ArrayType<T>.length");
}
if (value.length === 0) return;

for (let i = 0; i < this.length; i++) {
this.type.write(value[i], dt, options);
// No need for the increment offset. This is handled by the `type.write` function
}
}
}
47 changes: 47 additions & 0 deletions src/array/array_buffer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { type Options, SizedType } from "../types/mod.ts";

export class ArrayBufferType extends SizedType<ArrayBuffer> {
constructor(byteSize: number, byteAlignment = 1) {
super(byteSize, byteAlignment);
}

readPacked(
dt: DataView,
options: Options = { byteOffset: 0 },
): ArrayBuffer {
super.rangeCheck(dt.byteLength, options.byteOffset);

const resultAB = new ArrayBuffer(this.byteSize);
const resultView = new Uint8Array(resultAB);

resultView.set(
new Uint8Array(
dt.buffer,
dt.byteOffset + options.byteOffset,
this.byteSize,
),
);

super.incrementOffset(options);

return resultAB;
}

writePacked(
value: ArrayBuffer,
dt: DataView,
options: Options = { byteOffset: 0 },
): void {
super.rangeCheck(dt.byteLength, options.byteOffset);

const view = new Uint8Array(
dt.buffer,
dt.byteOffset + options.byteOffset,
this.byteSize,
);

view.set(new Uint8Array(value));

super.incrementOffset(options);
}
}
Loading

0 comments on commit 796f19a

Please sign in to comment.