diff --git a/CHANGELOG.md b/CHANGELOG.md index 5d5c60084..de03c7478 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,8 @@ Contributed: Changes: - Internal code maintainability enhancements -- Add `u8a*Strict` variants with Uint8Array-only inputs +- Add `compactFromU8a` variant with Uint8Array-only inputs +- Add `u8aConcatStrict` variant with Uint8Array-only inputs - Adjust `u8aToU8a` checks with better `isU8a` shortcut - Adjust compact conversion with unrolled loops - Adjust `string{Camel, Pascal}Case` with loop (no map) diff --git a/packages/util/src/compact/fromU8a.spec.ts b/packages/util/src/compact/fromU8a.spec.ts index 167829851..9f274de6f 100644 --- a/packages/util/src/compact/fromU8a.spec.ts +++ b/packages/util/src/compact/fromU8a.spec.ts @@ -4,7 +4,7 @@ import { BN } from '../bn/bn'; import { hexToU8a } from '../hex/toU8a'; import { performance } from '../test/performance'; -import { compactFromU8a, compactFromU8aStrict } from '.'; +import { compactFromU8a, compactFromU8aLim } from '.'; describe('compactFromU8a', (): void => { it('decoded u8 value', (): void => { @@ -76,7 +76,7 @@ describe('compactFromU8a', (): void => { performance('compactFromU8a (u8)', 1_000_000, [[new Uint8Array([63 << 2])]], compactFromU8a); performance('compactFromU8a (u16)', 1_000_000, [[new Uint8Array([0b11111101, 0b00000111])]], compactFromU8a); performance('compactFromU8a (u32)', 1_000_000, [[new Uint8Array([254, 255, 3, 0])]], compactFromU8a); - performance('compactFromU8aStrict', 1_000_000, [[new Uint8Array([254, 255, 3, 0])]], compactFromU8aStrict); + performance('compactFromU8aLim (u32)', 1_000_000, [[new Uint8Array([254, 255, 3, 0])]], compactFromU8aLim); performance('compactFromU8a (u48)', 1_000_000, [[hexToU8a('0x0b00407a10f35a')]], compactFromU8a); performance('compactFromU8a (u96)', 250_000, [[new Uint8Array([23, 52, 0x40, 0x7a, 0x10, 0xf3, 0x5a, 0, 0, 18])]], compactFromU8a); }); diff --git a/packages/util/src/compact/fromU8a.ts b/packages/util/src/compact/fromU8a.ts index f06a9331c..41159dbcc 100644 --- a/packages/util/src/compact/fromU8a.ts +++ b/packages/util/src/compact/fromU8a.ts @@ -21,14 +21,8 @@ import { u8aToBn, u8aToU8a } from '../u8a'; * ``` */ export function compactFromU8a (input: U8aLike): [number, BN] { - return compactFromU8aStrict(u8aToU8a(input)); -} + const u8a = u8aToU8a(input); -/** - * @name compactFromU8aStrict - * @description A strict version of [[compactFromU8a]], accepting only Uint8Array inputs - */ -export function compactFromU8aStrict (u8a: Uint8Array): [number, BN] { // The u8a is manually converted here for 1, 2 & 4 lengths, it is 2x faster // than doing an additional call to u8aToBn (as with variable length) switch (u8a[0] & 0b11) { @@ -67,3 +61,47 @@ export function compactFromU8aStrict (u8a: Uint8Array): [number, BN] { return [offset, u8aToBn(u8a.subarray(1, offset))]; } } + +/** + * @name compactFromU8aLim + * @description A limited version of [[compactFromU8a]], accepting only Uint8Array inputs for values <= 48 bits + */ +export function compactFromU8aLim (u8a: Uint8Array): [number, number] { + // The u8a is manually converted here for 1, 2 & 4 lengths, it is 2x faster + // than doing an additional call to u8aToBn (as with variable length) + switch (u8a[0] & 0b11) { + case 0b00: + return [1, u8a[0] >>> 2]; + + case 0b01: + return [2, (u8a[0] + (u8a[1] * 0x1_00)) >>> 2]; + + case 0b10: + return [4, (u8a[0] + (u8a[1] * 0x1_00) + (u8a[2] * 0x1_00_00) + (u8a[3] * 0x1_00_00_00)) >>> 2]; + + // 0b11 + default: + break; + } + + // add 5 to shifted (4 for base length, 1 for this byte) + const offset = (u8a[0] >>> 2) + 5; + + // we unroll the loop + switch (offset) { + // there still could be 4 bytes data, similar to 0b10 above (with offsets) + case 5: + return [5, u8a[1] + (u8a[2] * 0x1_00) + (u8a[3] * 0x1_00_00) + (u8a[4] * 0x1_00_00_00)]; + + case 6: + return [6, u8a[1] + (u8a[2] * 0x1_00) + (u8a[3] * 0x1_00_00) + (u8a[4] * 0x1_00_00_00) + (u8a[5] * 0x1_00_00_00_00)]; + + // 6 bytes data is the maximum, 48 bits (56 would overflow) + case 7: + return [7, u8a[1] + (u8a[2] * 0x1_00) + (u8a[3] * 0x1_00_00) + (u8a[4] * 0x1_00_00_00) + (u8a[5] * 0x1_00_00_00_00) + (u8a[6] * 0x1_00_00_00_00_00)]; + + // for anything else, use the non-unrolled version + default: + throw new Error('Compact input is > Number.MAX_SAFE_INTEGER'); + } +} diff --git a/packages/util/src/compact/index.ts b/packages/util/src/compact/index.ts index 2455b560b..0f00d0fa7 100644 --- a/packages/util/src/compact/index.ts +++ b/packages/util/src/compact/index.ts @@ -24,5 +24,5 @@ export { compactAddLength } from './addLength'; export { compactStripLength } from './stripLength'; -export { compactFromU8a, compactFromU8aStrict } from './fromU8a'; +export { compactFromU8a, compactFromU8aLim } from './fromU8a'; export { compactToU8a } from './toU8a'; diff --git a/packages/util/src/is/ascii.ts b/packages/util/src/is/ascii.ts index 6ec862ce3..670bfa286 100644 --- a/packages/util/src/is/ascii.ts +++ b/packages/util/src/is/ascii.ts @@ -33,16 +33,8 @@ export function isAscii (value?: U8aLike | null): boolean { if (value) { return isStringIn && !isHex(value) ? value.toString().split('').every(isAsciiChar) - : isAsciiStrict(u8aToU8a(value)); + : u8aToU8a(value).every(isAsciiByte); } return isStringIn; } - -/** - * @name isAsciiStrict - * @description A strict version of [[isAscii]], accepting only Uint8Array inputs - */ -export function isAsciiStrict (u8a: Uint8Array): boolean { - return u8a.every(isAsciiByte); -} diff --git a/packages/util/src/is/index.ts b/packages/util/src/is/index.ts index 0ed8d3c08..38610182e 100644 --- a/packages/util/src/is/index.ts +++ b/packages/util/src/is/index.ts @@ -5,7 +5,7 @@ * @summary Type checking utilities */ export { isArray } from './array'; -export { isAscii, isAsciiStrict } from './ascii'; +export { isAscii } from './ascii'; export { isBigInt } from './bigInt'; export { isBn } from './bn'; export { isBuffer } from './buffer'; @@ -30,5 +30,5 @@ export { isToBigInt } from './toBigInt'; export { isToBn } from './toBn'; export { isU8a } from './u8a'; export { isUndefined } from './undefined'; -export { isUtf8, isUtf8Strict } from './utf8'; +export { isUtf8 } from './utf8'; export { isWasm } from './wasm'; diff --git a/packages/util/src/is/utf8.ts b/packages/util/src/is/utf8.ts index 638f7d97b..10f429e41 100644 --- a/packages/util/src/is/utf8.ts +++ b/packages/util/src/is/utf8.ts @@ -19,14 +19,7 @@ export function isUtf8 (value?: HexString | number[] | Buffer | Uint8Array | str return isString(value); } - return isUtf8Strict(u8aToU8a(value)); -} - -/** - * @name isUtf8Strict - * @description A strict version of [[isUtf8]], acception only Uint8Array inpouts - */ -export function isUtf8Strict (u8a: Uint8Array): boolean { + const u8a = u8aToU8a(value); const len = u8a.length; let i = 0; diff --git a/packages/util/src/u8a/cmp.ts b/packages/util/src/u8a/cmp.ts index d26889a0d..61c38918f 100644 --- a/packages/util/src/u8a/cmp.ts +++ b/packages/util/src/u8a/cmp.ts @@ -22,14 +22,8 @@ import { u8aToU8a } from './toU8a'; * ``` */ export function u8aCmp (a: HexString | Uint8Array | string, b: HexString | Uint8Array | string): number { - return u8aCmpStrict(u8aToU8a(a), u8aToU8a(b)); -} - -/** - * @name u8aCmpStrict - * @description A strict version of [[u8aCmp]], accepting only Uint8Array inputs - */ -export function u8aCmpStrict (u8aa: Uint8Array, u8ab: Uint8Array): number { + const u8aa = u8aToU8a(a); + const u8ab = u8aToU8a(b); let i = 0; while (true) { diff --git a/packages/util/src/u8a/eq.ts b/packages/util/src/u8a/eq.ts index b7c054d60..21f7c9bc7 100644 --- a/packages/util/src/u8a/eq.ts +++ b/packages/util/src/u8a/eq.ts @@ -20,14 +20,9 @@ import { u8aToU8a } from './toU8a'; * ``` */ export function u8aEq (a: HexString | Uint8Array | string, b: HexString | Uint8Array | string): boolean { - return u8aEqStrict(u8aToU8a(a), u8aToU8a(b)); -} + const u8aa = u8aToU8a(a); + const u8ab = u8aToU8a(b); -/** - * @name u8aEqStrict - * @description A strict version of [[u8aCmp]], accepting only Uint8Array inputs - */ -export function u8aEqStrict (u8aa: Uint8Array, u8ab: Uint8Array): boolean { if (u8aa.length === u8ab.length) { const dvA = new DataView(u8aa.buffer, u8aa.byteOffset); const dvB = new DataView(u8ab.buffer, u8ab.byteOffset); diff --git a/packages/util/src/u8a/index.ts b/packages/util/src/u8a/index.ts index a513c7bf8..b9e680ada 100644 --- a/packages/util/src/u8a/index.ts +++ b/packages/util/src/u8a/index.ts @@ -5,10 +5,10 @@ * @summary Utility methods to convert to and from `Uint8Array` objects */ -export { u8aCmp, u8aCmpStrict } from './cmp'; +export { u8aCmp } from './cmp'; export { u8aConcat, u8aConcatStrict } from './concat'; export { u8aEmpty } from './empty'; -export { u8aEq, u8aEqStrict } from './eq'; +export { u8aEq } from './eq'; export { u8aFixLength } from './fixLength'; export { u8aSorted } from './sorted'; export { u8aToBigInt } from './toBigInt';