From d4491aeb7fa5b63a3ee58e62e95957f309813295 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Mon, 1 Jun 2020 09:29:44 +0200 Subject: [PATCH] Small cleanups, u8aConcat optimization (#607) * Small cleanups, u8aConcat oprimizations * Cleanup * Pre-allocate arrays * Don't recreate PREFIX --- CHANGELOG.md | 4 ++-- packages/util-crypto/src/address/keyMulti.ts | 6 +++-- packages/util-crypto/src/address/keySub.ts | 6 +++-- packages/util/src/bn/max.ts | 2 +- packages/util/src/bn/min.ts | 2 +- packages/util/src/detectPackage.ts | 2 +- packages/util/src/is/function.ts | 5 ++-- packages/util/src/logger.ts | 6 ++--- packages/util/src/polyfill/textDecoder.ts | 3 +-- packages/util/src/promisify.ts | 4 ++-- packages/util/src/types.ts | 6 ++--- packages/util/src/u8a/concat.ts | 24 +++++++++++++------- 12 files changed, 39 insertions(+), 31 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b9341bea5..518769f22 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,10 +2,10 @@ ## 2.12.0-beta.x -- **Important** Removed `mnemonicToSeed`, it has not been in-active use since POC-2, use `mnemonicToMiniSecret` for the expected outcome +- **Important** Removed `mnemonicToSeed`, it is only for bip39, use `mnemonicToMiniSecret` for the Substrate variant - Added `u8aEq` to compare two Uint8Array or hex values for an exact match - Added `addressEq` to compare two addresses (ss58, Uint8array or hex) for an exact match -- Performance optimizations in `u8aToHex` conversions +- Performance optimizations in `u8aToHex` & `u8aConcat` ## 2.11.1 May 26, 2020 diff --git a/packages/util-crypto/src/address/keyMulti.ts b/packages/util-crypto/src/address/keyMulti.ts index 0ad2e943c..87cca0a8a 100644 --- a/packages/util-crypto/src/address/keyMulti.ts +++ b/packages/util-crypto/src/address/keyMulti.ts @@ -3,15 +3,17 @@ // of the Apache-2.0 license. See the LICENSE file for details. import BN from 'bn.js'; -import { bnToU8a, compactToU8a, u8aConcat, u8aSorted } from '@polkadot/util'; +import { bnToU8a, compactToU8a, stringToU8a, u8aConcat, u8aSorted } from '@polkadot/util'; import blake2AsU8a from '../blake2/asU8a'; import decodeAddress from './decode'; +const PREFIX = stringToU8a('modlpy/utilisuba'); + export default function createKeyMulti (who: (Uint8Array | string)[], threshold: BigInt | BN | number): Uint8Array { return blake2AsU8a( u8aConcat( - 'modlpy/utilisuba', + PREFIX, compactToU8a(who.length), ...u8aSorted(who.map((who) => decodeAddress(who))), bnToU8a(threshold, { bitLength: 16, isLe: true }) diff --git a/packages/util-crypto/src/address/keySub.ts b/packages/util-crypto/src/address/keySub.ts index 19afdbfee..9d1cb9ce7 100644 --- a/packages/util-crypto/src/address/keySub.ts +++ b/packages/util-crypto/src/address/keySub.ts @@ -3,15 +3,17 @@ // of the Apache-2.0 license. See the LICENSE file for details. import BN from 'bn.js'; -import { bnToU8a, u8aConcat } from '@polkadot/util'; +import { bnToU8a, stringToU8a, u8aConcat } from '@polkadot/util'; import blake2AsU8a from '../blake2/asU8a'; import decodeAddress from './decode'; +const PREFIX = stringToU8a('modlpy/utilisuba'); + export default function createKeySub (who: Uint8Array | string, index: BigInt | BN | number): Uint8Array { return blake2AsU8a( u8aConcat( - 'modlpy/utilisuba', + PREFIX, decodeAddress(who), bnToU8a(index, { bitLength: 16, isLe: true }) ) diff --git a/packages/util/src/bn/max.ts b/packages/util/src/bn/max.ts index 3ae6532ae..44a025f56 100644 --- a/packages/util/src/bn/max.ts +++ b/packages/util/src/bn/max.ts @@ -22,5 +22,5 @@ import assert from '../assert'; export default function max (...items: BN[]): BN { assert(items && items.length >= 1, 'Must provide one or more BN arguments'); - return items.reduce((acc: BN, val: BN): BN => BN.max(acc, val), items[0]); + return items.reduce((acc: BN, val: BN) => BN.max(acc, val), items[0]); } diff --git a/packages/util/src/bn/min.ts b/packages/util/src/bn/min.ts index 53611fe3a..e8738f87e 100644 --- a/packages/util/src/bn/min.ts +++ b/packages/util/src/bn/min.ts @@ -22,5 +22,5 @@ import assert from '../assert'; export default function min (...items: BN[]): BN { assert(items && items.length >= 1, 'Must provide one or more BN arguments'); - return items.reduce((acc: BN, val: BN): BN => BN.min(acc, val), items[0]); + return items.reduce((acc: BN, val: BN) => BN.min(acc, val), items[0]); } diff --git a/packages/util/src/detectPackage.ts b/packages/util/src/detectPackage.ts index aa4e2120f..68ae0e550 100644 --- a/packages/util/src/detectPackage.ts +++ b/packages/util/src/detectPackage.ts @@ -39,7 +39,7 @@ function flattenVersions (_all: (VersionPath | string)[]): string { const verLength = all.reduce((max, { version }): number => Math.max(max, version.length), 0); return all - .map(({ path, version }): string => `\t${version.padEnd(verLength)}\t${expandPath(path)}`) + .map(({ path, version }) => `\t${version.padEnd(verLength)}\t${expandPath(path)}`) .join('\n'); } diff --git a/packages/util/src/is/function.ts b/packages/util/src/is/function.ts index 3bed5a7e1..cd6eb1ee3 100644 --- a/packages/util/src/is/function.ts +++ b/packages/util/src/is/function.ts @@ -2,6 +2,8 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. +type FnType = (...params: unknown[]) => unknown; + /** * @name isFunction * @summary Tests for a `function`. @@ -16,7 +18,6 @@ * isFunction(() => false); // => true * ``` */ -// eslint-disable-next-line @typescript-eslint/ban-types -export default function isFunction (value: unknown): value is Function { +export default function isFunction (value: unknown): value is FnType { return typeof value === 'function'; } diff --git a/packages/util/src/logger.ts b/packages/util/src/logger.ts index 3b86d8320..d4174c77c 100644 --- a/packages/util/src/logger.ts +++ b/packages/util/src/logger.ts @@ -67,8 +67,7 @@ export function format (value: unknown): unknown { function apply (log: LogType, type: string, values: Logger$Data): void { if (values.length === 1 && isFunction(values[0])) { - // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment,@typescript-eslint/ban-types - const fnResult = (values[0] as Function)(); + const fnResult = values[0](); return apply(log, type, Array.isArray(fnResult) ? fnResult : [fnResult]); } @@ -83,8 +82,7 @@ function apply (log: LogType, type: string, values: Logger$Data): void { ); } -// eslint-disable-next-line @typescript-eslint/no-unused-vars -function noop (...values: Logger$Data): void { +function noop (): void { // noop } diff --git a/packages/util/src/polyfill/textDecoder.ts b/packages/util/src/polyfill/textDecoder.ts index f7c3c57d8..43e62294d 100644 --- a/packages/util/src/polyfill/textDecoder.ts +++ b/packages/util/src/polyfill/textDecoder.ts @@ -7,8 +7,7 @@ if (typeof TextDecoder === 'undefined') { // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-var-requires const { TextDecoder: UTD } = require('util') as { TextDecoder: { new(): TextDecoder } }; - // eslint-disable-next-line @typescript-eslint/no-explicit-any,@typescript-eslint/no-unsafe-member-access - (global as any).TextDecoder = UTD; + global.TextDecoder = UTD; } catch (error) { // noop } diff --git a/packages/util/src/promisify.ts b/packages/util/src/promisify.ts index c94e68bc6..c57ac5ead 100644 --- a/packages/util/src/promisify.ts +++ b/packages/util/src/promisify.ts @@ -17,8 +17,8 @@ * await promisify(null, (cb) => cb(new Error('error!'))); // rejects with `error!` * ``` */ -// eslint-disable-next-line @typescript-eslint/ban-types,@typescript-eslint/explicit-module-boundary-types,@typescript-eslint/no-explicit-any -export default function promisify (self: unknown, fn: Function, ...params: any[]): Promise { +// eslint-disable-next-line @typescript-eslint/no-explicit-any +export default function promisify (self: unknown, fn: (...params: any) => any, ...params: any[]): Promise { return new Promise((resolve, reject): void => { const handler = (error: Error | null, result?: R): void => { if (error) { diff --git a/packages/util/src/types.ts b/packages/util/src/types.ts index 88e01cb07..2fff8a251 100644 --- a/packages/util/src/types.ts +++ b/packages/util/src/types.ts @@ -20,10 +20,8 @@ export interface SiDef { value: string; } -// eslint-disable-next-line @typescript-eslint/no-explicit-any -type Logger$Data$Fn = () => any[]; -// eslint-disable-next-line @typescript-eslint/no-explicit-any -export type Logger$Data = (any | Logger$Data$Fn)[]; +type Logger$Data$Fn = () => unknown[]; +export type Logger$Data = (unknown | Logger$Data$Fn)[]; export interface Logger { debug: (...values: Logger$Data) => void; diff --git a/packages/util/src/u8a/concat.ts b/packages/util/src/u8a/concat.ts index 2cf6e5847..cfc35246a 100644 --- a/packages/util/src/u8a/concat.ts +++ b/packages/util/src/u8a/concat.ts @@ -21,15 +21,23 @@ import u8aToU8a from './toU8a'; * ); // [1, 2, 3, 4, 5, 6] * ``` */ -export default function u8aConcat (..._list: (Uint8Array | string)[]): Uint8Array { - const list = _list.map(u8aToU8a); - const result = new Uint8Array(list.reduce((total, item): number => total + item.length, 0)); +export default function u8aConcat (...list: (Uint8Array | string)[]): Uint8Array { + let length = 0; let offset = 0; + const u8as = new Array(list.length) as Uint8Array[]; - return list.reduce((result, item): Uint8Array => { - result.set(item, offset); - offset += item.length; + for (let i = 0; i < list.length; i++) { + u8as[i] = u8aToU8a(list[i]); - return result; - }, result); + length += u8as[i].length; + } + + const result = new Uint8Array(length); + + for (let i = 0; i < u8as.length; i++) { + result.set(u8as[i], offset); + offset += u8as[i].length; + } + + return result; }