Remove concatBuffer viariant (#1234)

This commit is contained in:
Jaco
2021-11-23 17:56:33 +02:00
committed by GitHub
parent 25a53e43fd
commit 5e16b1e84c
12 changed files with 66 additions and 107 deletions
+5 -37
View File
@@ -1,49 +1,17 @@
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { arrayRange, formatNumber } from '@polkadot/util';
import { arrayRange } from '@polkadot/util';
import { randomAsU8a } from '@polkadot/util-crypto';
import { formatOps, performanceJs } from '../../util/test/performance';
import { performance, performanceCmp } from '../../util/test/performance';
type ExecFn = (input: Uint8Array, onlyJs: boolean) => unknown;
const GENERATED = arrayRange(256).map(() => randomAsU8a());
function loop (count: number, onlyJs: boolean, exec: ExecFn): [number, unknown[]] {
const start = Date.now();
const results = new Array<unknown>(GENERATED.length);
for (let i = 0; i < count; i++) {
const result = exec(GENERATED[i % GENERATED.length], onlyJs);
if (i < GENERATED.length) {
results[i] = result;
}
}
return [Date.now() - start, results];
}
const GENERATED = arrayRange(256).map(() => [randomAsU8a()]);
export function performanceWasm (name: string, count: number, exec: ExecFn): void {
it(`performance: ${name}`, (): void => {
const [ws, rws] = loop(count, false, exec);
const [js, rjs] = loop(count, true, exec);
console.log(`
performance run for ${name} completed with ${formatNumber(count)} iterations.
${'WebAssembly:'.padStart(19)} ${ws.toString().padStart(15)} ms ${ws < js ? '(fastest)' : `(slowest, ${(ws / js).toFixed(2)}x)`}${formatOps(count, ws)}
${'JavaScript:'.padStart(19)} ${js.toString().padStart(15)} ms ${ws > js ? '(fastest)' : `(slowest, ${(js / ws).toFixed(2)}x)`}${formatOps(count, js)}
`);
const unmatched = rws.filter((r, i) =>
JSON.stringify(rjs[i]) !== JSON.stringify(r)
);
expect(unmatched.length).toEqual(0);
});
performanceCmp(name, ['WebAssembly', 'JavaScript'], count, GENERATED, exec);
}
export { performanceJs };
export { performance, performanceCmp };
-6
View File
@@ -13,12 +13,6 @@
"default": "./hex/toU8aBuffer.js"
}
},
"./u8a/concat": {
"node": {
"require": "./u8a/concatBuffer.cjs",
"default": "./u8a/concatBuffer.js"
}
},
"./u8a/toHex": {
"node": {
"require": "./u8a/toHexBuffer.cjs",
+7 -2
View File
@@ -1,8 +1,9 @@
// Copyright 2017-2021 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { performanceJs } from '../../test/performance';
import { performanceCmp } from '../../test/performance';
import { U16_TO_HEX } from './alphabet';
import { hexToU8a as hexToU8aBuffer } from './toU8aBuffer';
import { hexToU8a } from '.';
let ptest = '0x';
@@ -54,5 +55,9 @@ describe('hexToU8a', (): void => {
).toThrow(/hex value to convert/);
});
performanceJs('hexToU8a (640k input)', 10, [[ptest]], hexToU8a);
performanceCmp('hexToU8a (640k input)', ['Node + Buffer', 'Uint8Array'], 10, [[ptest]], (s: string, isSecond) =>
isSecond
? hexToU8a(s)
: hexToU8aBuffer(s)
);
});
-10
View File
@@ -1,16 +1,8 @@
// Copyright 2017-2021 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { performanceJs } from '../../test/performance';
import { U16_TO_HEX } from './alphabet';
import { hexToU8a } from './toU8aBuffer';
let ptest = '0x';
for (let i = 0; i < 640 * 1024; i++) {
ptest += U16_TO_HEX[i % (256 * 256)];
}
describe('hexToU8a', (): void => {
it('returns an empty Uint8Array when null provided', (): void => {
expect(
@@ -47,6 +39,4 @@ describe('hexToU8a', (): void => {
() => hexToU8a('notahex')
).toThrow(/hex value to convert/);
});
performanceJs('hexToU8a (Node Buffer, 640k input)', 10, [[ptest]], hexToU8a);
});
+2 -2
View File
@@ -1,7 +1,7 @@
// Copyright 2017-2021 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { performanceJs } from '../../test/performance';
import { performance } from '../../test/performance';
import { isHex } from '.';
describe('isHex', (): void => {
@@ -71,5 +71,5 @@ describe('isHex', (): void => {
).toEqual(true);
});
performanceJs('isHex (1024-bit)', 1000000, [[ptest]], isHex);
performance('isHex (1024-bit)', 1000000, [[ptest]], isHex);
});
+2 -2
View File
@@ -4,7 +4,7 @@
import { randomAsNumber } from '@polkadot/util-crypto';
import words from '@polkadot/util-crypto/mnemonic/bip39-en';
import { performanceJs } from '../../test/performance';
import { performance } from '../../test/performance';
import { arrayRange } from '../array';
import { stringCamelCase, stringUpperFirst } from '.';
@@ -43,5 +43,5 @@ describe('stringCamelCase', (): void => {
).toBe('fooBarBazTestSpacedExtra');
});
performanceJs('stringCamelCase', 64000, randomWords, stringCamelCase);
performance('stringCamelCase', 64000, randomWords, stringCamelCase);
});
+8
View File
@@ -1,8 +1,14 @@
// Copyright 2017-2021 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { randomAsU8a } from '@polkadot/util-crypto';
import { performance } from '../../test/performance';
import { arrayRange } from '../array';
import { u8aConcat } from '.';
const ptest = arrayRange(10).map(() => randomAsU8a());
describe('u8aConcat', (): void => {
it('concatenates arrays', (): void => {
expect(
@@ -27,4 +33,6 @@ describe('u8aConcat', (): void => {
new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9])
);
});
performance('u8aConcat', 50000, [[ptest]], u8aConcat);
});
@@ -1,30 +0,0 @@
// Copyright 2017-2021 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { u8aConcat } from './concatBuffer';
describe('u8aConcat', (): void => {
it('concatenates arrays', (): void => {
expect(
u8aConcat(
new Uint8Array([1, 2, 3, 4]),
new Uint8Array([5, 6]),
new Uint8Array([7, 8, 9])
)
).toEqual(
new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9])
);
});
it('concatenates arrays & hex values', (): void => {
expect(
u8aConcat(
new Uint8Array([1, 2, 3, 4]),
'0x0506',
'0x070809'
)
).toEqual(
new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9])
);
});
});
+2 -2
View File
@@ -1,7 +1,7 @@
// Copyright 2017-2021 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { performanceJs } from '../../test/performance';
import { performance } from '../../test/performance';
import { u8aEq } from '.';
const ptest = new Uint8Array(32768);
@@ -95,5 +95,5 @@ describe('u8aEq', (): void => {
).toEqual(true);
});
performanceJs('u8aEq (32k cmp)', 50000, [[ptest, ptest]], u8aEq);
performance('u8aEq (32k cmp)', 50000, [[ptest, ptest]], u8aEq);
});
+7 -2
View File
@@ -1,7 +1,8 @@
// Copyright 2017-2021 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { performanceJs } from '../../test/performance';
import { performanceCmp } from '../../test/performance';
import { u8aToHex as u8aToHexBuffer } from './toHexBuffer';
import { u8aToHex } from '.';
const ptest = new Uint8Array(32768);
@@ -65,5 +66,9 @@ describe('u8aToHex', (): void => {
).toEqual('0x8000…0c0d');
});
performanceJs('u8aToHex (32k input)', 2000, [[ptest]], u8aToHex);
performanceCmp('u8aToHex (32k input)', ['Node + Buffer', 'Uint8Array'], 2000, [[ptest]], (s: Uint8Array, isSecond) =>
isSecond
? u8aToHex(s)
: u8aToHexBuffer(s)
);
});
@@ -1,15 +1,8 @@
// Copyright 2017-2021 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { performanceJs } from '../../test/performance';
import { u8aToHex } from './toHexBuffer';
const ptest = new Uint8Array(32768);
for (let i = 0; i < ptest.length; i++) {
ptest[i] = i % 256;
}
describe('u8aToHex', (): void => {
it('returns empty as 0x', (): void => {
expect(
@@ -64,6 +57,4 @@ describe('u8aToHex', (): void => {
)
).toEqual('0x8000…0c0d');
});
performanceJs('u8aToHex (Node Buffer, 32k input)', 2000, [[ptest]], u8aToHex);
});
+33 -5
View File
@@ -6,14 +6,19 @@ import { formatDecimal, formatNumber } from '@polkadot/util';
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type ExecFn = (...params: any[]) => unknown;
function loop (count: number, inputs: unknown[][], exec: ExecFn): number {
function loop (count: number, inputs: unknown[][], exec: ExecFn): [number, unknown[]] {
const start = Date.now();
const results = new Array<unknown>(inputs.length);
for (let i = 0; i < count; i++) {
exec(...inputs[i % inputs.length]);
const result = exec(...inputs[i % inputs.length]);
if (i < inputs.length) {
results[i] = result;
}
}
return Date.now() - start;
return [Date.now() - start, results];
}
export function formatFixed (value: number): string {
@@ -31,9 +36,9 @@ ${formatFixed(ops).padStart(15 + 20)} ops/s
${formatFixed(micro).padStart(15 + 20)} μs/op`;
}
export function performanceJs (name: string, count: number, inputs: unknown[][], exec: ExecFn): void {
export function performance (name: string, count: number, inputs: unknown[][], exec: ExecFn): void {
it(`performance: ${name}`, (): void => {
const time = loop(count, inputs, exec);
const [time] = loop(count, inputs, exec);
console.log(`
performance run for ${name} completed with ${formatNumber(count)} iterations.
@@ -42,3 +47,26 @@ ${'JavaScript:'.padStart(19)} ${time.toString().padStart(15)} ms${formatOps(coun
`);
});
}
export function performanceCmp (name: string, [first, second]: [string, string], count: number, inputs: unknown[][], exec: ExecFn): void {
it(`performance: ${name}`, (): void => {
const pa = inputs.map((values) => [...values, false]);
const pb = inputs.map((values) => [...values, true]);
const [ta, ra] = loop(count, pa, exec);
const [tb, rb] = loop(count, pb, exec);
console.log(`
performance run for ${name} completed with ${formatNumber(count)} iterations.
${`${first}:`.padStart(19)} ${ta.toString().padStart(15)} ms ${ta < tb ? '(fastest)' : `(slowest, ${(ta / tb).toFixed(2)}x)`}${formatOps(count, ta)}
${`${second}:`.padStart(19)} ${tb.toString().padStart(15)} ms ${ta > tb ? '(fastest)' : `(slowest, ${(tb / ta).toFixed(2)}x)`}${formatOps(count, tb)}
`);
const unmatched = ra.filter((r, i) =>
JSON.stringify(r) !== JSON.stringify(rb[i])
);
expect(unmatched.length).toEqual(0);
});
}