Skip to content

Commit

Permalink
remove headerEncode from stack, clean commented code, address PR comm…
Browse files Browse the repository at this point in the history
…ents
  • Loading branch information
kuhe committed Jul 9, 2024
1 parent 01d0c33 commit 3557cdc
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 83 deletions.
3 changes: 2 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"clean": "rimraf ./dist-* && rimraf *.tsbuildinfo || exit 0",
"lint": "npx eslint -c ../../.eslintrc.js \"src/**/*.ts\" && node ./scripts/lint",
"format": "prettier --config ../../prettier.config.js --ignore-path ../.prettierignore --write \"**/*.{ts,md,json}\"",
"test": "yarn g:jest --maxWorkers=1"
"test": "yarn g:jest --maxWorkers=1",
"test:cbor:perf": "node ./scripts/cbor-perf.mjs"
},
"main": "./dist-cjs/index.js",
"module": "./dist-es/index.js",
Expand Down
14 changes: 0 additions & 14 deletions packages/core/src/submodules/cbor/cbor-decode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,20 +218,6 @@ export function bytesToFloat16(a: Uint8, b: Uint8): Float32 {
return scalar * (exponentComponent * summation);
}

// function bytesToFloat32(a: Uint8, b: Uint8, c: Uint8, d: Uint8): Float32 {
// const sign = a >> 7;
// const exponent = ((a & 0b0111_1111) << 1) | ((b & 0b1000_0000) >> 7);
// const fraction = ((b & 0b0111_1111) << 16) | (c << 8) | d;
//
// const scalar = sign === 0 ? 1 : -1;
// const exponentComponent = Math.pow(2, exponent - 127);
// let summation = 1;
// for (let i = 1; i <= 23; ++i) {
// summation += ((fraction >> (23 - i)) & 1) * Math.pow(2, -i);
// }
// return scalar * exponentComponent * summation;
// }

function decodeCount(at: Uint32, to: Uint32): number {
const minor = payload[at] & 0b0001_1111;

Expand Down
80 changes: 12 additions & 68 deletions packages/core/src/submodules/cbor/cbor-encode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
majorMap,
majorNegativeInt64,
majorSpecial,
majorTag,
majorUint64,
majorUnstructuredByteString,
majorUtf8String,
Expand All @@ -22,10 +21,6 @@ import { alloc } from "./cbor-types";

const USE_BUFFER = typeof Buffer !== "undefined";

type BufferWithUtf8Write = Buffer & {
utf8Write(str: string, index: number): number;
};

const initialSize = 2048;
let data: Uint8Array = alloc(initialSize);
let dataView: DataView = new DataView(data.buffer, data.byteOffset, data.byteLength);
Expand All @@ -42,6 +37,9 @@ function ensureSpace(bytes: number) {
}
}

/**
* @internal
*/
export function toUint8Array(): Uint8Array {
const out = alloc(cursor);
out.set(data.subarray(0, cursor), 0);
Expand Down Expand Up @@ -83,8 +81,6 @@ function encodeHeader(major: CborMajorType, value: Uint64 | number): void {
}
}

const headerEncode: Symbol = Symbol("headerEncodeCbor");

