From 7eac68807458c9edb923f0da63590c060fb81148 Mon Sep 17 00:00:00 2001 From: Jaco Date: Mon, 18 Jul 2022 13:51:39 +0200 Subject: [PATCH] Small performance adjustments for Uint8Array indexing (#1603) --- packages/util/src/is/ascii.spec.ts | 2 +- packages/util/src/is/ascii.ts | 10 +++++----- packages/util/src/u8a/cmp.spec.ts | 21 +++++++++++++++++++++ packages/util/src/u8a/empty.ts | 4 ++-- packages/util/src/u8a/eq.ts | 4 ++-- 5 files changed, 31 insertions(+), 10 deletions(-) diff --git a/packages/util/src/is/ascii.spec.ts b/packages/util/src/is/ascii.spec.ts index 4b60c9de9..e9dcd5591 100644 --- a/packages/util/src/is/ascii.spec.ts +++ b/packages/util/src/is/ascii.spec.ts @@ -43,5 +43,5 @@ describe('isAscii', (): void => { }); perf('isAscii (str)', 2_000_000, [['Hello World! Testing']], isAscii); - perf('isAscii (u8a)', 100_000, [[[stringToU8a('Hello World! Testing')]]], isAscii); + perf('isAscii (u8a)', 200_000, [[[stringToU8a('Hello World! Testing')]]], isAscii); }); diff --git a/packages/util/src/is/ascii.ts b/packages/util/src/is/ascii.ts index 7bded4f87..be12bf4f4 100644 --- a/packages/util/src/is/ascii.ts +++ b/packages/util/src/is/ascii.ts @@ -9,13 +9,13 @@ import { isString } from './string'; /** @internal */ function isAsciiStr (str: AnyString): boolean { - const count = str.length; + const count = str.length | 0; for (let i = 0; i < count; i++) { const b = str.charCodeAt(i); // check is inlined here, it is faster than making a call - if ((b < 32) || (b > 126)) { + if (b < 32 || b > 126) { return false; } } @@ -25,13 +25,13 @@ function isAsciiStr (str: AnyString): boolean { /** @internal */ function isAsciiBytes (u8a: Uint8Array | Buffer | number[]): boolean { - const count = u8a.length; + const count = u8a.length | 0; for (let i = 0; i < count; i++) { - const b = u8a[i]; + const b = u8a[i] | 0; // check is inlined here, it is faster than making a call - if ((b < 32) || (b > 126)) { + if (b < 32 || b > 126) { return false; } } diff --git a/packages/util/src/u8a/cmp.spec.ts b/packages/util/src/u8a/cmp.spec.ts index 470b43d22..011e8cce1 100644 --- a/packages/util/src/u8a/cmp.spec.ts +++ b/packages/util/src/u8a/cmp.spec.ts @@ -1,8 +1,25 @@ // Copyright 2017-2022 @polkadot/util authors & contributors // SPDX-License-Identifier: Apache-2.0 +import { perf } from '../test/performance'; import { u8aCmp } from '.'; +const ltest = new Uint8Array(32768); +const stest = new Uint8Array(256); +const ztest = new Uint8Array(64); + +for (let i = 0; i < ltest.length; i++) { + if (i < ztest.length) { + ztest[i] = i % 256; + } + + if (i < stest.length) { + stest[i] = i % 256; + } + + ltest[i] = i % 256; +} + describe('u8aCmp', (): void => { it('has the same result as localCompare (equal length)', (): void => { const a = 'test abc'; @@ -36,4 +53,8 @@ describe('u8aCmp', (): void => { ) ).toEqual(0); }); + + perf('u8aCmp (64 cmp)', 100_000, [[ztest, ztest]], u8aCmp); + perf('u8aCmp (256 cmp)', 50_000, [[stest, stest]], u8aCmp); + perf('u8aCmp (32k cmp)', 500, [[ltest, ltest]], u8aCmp); }); diff --git a/packages/util/src/u8a/empty.ts b/packages/util/src/u8a/empty.ts index 3f6af7552..1ba72ab34 100644 --- a/packages/util/src/u8a/empty.ts +++ b/packages/util/src/u8a/empty.ts @@ -8,12 +8,12 @@ * Checks to see if the input `Uint8Array` has zero length or contains all 0 values. */ export function u8aEmpty (value: Uint8Array): boolean { - const len = value.length; + const len = value.length | 0; // on smaller sizes, the byte-by-byte compare is faster than allocating // another object for DataView (on very large arrays the DataView is faster) for (let i = 0; i < len; i++) { - if (value[i]) { + if (value[i] | 0) { return false; } } diff --git a/packages/util/src/u8a/eq.ts b/packages/util/src/u8a/eq.ts index 21f7c9bc7..f940ab3a0 100644 --- a/packages/util/src/u8a/eq.ts +++ b/packages/util/src/u8a/eq.ts @@ -26,8 +26,8 @@ export function u8aEq (a: HexString | Uint8Array | string, b: HexString | Uint8A if (u8aa.length === u8ab.length) { const dvA = new DataView(u8aa.buffer, u8aa.byteOffset); const dvB = new DataView(u8ab.buffer, u8ab.byteOffset); - const mod = u8aa.length % 4; - const length = u8aa.length - mod; + const mod = (u8aa.length % 4) | 0; + const length = (u8aa.length - mod) | 0; for (let i = 0; i < length; i += 4) { if (dvA.getUint32(i) !== dvB.getUint32(i)) {