Aligh u8aToHex optimization (#1184)

* Aligh u8aToHex optimization

* Adjust

* Fix failing tests, empty value

* Adjust

* numberToHex fix

* Fix hexFixLength tests
This commit is contained in:
Jaco
2021-11-14 21:03:41 +02:00
committed by GitHub
parent 8a6fd0812a
commit cf990dd4ec
12 changed files with 70 additions and 54 deletions
+5 -3
View File
@@ -22,14 +22,16 @@ export function arrayFlatten <T> (arrays: T[][]): T[] {
// pre-allocate based on the combined size
let size = 0;
for (const a of arrays) {
size += a.length;
for (let i = 0; i < arrays.length; i++) {
size += arrays[i].length;
}
const output = new Array<T>(size);
let i = -1;
for (const a of arrays) {
for (let j = 0; j < arrays.length; j++) {
const a = arrays[j];
// instead of pushing, we just set the entries
for (let e = 0; e < a.length; e++) {
output[++i] = a[e];
-6
View File
@@ -10,12 +10,6 @@ describe('hexFixLength', (): void => {
).toEqual('0x12345678');
});
it('returns bitLength === -1 as-is (adding missing 0)', (): void => {
expect(
hexFixLength('0x1234567')
).toEqual('0x01234567');
});
it('does not change when bitlength === length', (): void => {
expect(
hexFixLength('0x12345678', 32)
+2 -3
View File
@@ -6,7 +6,7 @@ import { hexHasPrefix } from '.';
describe('hexHasPrefix', (): void => {
it('returns true when hex prefix is found', (): void => {
expect(
hexHasPrefix('0x123')
hexHasPrefix('0x1234')
).toEqual(true);
});
@@ -24,8 +24,7 @@ describe('hexHasPrefix', (): void => {
it('returns false when non-string value supplied', (): void => {
expect(
// eslint-disable-next-line @typescript-eslint/no-explicit-any,@typescript-eslint/no-unsafe-argument
hexHasPrefix(false as any)
hexHasPrefix(false as unknown as string)
).toEqual(false);
});
});
+1 -1
View File
@@ -20,5 +20,5 @@ import { isHex } from '../is/hex';
* ```
*/
export function hexHasPrefix (value?: string | null): value is HexString {
return !!(value && isHex(value, -1, true) && value.substr(0, 2) === '0x');
return !!value && isHex(value, -1);
}
+8 -2
View File
@@ -10,6 +10,12 @@ describe('hexStripPrefix', (): void => {
).toEqual('');
});
it('returns an empty string when 0x value supplied', (): void => {
expect(
hexStripPrefix('0x')
).toEqual('');
});
it('strips the prefix from hex strings', (): void => {
expect(
hexStripPrefix('0x1223')
@@ -24,7 +30,7 @@ describe('hexStripPrefix', (): void => {
it('throws when invalid hex', (): void => {
expect(
(): string => hexStripPrefix('0x0x01ab')
).toThrow(/Invalid hex/);
() => hexStripPrefix('0x0x01ab')
).toThrow(/Expected hex value to convert/);
});
});
+5 -7
View File
@@ -1,9 +1,7 @@
// Copyright 2017-2021 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { hexHasPrefix } from './hasPrefix';
const UNPREFIX_HEX_REGEX = /^[a-fA-F0-9]+$/;
import { REGEX_HEX_NOPREFIX, REGEX_HEX_PREFIXED } from '../is/hex';
/**
* @name hexStripPrefix
@@ -20,13 +18,13 @@ const UNPREFIX_HEX_REGEX = /^[a-fA-F0-9]+$/;
* ```
*/
export function hexStripPrefix (value?: string | null): string {
if (!value) {
if (!value || value === '0x') {
return '';
} else if (hexHasPrefix(value)) {
} else if (REGEX_HEX_PREFIXED.test(value)) {
return value.substr(2);
} else if (UNPREFIX_HEX_REGEX.test(value)) {
} else if (REGEX_HEX_NOPREFIX.test(value)) {
return value;
}
throw new Error(`Invalid hex ${value} passed to hexStripPrefix`);
throw new Error(`Expected hex value to convert, found '${value}'`);
}
+6 -6
View File
@@ -21,9 +21,9 @@ describe('hexToU8a', (): void => {
it('returns a Uint8Array with the correct values (bitLength > provided)', (): void => {
expect(
hexToU8a('0x80000A', 32)
hexToU8a('0x80000A', 64)
).toEqual(
new Uint8Array([0, 128, 0, 10])
new Uint8Array([0, 0, 0, 0, 0, 128, 0, 10])
);
});
@@ -37,20 +37,20 @@ describe('hexToU8a', (): void => {
it('fails when non-hex value provided', (): void => {
expect(
(): Uint8Array => hexToU8a('notahex')
() => hexToU8a('notahex')
).toThrow(/hex value to convert/);
});
it.skip('performance', (): void => {
let a = '0x';
for (let i = 0; i < 8192; i++) {
a += U16_TO_HEX[i];
for (let i = 0; i < 640000; i++) {
a += U16_TO_HEX[i % (256 * 256)];
}
console.time('hexToU8a:performance');
for (let i = 0; i < 8192; i++) {
for (let i = 0; i < 64; i++) {
hexToU8a(a);
}
+25 -10
View File
@@ -3,9 +3,7 @@
import type { HexString } from '../types';
import { assert } from '../assert';
import { isHex } from '../is/hex';
import { HEX_TO_U8 } from './alphabet';
import { HEX_TO_U8, HEX_TO_U16 } from './alphabet';
import { hexStripPrefix } from './stripPrefix';
/**
@@ -28,8 +26,6 @@ export function hexToU8a (_value?: HexString | string | null, bitLength = -1): U
return new Uint8Array();
}
assert(isHex(_value), () => `Expected hex value to convert, found '${_value}'`);
const value = hexStripPrefix(_value).toLowerCase();
const valLength = value.length / 2;
const resultLength = Math.ceil(
@@ -38,12 +34,31 @@ export function hexToU8a (_value?: HexString | string | null, bitLength = -1): U
: bitLength / 8
);
const result = new Uint8Array(resultLength);
const offset = Math.max(0, resultLength - valLength);
const offset = resultLength > valLength
? resultLength - valLength
: 0;
for (let i = 0; i < resultLength; i++) {
// TODO With some magic we can use the HEX_TO_U16 lookups here as well
// (and then finally HEX_TO_U8 for the overflows)
result[i + offset] = HEX_TO_U8[value.substr(i * 2, 2)];
// With offsets, it becomes slightly trickier since we need to ensure the U16
// is at the correct alignment as well. Due to this, we just focus on the simple
// case where we do no bitlength conversions (which is the most-taken path)
if (offset) {
for (let i = 0; i < resultLength; i++) {
result[i + offset] = HEX_TO_U8[value.substr(i * 2, 2)];
}
} else {
// NOTE: It is only _slightly_ more optimal to use HEX_TO_U16 here as
// well. The overhead does not seem to be in the lookup map iteration,
// even with 640k inputs
const mod = resultLength % 2;
const u16 = new Uint16Array(result.buffer, 0, (resultLength - mod) / 2);
for (let i = 0; i < u16.length; i++) {
u16[i] = HEX_TO_U16[value.substr(i * 4, 4)];
}
if (mod) {
result[resultLength - 1] = HEX_TO_U8[value.substr(value.length - 2, 2)];
}
}
return result;
+6 -6
View File
@@ -21,9 +21,9 @@ describe('hexToU8a', (): void => {
it('returns a Uint8Array with the correct values (bitLength > provided)', (): void => {
expect(
hexToU8a('0x80000A', 32)
hexToU8a('0x80000A', 64)
).toEqual(
new Uint8Array([0, 128, 0, 10])
new Uint8Array([0, 0, 0, 0, 0, 128, 0, 10])
);
});
@@ -37,20 +37,20 @@ describe('hexToU8a', (): void => {
it('fails when non-hex value provided', (): void => {
expect(
(): Uint8Array => hexToU8a('notahex')
() => hexToU8a('notahex')
).toThrow(/hex value to convert/);
});
it.skip('performance', (): void => {
let a = '0x';
for (let i = 0; i < 8192; i++) {
a += U16_TO_HEX[i];
for (let i = 0; i < 640000; i++) {
a += U16_TO_HEX[i % (256 * 256)];
}
console.time('hexToU8a:performance');
for (let i = 0; i < 8192; i++) {
for (let i = 0; i < 64; i++) {
hexToU8a(a);
}
+3 -5
View File
@@ -3,8 +3,6 @@
import type { HexString } from '../types';
import { assert } from '../assert';
import { isHex } from '../is/hex';
import { hexStripPrefix } from './stripPrefix';
/**
@@ -27,8 +25,6 @@ export function hexToU8a (_value?: HexString | string | null, bitLength = -1): U
return new Uint8Array();
}
assert(isHex(_value), () => `Expected hex value to convert, found '${_value}'`);
const value = hexStripPrefix(_value);
const buf = Buffer.from(value, 'hex');
const valLength = value.length / 2;
@@ -42,7 +38,9 @@ export function hexToU8a (_value?: HexString | string | null, bitLength = -1): U
return Uint8Array.from(buf);
}
const offset = Math.max(0, resultLength - valLength);
const offset = resultLength > valLength
? resultLength - valLength
: 0;
if (offset) {
const u8a = new Uint8Array(resultLength);
+6 -4
View File
@@ -3,7 +3,9 @@
import type { HexString } from '../types';
const HEX_REGEX = /^0x[a-fA-F0-9]+$/;
export const REGEX_HEX_PREFIXED = /^0x([\da-fA-F]{2})+$/;
export const REGEX_HEX_NOPREFIX = /^([\da-fA-F]{2})+$/;
/**
* @name isHex
@@ -20,10 +22,10 @@ const HEX_REGEX = /^0x[a-fA-F0-9]+$/;
* isHex('0x1234', 8); // => false
* ```
*/
export function isHex (value: unknown, bitLength = -1, ignoreLength = false): value is HexString {
return (typeof value === 'string' && (value === '0x' || HEX_REGEX.test(value)))
export function isHex (value: unknown, bitLength = -1): value is HexString {
return (typeof value === 'string' && (value === '0x' || REGEX_HEX_PREFIXED.test(value)))
? bitLength === -1
? ((value.length % 2 === 0) || ignoreLength)
? true
: (value.length === (2 + Math.ceil(bitLength / 4)))
: false;
}
+3 -1
View File
@@ -27,5 +27,7 @@ export function numberToHex (value?: number | null, bitLength = -1): HexString {
return '0x';
}
return hexFixLength(value.toString(16), bitLength, true);
const hex = value.toString(16);
return hexFixLength(hex.length % 2 ? `0${hex}` : hex, bitLength, true);
}