-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: major refactor of byte_type (#21)
Co-authored-by: Elias Sjögreen <eliassjogreen1@gmail.com>
- Loading branch information
1 parent
9e6e56d
commit 796f19a
Showing
138 changed files
with
2,156 additions
and
2,339 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.