/**
* @param _input - JS data object.
*/
Expand All @@ -93,21 +89,13 @@ export function encode(_input: any): void {

while (encodeStack.length) {
const input = encodeStack.pop();
if (input?.headerEncode === headerEncode) {
encodeHeader(input.major, input.count);
continue;
}

ensureSpace(typeof input === "string" ? input.length * 4 : 64);

if (typeof input === "string") {
if (USE_BUFFER) {
encodeHeader(majorUtf8String, Buffer.byteLength(input));
if ((data as BufferWithUtf8Write).utf8Write) {
cursor += (data as BufferWithUtf8Write).write(input, cursor);
} else {
cursor += (data as Buffer).write(input, cursor);
}
cursor += (data as Buffer).write(input, cursor);
} else {
const bytes = fromUtf8(input);
encodeHeader(majorUtf8String, bytes.byteLength);
Expand All @@ -117,8 +105,9 @@ export function encode(_input: any): void {
continue;
} else if (typeof input === "number") {
if (Number.isInteger(input)) {
const major = input >= 0 ? majorUint64 : majorNegativeInt64;
const value = input >= 0 ? input : -input - 1;
const nonNegative = input >= 0;
const major = nonNegative ? majorUint64 : majorNegativeInt64;
const value = nonNegative ? input : -input - 1;
if (value < 24) {
data[cursor++] = (major << 5) | value;
} else if (value < 256 /* 2 ** 8 */) {
Expand All @@ -144,8 +133,9 @@ export function encode(_input: any): void {
cursor += 8;
continue;
} else if (typeof input === "bigint") {
const major = input >= 0 ? majorUint64 : majorNegativeInt64;
const value = input >= 0 ? input : -input - BigInt(1);
const nonNegative = input >= 0;
const major = nonNegative ? majorUint64 : majorNegativeInt64;
const value = nonNegative ? input : -input - BigInt(1);
const n = Number(value);
if (n < 24) {
data[cursor++] = (major << 5) | n;
Expand Down Expand Up @@ -180,71 +170,25 @@ export function encode(_input: any): void {
for (let i = input.length - 1; i >= 0; --i) {
encodeStack.push(input[i]);
}
encodeStack.push({
headerEncode,
major: majorList,
count: input.length,
});
encodeHeader(majorList, input.length);
continue;
} else if (typeof input.byteLength === "number") {
ensureSpace(input.length * 2);
encodeHeader(majorUnstructuredByteString, input.length);
data.set(input, cursor);
cursor += input.byteLength;
continue;
} else if (typeof input === "object" && "tag" in input && "value" in input && Object.keys(input).length === 2) {
encodeStack.push(input.value);
encodeStack.push({
headerEncode,
major: majorTag,
count: input.tag,
});
continue;
} else if (typeof input === "object") {
const keys = Object.keys(input);
for (let i = keys.length - 1; i >= 0; --i) {
const key = keys[i];
encodeStack.push(input[key]);
encodeStack.push(key);
}
encodeStack.push({
headerEncode,
major: majorMap,
count: keys.length,
});
encodeHeader(majorMap, keys.length);
continue;
}

throw new Error(`data type ${input?.constructor?.name ?? typeof input} not compatible for encoding.`);
}
}

// function encodeString(input: string) {
// for (let i = 0; i < input.length; ++i) {
// const c = input.charCodeAt(i);
// const trailer = 0b1000_0000;
//
// if (c < 128 /* byte */) {
// data[cursor++] = c;
// } else if (c < 2048 /* 12 bits */) {
// // left 6 bits
// data[cursor++] = (c >> 6) | 0b1100_00000; // 11 leading.
// // right 6 bits
// data[cursor++] = (c & 0b00_111111) | trailer;
// } else if (c < 65536 /* 17 bits*/) {
// data[cursor++] = ((c >> 12) & 0b0011_1111) | 0b1110_0000; // 111 leading.
// data[cursor++] = ((c >> 6) & 0b0011_1111) | trailer;
// data[cursor++] = ((c >> 0) & 0b0011_1111) | trailer;
// } else {
// // surrogate pair
// i++;
// // 1st
// data[cursor++] = ((c >> 18) & 0b0011_1111) | 0b1111_0000; // 1111 leading.
// data[cursor++] = ((c >> 12) & 0b0011_1111) | trailer;
//
// // 2nd
// data[cursor++] = ((c >> 6) & 0b0011_1111) | trailer;
// data[cursor++] = ((c >> 0) & 0b0011_1111) | trailer;
// }
// }
// }

0 comments on commit 3557cdc

Please sign in to comment.