Skip to content

Commit

Permalink
fix: slow types
Browse files Browse the repository at this point in the history
  • Loading branch information
eliassjogreen committed Mar 5, 2024
1 parent 609a9bc commit 9cc7a5b
Show file tree
Hide file tree
Showing 19 changed files with 75 additions and 58 deletions.
3 changes: 2 additions & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "@denosaurs/byte-type",
"version": "0.3.1",
"exports": "./mod.ts"
"exports": "./mod.ts",
"lock": false
}
40 changes: 28 additions & 12 deletions src/array/typed_array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,24 +68,40 @@ export class TypedArray<T extends TypedArrays> extends SizedType<T> {
}
}

export interface TypedArrayTypeConstructor<T extends TypedArrays> {
new (length: number): TypedArray<T>;
}

function createTypedArrayType<E extends TypedConstructors<TypedArrays>>(
constructor: E,
) {
): TypedArrayTypeConstructor<ReturnType<E["from"]>> {
return class extends TypedArray<ReturnType<E["from"]>> {
constructor(length: number) {
super(constructor, length);
}
};
}

export const Uint8ArrayType = createTypedArrayType(Uint8Array);
export const Uint8ClampedArrayType = createTypedArrayType(Uint8ClampedArray);
export const Int8ArrayType = createTypedArrayType(Int8Array);
export const Uint16ArrayType = createTypedArrayType(Uint16Array);
export const Int16ArrayType = createTypedArrayType(Int16Array);
export const Uint32ArrayType = createTypedArrayType(Uint32Array);
export const Int32ArrayType = createTypedArrayType(Int32Array);
export const Float32ArrayType = createTypedArrayType(Float32Array);
export const Float64ArrayType = createTypedArrayType(Float64Array);
export const BigUint64ArrayType = createTypedArrayType(BigUint64Array);
export const BigInt64ArrayType = createTypedArrayType(BigInt64Array);
export const Uint8ArrayType: TypedArrayTypeConstructor<Uint8Array> =
createTypedArrayType(Uint8Array);
export const Uint8ClampedArrayType: TypedArrayTypeConstructor<
Uint8ClampedArray
> = createTypedArrayType(Uint8ClampedArray);
export const Int8ArrayType: TypedArrayTypeConstructor<Int8Array> =
createTypedArrayType(Int8Array);
export const Uint16ArrayType: TypedArrayTypeConstructor<Uint16Array> =
createTypedArrayType(Uint16Array);
export const Int16ArrayType: TypedArrayTypeConstructor<Int16Array> =
createTypedArrayType(Int16Array);
export const Uint32ArrayType: TypedArrayTypeConstructor<Uint32Array> =
createTypedArrayType(Uint32Array);
export const Int32ArrayType: TypedArrayTypeConstructor<Int32Array> =
createTypedArrayType(Int32Array);
export const Float32ArrayType: TypedArrayTypeConstructor<Float32Array> =
createTypedArrayType(Float32Array);
export const Float64ArrayType: TypedArrayTypeConstructor<Float64Array> =
createTypedArrayType(Float64Array);
export const BigUint64ArrayType: TypedArrayTypeConstructor<BigUint64Array> =
createTypedArrayType(BigUint64Array);
export const BigInt64ArrayType: TypedArrayTypeConstructor<BigInt64Array> =
createTypedArrayType(BigInt64Array);
2 changes: 1 addition & 1 deletion src/primitives/bool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ export class Bool extends SizedType<boolean> {
}
}

export const bool = new Bool();
export const bool: Bool = new Bool();
8 changes: 4 additions & 4 deletions src/primitives/f32.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { type Options, SizedType } from "../types/mod.ts";
import { isLittleEndian } from "../util.ts";

