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

Optimization: Remove rangecheck #43

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 0 additions & 4 deletions src/array/array_buffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ export class ArrayBufferType extends SizedType<ArrayBuffer> {
dt: DataView,
options: Options = { byteOffset: 0 },
): ArrayBuffer {
super.rangeCheck(dt.byteLength, options.byteOffset);

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

Expand All @@ -32,8 +30,6 @@ export class ArrayBufferType extends SizedType<ArrayBuffer> {
dt: DataView,
options: Options = { byteOffset: 0 },
): void {
super.rangeCheck(dt.byteLength, options.byteOffset);

const view = new Uint8Array(
dt.buffer,
dt.byteOffset + options.byteOffset,
Expand Down
4 changes: 0 additions & 4 deletions src/array/typed_array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ export class TypedArray<T extends TypedArrays> extends SizedType<T> {
}

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

const value = new this.arrayConstructor(
dt.buffer,
dt.byteOffset + options.byteOffset,
Expand All @@ -56,8 +54,6 @@ export class TypedArray<T extends TypedArrays> extends SizedType<T> {
dt: DataView,
options: Options = { byteOffset: 0 },
): void {
super.rangeCheck(dt.byteLength, options.byteOffset);

const view = new this.arrayConstructor(
dt.buffer,
dt.byteOffset + options.byteOffset,
Expand Down
4 changes: 0 additions & 4 deletions src/bitflags/bitflags16.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ export class BitFlags16<
}

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

const returnObject: Record<string, boolean> = {};

const byteBag = dt.getUint16(options.byteOffset);
Expand All @@ -32,8 +30,6 @@ export class BitFlags16<
dt: DataView,
options: Options = { byteOffset: 0 },
): void {
super.rangeCheck(dt.byteLength, options.byteOffset);

let flags = 0;

for (const { 0: key, 1: flagValue } of this.#recordEntries) {
Expand Down
4 changes: 0 additions & 4 deletions src/bitflags/bitflags32.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ export class BitFlags32<
}

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

const returnObject: Record<string, boolean> = {};

const byteBag = dt.getUint32(options.byteOffset);
Expand All @@ -32,8 +30,6 @@ export class BitFlags32<
dt: DataView,
options: Options = { byteOffset: 0 },
): void {
super.rangeCheck(dt.byteLength, options.byteOffset);

let flags = 0;

for (const { 0: key, 1: flagValue } of this.#recordEntries) {
Expand Down
4 changes: 0 additions & 4 deletions src/bitflags/bitflags64.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ export class BitFlags64<
}

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

const returnObject: Record<string, boolean> = {};

const byteBag = dt.getBigUint64(options.byteOffset);
Expand All @@ -32,8 +30,6 @@ export class BitFlags64<
dt: DataView,
options: Options = { byteOffset: 0 },
): void {
super.rangeCheck(dt.byteLength, options.byteOffset);

let flags = 0n;

for (const { 0: key, 1: flagValue } of this.#recordEntries) {
Expand Down
4 changes: 0 additions & 4 deletions src/bitflags/bitflags8.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ export class BitFlags8<
}

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

const returnObject: Record<string, boolean> = {};

const byteBag = dt.getUint8(options.byteOffset);
Expand All @@ -32,8 +30,6 @@ export class BitFlags8<
dt: DataView,
options: Options = { byteOffset: 0 },
): void {
super.rangeCheck(dt.byteLength, options.byteOffset);

let flags = 0;

for (const { 0: key, 1: flagValue } of this.#recordEntries) {
Expand Down
6 changes: 1 addition & 5 deletions src/primitives/bool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ export class Bool extends SizedType<boolean> {
}

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

const value = Boolean(dt.getInt8(options.byteOffset));
const value = !!dt.getInt8(options.byteOffset);
super.incrementOffset(options);
return value;
}
Expand All @@ -18,8 +16,6 @@ export class Bool extends SizedType<boolean> {
dt: DataView,
options: Options = { byteOffset: 0 },
): void {
super.rangeCheck(dt.byteLength, options.byteOffset);

dt.setInt8(options.byteOffset, Number(value));
super.incrementOffset(options);
}
Expand Down
2 changes: 0 additions & 2 deletions src/primitives/f32.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export class F32 extends SizedType<number> {
}

readPacked(dt: DataView, options: Options = { byteOffset: 0 }): number {
super.rangeCheck(dt.byteLength, options.byteOffset);
const value = dt.getFloat32(options.byteOffset, this.littleEndian);
super.incrementOffset(options);
return value;
Expand All @@ -18,7 +17,6 @@ export class F32 extends SizedType<number> {
dt: DataView,
options: Options = { byteOffset: 0 },
): void {
super.rangeCheck(dt.byteLength, options.byteOffset);
dt.setFloat32(options.byteOffset, value, this.littleEndian);
super.incrementOffset(options);
}
Expand Down
2 changes: 0 additions & 2 deletions src/primitives/f64.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export class F64 extends SizedType<number> {
}

readPacked(dt: DataView, options: Options = { byteOffset: 0 }): number {
super.rangeCheck(dt.byteLength, options.byteOffset);
const value = dt.getFloat64(options.byteOffset, this.littleEndian);
super.incrementOffset(options);
return value;
Expand All @@ -18,7 +17,6 @@ export class F64 extends SizedType<number> {
dt: DataView,
options: Options = { byteOffset: 0 },
): void {
super.rangeCheck(dt.byteLength, options.byteOffset);
dt.setFloat64(options.byteOffset, value, this.littleEndian);
super.incrementOffset(options);
}
Expand Down
2 changes: 0 additions & 2 deletions src/primitives/i16.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export class I16 extends SizedType<number> {
}

readPacked(dt: DataView, options: Options = { byteOffset: 0 }): number {
super.rangeCheck(dt.byteLength, options.byteOffset);
const value = dt.getInt16(options.byteOffset, this.littleEndian);
super.incrementOffset(options);
return value;
Expand All @@ -18,7 +17,6 @@ export class I16 extends SizedType<number> {
dt: DataView,
options: Options = { byteOffset: 0 },
): void {
super.rangeCheck(dt.byteLength, options.byteOffset);
dt.setInt16(options.byteOffset, value, this.littleEndian);
super.incrementOffset(options);
}
Expand Down
2 changes: 0 additions & 2 deletions src/primitives/i32.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export class I32 extends SizedType<number> {
}

readPacked(dt: DataView, options: Options = { byteOffset: 0 }): number {
super.rangeCheck(dt.byteLength, options.byteOffset);
const value = dt.getInt32(options.byteOffset, this.littleEndian);
super.incrementOffset(options);
return value;
Expand All @@ -18,7 +17,6 @@ export class I32 extends SizedType<number> {
dt: DataView,
options: Options = { byteOffset: 0 },
): void {
super.rangeCheck(dt.byteLength, options.byteOffset);
dt.setInt32(options.byteOffset, value, this.littleEndian);
super.incrementOffset(options);
}
Expand Down
2 changes: 0 additions & 2 deletions src/primitives/i64.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export class I64 extends SizedType<bigint> {
}

readPacked(dt: DataView, options: Options = { byteOffset: 0 }): bigint {
super.rangeCheck(dt.byteLength, options.byteOffset);
const value = dt.getBigInt64(options.byteOffset, this.littleEndian);
super.incrementOffset(options);
return value;
Expand All @@ -18,7 +17,6 @@ export class I64 extends SizedType<bigint> {
dt: DataView,
options: Options = { byteOffset: 0 },
): void {
super.rangeCheck(dt.byteLength, options.byteOffset);
dt.setBigInt64(options.byteOffset, value, this.littleEndian);
super.incrementOffset(options);
}
Expand Down
2 changes: 0 additions & 2 deletions src/primitives/i8.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ export class I8 extends SizedType<number> {
}

readPacked(dt: DataView, options: Options = { byteOffset: 0 }): number {
super.rangeCheck(dt.byteLength, options.byteOffset);
const value = dt.getInt8(options.byteOffset);
super.incrementOffset(options);
return value;
Expand All @@ -17,7 +16,6 @@ export class I8 extends SizedType<number> {
dt: DataView,
options: Options = { byteOffset: 0 },
): void {
super.rangeCheck(dt.byteLength, options.byteOffset);
dt.setInt8(options.byteOffset, value);
super.incrementOffset(options);
}
Expand Down
2 changes: 0 additions & 2 deletions src/primitives/u16.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export class U16 extends SizedType<number> {
}

readPacked(dt: DataView, options: Options = { byteOffset: 0 }): number {
super.rangeCheck(dt.byteLength, options.byteOffset);
const value = dt.getUint16(options.byteOffset, this.littleEndian);
super.incrementOffset(options);
return value;
Expand All @@ -18,7 +17,6 @@ export class U16 extends SizedType<number> {
dt: DataView,
options: Options = { byteOffset: 0 },
): void {
super.rangeCheck(dt.byteLength, options.byteOffset);
dt.setUint16(options.byteOffset, value, this.littleEndian);
super.incrementOffset(options);
}
Expand Down
2 changes: 0 additions & 2 deletions src/primitives/u32.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export class U32 extends SizedType<number> {
}

readPacked(dt: DataView, options: Options = { byteOffset: 0 }): number {
super.rangeCheck(dt.byteLength, options.byteOffset);
const value = dt.getUint32(options.byteOffset, this.littleEndian);
super.incrementOffset(options);
return value;
Expand All @@ -18,7 +17,6 @@ export class U32 extends SizedType<number> {
dt: DataView,
options: Options = { byteOffset: 0 },
): void {
super.rangeCheck(dt.byteLength, options.byteOffset);
dt.setUint32(options.byteOffset, value, this.littleEndian);
super.incrementOffset(options);
}
Expand Down
2 changes: 0 additions & 2 deletions src/primitives/u64.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export class U64 extends SizedType<bigint> {
}

readPacked(dt: DataView, options: Options = { byteOffset: 0 }): bigint {
super.rangeCheck(dt.byteLength, options.byteOffset);
const value = dt.getBigUint64(options.byteOffset, this.littleEndian);
super.incrementOffset(options);
return value;
Expand All @@ -18,7 +17,6 @@ export class U64 extends SizedType<bigint> {
dt: DataView,
options: Options = { byteOffset: 0 },
): void {
super.rangeCheck(dt.byteLength, options.byteOffset);
dt.setBigUint64(options.byteOffset, value, this.littleEndian);
super.incrementOffset(options);
}
Expand Down
2 changes: 0 additions & 2 deletions src/primitives/u8.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ export class U8 extends SizedType<number> {
}

readPacked(dt: DataView, options: Options = { byteOffset: 0 }): number {
super.rangeCheck(dt.byteLength, options.byteOffset);
const value = dt.getUint8(options.byteOffset);
super.incrementOffset(options);
return value;
Expand All @@ -17,7 +16,6 @@ export class U8 extends SizedType<number> {
dt: DataView,
options: Options = { byteOffset: 0 },
): void {
super.rangeCheck(dt.byteLength, options.byteOffset);
dt.setUint8(options.byteOffset, value);
super.incrementOffset(options);
}
Expand Down
4 changes: 0 additions & 4 deletions src/string/fixed_length.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ export class FixedLengthString extends SizedType<string> {
}

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

const view = new Uint8Array(
dt.buffer,
dt.byteOffset + options.byteOffset,
Expand All @@ -25,8 +23,6 @@ export class FixedLengthString extends SizedType<string> {
dt: DataView,
options: Options = { byteOffset: 0 },
): void {
super.rangeCheck(dt.byteLength, options.byteOffset);

const view = new Uint8Array(
dt.buffer,
dt.byteOffset + options.byteOffset,
Expand Down
7 changes: 0 additions & 7 deletions src/types/sized.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,4 @@ export abstract class SizedType<T> extends UnsizedType<T> implements Sized<T> {
protected override incrementOffset(options: Options): void {
super.incrementOffset(options, this.byteSize);
}

/** Allows you to check upfront if you will go out of bound */
protected rangeCheck(byteLength: number, offset: number): void {
if (this.byteSize > (byteLength - offset)) {
throw new RangeError("Out of bound");
}
}
}
45 changes: 45 additions & 0 deletions src/varint/i32_leb128_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,48 @@ Deno.test("i32leb128", async ({ step }) => {
});
});
});

Deno.test("i32leb128", async ({ step }) => {
await step("read", async ({ step }) => {
await step("positive", () => {
let data = Uint8Array.of(127);
let result = i32leb128.readPacked2(new DataView(data.buffer));
assertEquals(result, 127);

data = Uint8Array.of(128, 1);
result = i32leb128.readPacked2(new DataView(data.buffer));
assertEquals(result, 128);

data = Uint8Array.of(221, 199, 1);
result = i32leb128.readPacked2(new DataView(data.buffer));
assertEquals(result, 25565);

data = Uint8Array.of(255, 255, 255, 255, 7);
result = i32leb128.readPacked2(new DataView(data.buffer));
assertEquals(result, 2147483647);
});

await step("negative", () => {
let data = Uint8Array.of(255, 255, 255, 255, 15);
let result = i32leb128.readPacked2(new DataView(data.buffer));
assertEquals(result, -1);

data = Uint8Array.of(128, 128, 128, 128, 8);
result = i32leb128.readPacked2(new DataView(data.buffer));
assertEquals(result, -2147483648);
});

await step("bad", () => {
const data = Uint8Array.of(255, 255, 255, 255, 255, 15);
assertThrows(() => i32leb128.readPacked2(new DataView(data.buffer)));
});

await step("i32 max", () => {
const data = Uint8Array.of(255, 255, 255, 255, 7);
assertEquals(
i32leb128.readPacked2(new DataView(data.buffer)),
2147483647,
);
});
});
});
Loading