diff --git a/packages/util/src/hex/alphabet.ts b/packages/util/src/hex/alphabet.ts deleted file mode 100644 index ee7f9be29..000000000 --- a/packages/util/src/hex/alphabet.ts +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright 2017-2022 @polkadot/util authors & contributors -// SPDX-License-Identifier: Apache-2.0 - -const U8_TO_HEX = new Array(256); -const U16_TO_HEX = new Array(256 * 256); - -const HEX_TO_U8: Record = {}; -const HEX_TO_U16: Record = {}; - -for (let n = 0; n < 256; n++) { - const hex = n.toString(16).padStart(2, '0'); - - U8_TO_HEX[n] = hex; - HEX_TO_U8[hex] = n; -} - -for (let i = 0; i < 256; i++) { - for (let j = 0; j < 256; j++) { - const hex = U8_TO_HEX[i] + U8_TO_HEX[j]; - const n = (i << 8) | j; - - U16_TO_HEX[n] = hex; - HEX_TO_U16[hex] = n; - } -} - -export { HEX_TO_U16, HEX_TO_U8, U16_TO_HEX, U8_TO_HEX }; diff --git a/packages/util/src/hex/toU8a.spec.ts b/packages/util/src/hex/toU8a.spec.ts index 6e38d1bd8..fad801395 100644 --- a/packages/util/src/hex/toU8a.spec.ts +++ b/packages/util/src/hex/toU8a.spec.ts @@ -2,14 +2,13 @@ // SPDX-License-Identifier: Apache-2.0 import { performanceCmp } from '../test/performance'; -import { U16_TO_HEX } from './alphabet'; import { hexToU8a as hexToU8aBuffer } from './toU8aBuffer'; import { hexToU8a } from '.'; let ptest = '0x'; -for (let i = 0; i < 500000; i++) { - ptest += U16_TO_HEX[i % (256 * 256)]; +for (let i = 0; i < 1_000_000; i++) { + ptest += (i % 256).toString(16); } describe('hexToU8a', (): void => { @@ -55,7 +54,7 @@ describe('hexToU8a', (): void => { ).toThrow(/hex value to convert/); }); - performanceCmp('hexToU8a', ['hexToU8aBuffer', 'hexToU8a'], 10, [[ptest]], (s: string, isSecond) => + performanceCmp('hexToU8a', ['hexToU8aBuffer', 'hexToU8a'], 20, [[ptest]], (s: string, isSecond) => isSecond ? hexToU8a(s) : hexToU8aBuffer(s) diff --git a/packages/util/src/hex/toU8a.ts b/packages/util/src/hex/toU8a.ts index afa227652..8d91da933 100644 --- a/packages/util/src/hex/toU8a.ts +++ b/packages/util/src/hex/toU8a.ts @@ -3,9 +3,19 @@ import type { HexString } from '../types'; -import { HEX_TO_U8, HEX_TO_U16 } from './alphabet'; import { hexStripPrefix } from './stripPrefix'; +const CHARS = '0123456789abcdef'; +const UNHEX = new Array(255); + +for (let i = 0; i < CHARS.length; i++) { + UNHEX[CHARS[i].charCodeAt(0)] = i; + + if (i > 9) { + UNHEX[CHARS[i].toUpperCase().charCodeAt(0)] = i; + } +} + /** * @name hexToU8a * @summary Creates a Uint8Array object from a hex string. @@ -21,34 +31,29 @@ import { hexStripPrefix } from './stripPrefix'; * hexToU8a('0x80001f', 32); // Uint8Array([0x00, 0x80, 0x00, 0x1f]) * ``` */ -export function hexToU8a (_value?: HexString | string | null, bitLength = -1): Uint8Array { - if (!_value) { +export function hexToU8a (value?: HexString | string | null, bitLength = -1): Uint8Array { + if (!value) { return new Uint8Array(); } - const value = hexStripPrefix(_value).toLowerCase(); - const valLength = value.length / 2; + const str = hexStripPrefix(value); + const strLength = str.length / 2; const endLength = Math.ceil( bitLength === -1 - ? valLength + ? strLength : bitLength / 8 ); const result = new Uint8Array(endLength); - const offset = endLength > valLength - ? endLength - valLength + const offset = endLength > strLength + ? endLength - strLength : 0; - const dv = new DataView(result.buffer, offset); - const mod = (endLength - offset) % 2; - const length = endLength - offset - mod; - for (let i = 0; i < length; i += 2) { - const idx = i * 2; - - dv.setUint16(i, HEX_TO_U16[value.substring(idx, idx + 4)]); - } - - if (mod) { - dv.setUint8(length, HEX_TO_U8[value.substring(value.length - 2)]); + for (let i = offset, s = 0; i < endLength; i++, s += 2) { + // The big factor here is actually the string lookups. If we do + // HEX_TO_U16[value.substring()] we get an 10x slowdown. In the + // same vein using charCodeAt (as opposed to value[s] or value.charAt(s)) is + // also the faster operation by at least 2x with the character map above + result[i] = ((UNHEX[str.charCodeAt(s)] << 4) + UNHEX[str.charCodeAt(s + 1)]) | 0; } return result; diff --git a/packages/util/src/u8a/toBigInt.ts b/packages/util/src/u8a/toBigInt.ts index bcf7f2d53..7cc13106d 100644 --- a/packages/util/src/u8a/toBigInt.ts +++ b/packages/util/src/u8a/toBigInt.ts @@ -10,43 +10,6 @@ import { _1n } from '../bi/consts'; const U8_MAX = BigInt(256); const U16_MAX = BigInt(256 * 256); -/** @internal */ -function xor (input: Uint8Array): Uint8Array { - const result = new Uint8Array(input.length); - const dvI = new DataView(input.buffer, input.byteOffset); - const dvO = new DataView(result.buffer); - const mod = input.length % 2; - const length = input.length - mod; - - for (let i = 0; i < length; i += 2) { - dvO.setUint16(i, dvI.getUint16(i) ^ 0xffff); - } - - if (mod) { - dvO.setUint8(length, dvI.getUint8(length) ^ 0xff); - } - - return result; -} - -/** @internal */ -function toBigInt (input: Uint8Array): bigint { - const dvI = new DataView(input.buffer, input.byteOffset); - const mod = input.length % 2; - const length = input.length - mod; - let result = BigInt(0); - - for (let i = 0; i < length; i += 2) { - result = (result * U16_MAX) + BigInt(dvI.getUint16(i)); - } - - if (mod) { - result = (result * U8_MAX) + BigInt(dvI.getUint8(length)); - } - - return result; -} - /** * @name u8aToBigInt * @summary Creates a BigInt from a Uint8Array object. @@ -57,10 +20,34 @@ export function u8aToBigInt (value: Uint8Array, { isLe = true, isNegative = fals } const u8a = isLe - ? value.reverse() - : value; + ? value + : value.reverse(); + + const dvI = new DataView(u8a.buffer, u8a.byteOffset); + const mod = u8a.length % 2; + let result = BigInt(0); + + // This is mostly written for readability (with the single isNegative shortcut), + // as opposed to performance, e.g. `u8aToBn` does loop unrolling, etc. + if (isNegative) { + for (let i = u8a.length - 2; i >= mod; i -= 2) { + result = (result * U16_MAX) + BigInt(dvI.getUint16(i, true) ^ 0xffff); + } + + if (mod) { + result = (result * U8_MAX) + BigInt(dvI.getUint8(0) ^ 0xff); + } + } else { + for (let i = u8a.length - 2; i >= mod; i -= 2) { + result = (result * U16_MAX) + BigInt(dvI.getUint16(i, true)); + } + + if (mod) { + result = (result * U8_MAX) + BigInt(dvI.getUint8(0)); + } + } return isNegative - ? ((toBigInt(xor(u8a)) * -_1n) - _1n) - : toBigInt(u8a); + ? ((result * -_1n) - _1n) + : result; } diff --git a/packages/util/src/u8a/toHex.ts b/packages/util/src/u8a/toHex.ts index 9c4ae49e1..fe2436bd1 100644 --- a/packages/util/src/u8a/toHex.ts +++ b/packages/util/src/u8a/toHex.ts @@ -3,7 +3,25 @@ import type { HexString } from '../types'; -import { U8_TO_HEX, U16_TO_HEX } from '../hex/alphabet'; +const U8 = new Array(256); +const U16 = new Array(256 * 256); + +for (let n = 0; n < 256; n++) { + const hex = n.toString(16).padStart(2, '0'); + + U8[n] = hex; +} + +for (let i = 0; i < 256; i++) { + const s = i << 8; + + for (let j = 0; j < 256; j++) { + const hex = U8[i] + U8[j]; + const n = s | j; + + U16[n] = hex; + } +} /** @internal */ function hex (value: Uint8Array): string { @@ -15,11 +33,11 @@ function hex (value: Uint8Array): string { for (let i = 0; i < length; i += 2) { // we only use getUint16 here instead of getUint32 - at least in our // tests this is faster to execute (both long & short strings tested) - result += U16_TO_HEX[dv.getUint16(i)]; + result += U16[dv.getUint16(i)]; } if (mod) { - result += U8_TO_HEX[dv.getUint8(length)]; + result += U8[dv.getUint8(length)]; } return result;