export class F32 extends SizedType<number> {
constructor(readonly littleEndian = isLittleEndian) {
constructor(readonly littleEndian: boolean = isLittleEndian) {
super(4, 4);
}

Expand All @@ -24,6 +24,6 @@ export class F32 extends SizedType<number> {
}
}

export const f32le = new F32(true);
export const f32be = new F32(false);
export const f32 = new F32();
export const f32le: F32 = new F32(true);
export const f32be: F32 = new F32(false);
export const f32: F32 = new F32();
8 changes: 4 additions & 4 deletions src/primitives/f64.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { type Options, SizedType } from "../types/mod.ts";
import { isLittleEndian } from "../util.ts";

export class F64 extends SizedType<number> {
constructor(readonly littleEndian = isLittleEndian) {
constructor(readonly littleEndian: boolean = isLittleEndian) {
super(8, 8);
}

Expand All @@ -24,6 +24,6 @@ export class F64 extends SizedType<number> {
}
}

export const f64le = new F64(true);
export const f64be = new F64(false);
export const f64 = new F64();
export const f64le: F64 = new F64(true);
export const f64be: F64 = new F64(false);
export const f64: F64 = new F64();
8 changes: 4 additions & 4 deletions src/primitives/i16.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { type Options, SizedType } from "../types/mod.ts";
import { isLittleEndian } from "../util.ts";

export class I16 extends SizedType<number> {
constructor(readonly littleEndian = isLittleEndian) {
constructor(readonly littleEndian: boolean = isLittleEndian) {
super(2, 2);
}

Expand All @@ -24,6 +24,6 @@ export class I16 extends SizedType<number> {
}
}

export const i16le = new I16(true);
export const i16be = new I16(false);
export const i16 = new I16();
export const i16le: I16 = new I16(true);
export const i16be: I16 = new I16(false);
export const i16: I16 = new I16();
8 changes: 4 additions & 4 deletions src/primitives/i32.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { type Options, SizedType } from "../types/mod.ts";
import { isLittleEndian } from "../util.ts";

export class I32 extends SizedType<number> {
constructor(readonly littleEndian = isLittleEndian) {
constructor(readonly littleEndian: boolean = isLittleEndian) {
super(4, 4);
}

Expand All @@ -24,6 +24,6 @@ export class I32 extends SizedType<number> {
}
}

export const i32le = new I32(true);
export const i32be = new I32(false);
export const i32 = new I32();
export const i32le: I32 = new I32(true);
export const i32be: I32 = new I32(false);
export const i32: I32 = new I32();
8 changes: 4 additions & 4 deletions src/primitives/i64.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { type Options, SizedType } from "../types/mod.ts";
import { isLittleEndian } from "../util.ts";

export class I64 extends SizedType<bigint> {
constructor(readonly littleEndian = isLittleEndian) {
constructor(readonly littleEndian: boolean = isLittleEndian) {
super(8, 8);
}

Expand All @@ -24,6 +24,6 @@ export class I64 extends SizedType<bigint> {
}
}

export const i64le = new I64(true);
export const i64be = new I64(false);
export const i64 = new I64();
export const i64le: I64 = new I64(true);
export const i64be: I64 = new I64(false);
export const i64: I64 = new I64();
2 changes: 1 addition & 1 deletion src/primitives/i8.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ export class I8 extends SizedType<number> {
}
}

export const i8 = new I8();
export const i8: I8 = new I8();
8 changes: 4 additions & 4 deletions src/primitives/u16.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { type Options, SizedType } from "../types/mod.ts";
import { isLittleEndian } from "../util.ts";

export class U16 extends SizedType<number> {
constructor(readonly littleEndian = isLittleEndian) {
constructor(readonly littleEndian: boolean = isLittleEndian) {
super(2, 2);
}

Expand All @@ -24,6 +24,6 @@ export class U16 extends SizedType<number> {
}
}

export const u16le = new U16(true);
export const u16be = new U16(false);
export const u16 = new U16();
export const u16le: U16 = new U16(true);
export const u16be: U16 = new U16(false);
export const u16: U16 = new U16();
8 changes: 4 additions & 4 deletions src/primitives/u32.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { type Options, SizedType } from "../types/mod.ts";
import { isLittleEndian } from "../util.ts";

export class U32 extends SizedType<number> {
constructor(readonly littleEndian = isLittleEndian) {
constructor(readonly littleEndian: boolean = isLittleEndian) {
super(4, 4);
}

Expand All @@ -24,6 +24,6 @@ export class U32 extends SizedType<number> {
}
}

export const u32le = new U32(true);
export const u32be = new U32(false);
export const u32 = new U32();
export const u32le: U32 = new U32(true);
export const u32be: U32 = new U32(false);
export const u32: U32 = new U32();
8 changes: 4 additions & 4 deletions src/primitives/u64.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { type Options, SizedType } from "../types/mod.ts";
import { isLittleEndian } from "../util.ts";

export class U64 extends SizedType<bigint> {
constructor(readonly littleEndian = isLittleEndian) {
constructor(readonly littleEndian: boolean = isLittleEndian) {
super(8, 8);
}

Expand All @@ -24,6 +24,6 @@ export class U64 extends SizedType<bigint> {
}
}

export const u64le = new U64(true);
export const u64be = new U64(false);
export const u64 = new U64();
export const u64le: U64 = new U64(true);
export const u64be: U64 = new U64(false);
export const u64: U64 = new U64();
2 changes: 1 addition & 1 deletion src/primitives/u8.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ export class U8 extends SizedType<number> {
}
}

export const u8 = new U8();
export const u8: U8 = new U8();
4 changes: 2 additions & 2 deletions src/small_number/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@ export class U4 extends SizedType<U4Number> {
}
}

export const u2 = new U2();
export const u4 = new U4();
export const u2: U2 = new U2();
export const u4: U4 = new U4();
2 changes: 1 addition & 1 deletion src/string/cstring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ export class CString extends UnsizedType<string> {
}
}

export const cstring = new CString();
export const cstring: CString = new CString();
4 changes: 2 additions & 2 deletions src/string/fixed_length.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@ export class FixedLengthString extends SizedType<string> {
}
}

export const asciiChar = new FixedLengthString(1);
export const utf8Char = new FixedLengthString(4);
export const asciiChar: FixedLengthString = new FixedLengthString(1);
export const utf8Char: FixedLengthString = new FixedLengthString(4);
6 changes: 3 additions & 3 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ import type { UnsizedType } from "./mod.ts";
/**
* The endianess of your machine, true if little endian and false if big endian.
*/
export const isLittleEndian = (() => {
export const isLittleEndian: boolean = (() => {
const buffer = new ArrayBuffer(2);
new DataView(buffer).setUint16(0, 256, true);
return new Uint16Array(buffer)[0] === 256;
})();

/** Align the value `unaligned` to the first integer that is divisible by `alignment` */
export const align = (unaligned: number, alignment: number) =>
export const align = (unaligned: number, alignment: number): number =>
(unaligned + alignment - 1) & ~(alignment - 1);

type ArrayOrRecord<T> = T[] | Record<string | number, T>;

/** Find and returns the biggest alignment out of a record / array of types */
export const getBiggestAlignment = (
input: ArrayOrRecord<UnsizedType<unknown>>,
) =>
): number =>
Object.values(input)
.reduce((acc, x) => Math.max(acc, x.byteAlignment), 0);
2 changes: 1 addition & 1 deletion src/varint/i32_leb128.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ export class I32Leb128 extends UnsizedType<number> {
}
}

export const i32leb128 = new I32Leb128();
export const i32leb128: I32Leb128 = new I32Leb128();
2 changes: 1 addition & 1 deletion src/varint/i64_leb128.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,4 @@ export class I64Leb128 extends UnsizedType<bigint> {
}
}

export const i64leb128 = new I64Leb128();
export const i64leb128: I64Leb128 = new I64Leb128();

0 comments on commit 9cc7a5b

Please sign in to comment.