Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: major refactor of byte_type #21

Merged
merged 99 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from 82 commits
Commits
Show all changes
99 commits
Select commit Hold shift + click to select a range
0466c92
remove unneeded typecast
MierenManz Mar 15, 2023
bf66d6b
fix: Bug when buffer is split into multiple views
MierenManz Mar 15, 2023
8f4e28c
Merge branch 'main' of https://github.com/denosaurs/byte_type
MierenManz Mar 15, 2023
1ecf8aa
actually fix shit
MierenManz Mar 15, 2023
6c0f3e9
fmt
MierenManz Mar 15, 2023
817f5b2
add varint
MierenManz Apr 12, 2023
d07db3b
x
MierenManz Apr 12, 2023
6916291
add tests
MierenManz Apr 12, 2023
f6cc055
fmt
MierenManz Apr 12, 2023
8ae6735
remove console.log
MierenManz Apr 12, 2023
e64b567
Update types/varint/mod.ts
MierenManz Apr 20, 2023
06a7768
Update types/varint/mod.ts
MierenManz Apr 20, 2023
b366965
rename file
MierenManz Apr 20, 2023
7bd5bab
more tests
MierenManz Apr 20, 2023
cf2a1b4
fmt
MierenManz Apr 20, 2023
b714e64
fix: more descriptive error
eliassjogreen Apr 21, 2023
d60a32e
implement referenced options in primitives
MierenManz Aug 27, 2023
9b49d2c
x
MierenManz Aug 28, 2023
65b3cb8
rename
MierenManz Aug 28, 2023
d678750
x
MierenManz Aug 28, 2023
682cdca
back to bytestride
MierenManz Aug 30, 2023
8ce158f
new way to copy
MierenManz Aug 30, 2023
0f8a61b
Update types/struct/struct.ts
MierenManz Aug 30, 2023
512e46c
Update types/varint/leb128_test.ts
MierenManz Aug 30, 2023
bc17927
Merge branch 'breaking_change_references' of https://github.com/miere…
MierenManz Aug 30, 2023
3e54cf3
Merge branch 'MierenManz-breaking_change_references'
MierenManz Aug 30, 2023
56624fe
Merge branch 'main' of https://github.com/denosaurs/byte_type into br…
MierenManz Aug 30, 2023
1dae61f
yeet
MierenManz Aug 30, 2023
5ec787f
x
MierenManz Aug 30, 2023
2c2f81b
missed varint
MierenManz Aug 30, 2023
2dcbc3b
x
MierenManz Aug 30, 2023
ac042da
x
MierenManz Aug 31, 2023
8ef0a8d
major rewrite
MierenManz Oct 28, 2023
a05ca4d
fmt
MierenManz Oct 28, 2023
57c11cc
fix benchmarks
MierenManz Oct 28, 2023
1ae1743
rewrite basic example
MierenManz Oct 28, 2023
ff1de7e
remove build file
MierenManz Oct 28, 2023
20976f8
remove wabt
MierenManz Oct 28, 2023
ea90db3
update std
MierenManz Oct 28, 2023
b6420ff
add oob check for `Sized<T>`
MierenManz Oct 30, 2023
aff56fe
update lockfile
MierenManz Oct 30, 2023
43aec34
fix alignment issue
MierenManz Oct 30, 2023
8f6d4af
add some tests
MierenManz Oct 30, 2023
bcacb8b
make all types aligned
MierenManz Nov 2, 2023
b859604
more tests
MierenManz Nov 2, 2023
623e9df
fix algo's
MierenManz Nov 2, 2023
a98a171
add varint tests
MierenManz Nov 2, 2023
00347d7
remove dead code
MierenManz Nov 2, 2023
6cb9ce5
optimize
MierenManz Nov 2, 2023
bfc48d2
add array length check
MierenManz Nov 8, 2023
f5cf2a6
pretty error
MierenManz Nov 8, 2023
6775e06
OCD
MierenManz Nov 9, 2023
41de677
nitpick
MierenManz Nov 9, 2023
105ecfc
Revert "OCD"
MierenManz Nov 9, 2023
35b6efc
fix
MierenManz Nov 9, 2023
40de966
add new benchmark
MierenManz Nov 9, 2023
da96d17
fmt
MierenManz Nov 9, 2023
eec9af6
x
MierenManz Nov 9, 2023
c6f4c67
x
MierenManz Nov 9, 2023
b113f0c
x
MierenManz Nov 10, 2023
139c870
plural
MierenManz Nov 10, 2023
b5b8670
fix bench
MierenManz Nov 13, 2023
bb46371
fmt & lint
MierenManz Nov 13, 2023
038d913
Update benchmarks/popular_encodings.ts
MierenManz Nov 13, 2023
ee8a133
Update src/array/typed_array.ts
MierenManz Nov 13, 2023
fd521ca
Update src/compound/tagged_union.ts
MierenManz Nov 13, 2023
a3c1ba6
Update src/primitives/bool.ts
MierenManz Nov 13, 2023
a556294
Update src/varint/i32_leb128.ts
MierenManz Nov 13, 2023
8af2e6c
performance
MierenManz Nov 13, 2023
6bdd0ea
fix
MierenManz Nov 13, 2023
14db3d9
fix
MierenManz Nov 13, 2023
8463667
add copy
MierenManz Nov 13, 2023
fb8b653
add Phantom types
MierenManz Nov 13, 2023
4441c1a
fix tuple alignment
MierenManz Nov 13, 2023
702502b
delete lockfile
MierenManz Nov 13, 2023
2619acd
remove lockfile and config
MierenManz Nov 13, 2023
a83ae84
Update src/compound/tagged_union.ts
MierenManz Nov 13, 2023
d028552
Update src/compound/tagged_union.ts
MierenManz Nov 13, 2023
8fa27f1
tmp
MierenManz Nov 13, 2023
fc2a01b
Merge branch 'major_refactor' of https://github.com/mierenmanz/byte_t…
MierenManz Nov 13, 2023
ed34658
dedupe code
MierenManz Nov 13, 2023
6d66d17
x
MierenManz Nov 13, 2023
dfdcf79
suggestion
MierenManz Nov 16, 2023
92eecff
remove complicated types
MierenManz Nov 16, 2023
36a6403
awful
MierenManz Nov 16, 2023
79d1c19
x
MierenManz Nov 16, 2023
4e97c0b
x
MierenManz Nov 16, 2023
a2e7e9e
fmt
MierenManz Nov 16, 2023
ce4e412
fix
MierenManz Nov 24, 2023
7793dfe
x
MierenManz Nov 24, 2023
f9dfaec
x
MierenManz Dec 8, 2023
e87c637
Merge branch 'major_refactor' of https://github.com/mierenmanz/byte_t…
MierenManz Dec 8, 2023
64552b6
x
MierenManz Dec 8, 2023
4db28b2
x
MierenManz Dec 8, 2023
cd5338d
fmt
MierenManz Dec 8, 2023
dc59a56
x
MierenManz Dec 8, 2023
e81cf1e
x
MierenManz Dec 12, 2023
d11ccf7
Merge branch 'major_refactor' of https://github.com/mierenmanz/byte_t…
MierenManz Jan 1, 2024
4b89548
fmt
MierenManz Jan 1, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 { cstring, InnerType, Struct, u32, u8 } from "../mod.ts";
import {
decode as msgpackRead,
encode as msgpackWrite,
} from "std/msgpack/mod.ts";

const descriptor = {
handIndex: u8,
fieldIndex: u8,
card: new Struct({
name: cstring,
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(codec.byteLength);
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
MierenManz marked this conversation as resolved.
Outdated
Show resolved Hide resolved

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.

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

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

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

const result = [];
result.length = this.length;
result[0] = this.type.readUnaligned(dt, options);
MierenManz marked this conversation as resolved.
Show resolved Hide resolved

for (let i = 1; 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;
}

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

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

return result;
}

writeUnaligned(
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;

this.type.writeUnaligned(value[0], dt, options);
MierenManz marked this conversation as resolved.
Show resolved Hide resolved

for (let i = 1; 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
}
}

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.writeUnaligned(value[i], dt, options);
// No need for the increment offset. This is handled by the `type.writeUnaligned` 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);
}

readUnaligned(
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;
}

writeUnaligned(
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