Small u8aToU8a optimizations (#1498)

* Small u8aToU8a optimizations

* Slight order adjustments

* Adjust
This commit is contained in:
Jaco
2022-05-24 16:42:58 +03:00
committed by GitHub
parent bce5553a4b
commit a4256de444
10 changed files with 55 additions and 22 deletions
+2
View File
@@ -5,10 +5,12 @@
Contributed:
- Add Ledger support for Parallel (Thanks to https://github.com/carlosala)
- Add Ledger support for Astar & Polkadex (Thanks to https://github.com/carlosala)
Changes:
- Internal code maintainability enhancements
- Adjust `u8aToU8a` checks with better `isU8a` shortcut
## 9.2.1 May 13, 2022
+4 -4
View File
@@ -65,8 +65,8 @@ describe('compactFromU8a', (): void => {
).toEqual('100000000');
});
performance('compactFromU8a (u8)', 500_000, [[new Uint8Array([63 << 2])]], compactFromU8a);
performance('compactFromU8a (u16)', 500_000, [[new Uint8Array([0b11111101, 0b00000111])]], compactFromU8a);
performance('compactFromU8a (u32)', 500_000, [[new Uint8Array([254, 255, 3, 0])]], compactFromU8a);
performance('compactFromU8a (u64)', 500_000, [[hexToU8a('0x0b00407a10f35a')]], compactFromU8a);
performance('compactFromU8a (u8)', 1_000_000, [[new Uint8Array([63 << 2])]], compactFromU8a);
performance('compactFromU8a (u16)', 1_000_000, [[new Uint8Array([0b11111101, 0b00000111])]], compactFromU8a);
performance('compactFromU8a (u32)', 1_000_000, [[new Uint8Array([254, 255, 3, 0])]], compactFromU8a);
performance('compactFromU8a (u64)', 250_000, [[hexToU8a('0x0b00407a10f35a')]], compactFromU8a);
});
+2 -2
View File
@@ -30,10 +30,10 @@ export function compactFromU8a (input: U8aLike): [number, BN] {
return [1, new BN(u8a[0] >>> 2)];
case 0b01:
return [2, new BN((u8a[0] + (u8a[1] * 0x1_00)) >>> 2)];
return [2, new BN((u8a[0] >>> 2) + (u8a[1] << 6))];
case 0b10:
return [4, new BN((u8a[0] + (u8a[1] * 0x1_00) + (u8a[2] * 0x1_00_00) + (u8a[3] * 0x1_00_00_00)) >>> 2)];
return [4, new BN((u8a[0] >>> 2) + (u8a[1] << 6) + (u8a[2] << 14) + (u8a[3] << 22))];
default: { // 0b11
// add 5 to shifted (4 for base length, 1 for this byte)
+8 -2
View File
@@ -10,9 +10,15 @@ describe('isBuffer', (): void => {
).toEqual(true);
});
it('returns false on non-Buffer values', (): void => {
it('returns false on non-Buffer values (number)', (): void => {
expect(
isBuffer(0)
isBuffer(123)
).toEqual(false);
});
it('returns false on non-Buffer values (null)', (): void => {
expect(
isBuffer(null)
).toEqual(false);
});
});
+3 -1
View File
@@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
import { hasBuffer } from '../has';
import { isFunction } from './function';
/**
* @name isBuffer
@@ -18,5 +19,6 @@ import { hasBuffer } from '../has';
* ```
*/
export function isBuffer (value: unknown): value is Buffer {
return hasBuffer && Buffer.isBuffer(value);
// we do check a function first, since it is slightly faster than isBuffer itself
return hasBuffer && isFunction(value && (value as Buffer).readDoubleLE) && Buffer.isBuffer(value);
}
+8 -1
View File
@@ -12,10 +12,17 @@ describe('isU8a', (): void => {
it('returns false on Array values', (): void => {
expect(
isU8a([])
isU8a([1, 2, 3])
).toEqual(false);
});
it('returns true on Buffer values', (): void => {
// under Node, Buffer implements Uint8Array
expect(
isU8a(Buffer.from([1, 2, 3]))
).toEqual(true);
});
it('returns true on Uint8Array values', (): void => {
expect(
isU8a(new Uint8Array())
+4 -1
View File
@@ -6,10 +6,13 @@ import { stringToU8a } from '..';
const RUSS_HELLO = new Uint8Array([208, 159, 209, 128, 208, 184, 208, 178, 208, 181, 209, 130, 44, 32, 208, 188, 208, 184, 209, 128, 33]);
describe('stringToU8a', (): void => {
it('decodes to an empty string for undefined', (): void => {
it('decodes to an empty string for undefined/null', (): void => {
expect(
stringToU8a()
).toEqual(new Uint8Array());
expect(
stringToU8a(null)
).toEqual(new Uint8Array());
});
it('encodes the string correctly', (): void => {
+1 -2
View File
@@ -21,8 +21,7 @@ const encoder = new TextEncoder();
* stringToU8a('hello'); // [0x68, 0x65, 0x6c, 0x6c, 0x6f]
* ```
*/
// eslint-disable-next-line @typescript-eslint/ban-types
export function stringToU8a (value?: AnyString): Uint8Array {
export function stringToU8a (value?: AnyString | null): Uint8Array {
return value
? encoder.encode(value.toString())
: new Uint8Array();
+14 -1
View File
@@ -39,11 +39,24 @@ describe('u8aToU8a', (): void => {
});
it('creates from a Buffer', (): void => {
const input = Buffer.from([1, 2, 3, 128, 0, 10, 11, 12]);
const output = u8aToU8a(input);
expect(
u8aToU8a(Buffer.from([1, 2, 3, 128, 0, 10, 11, 12]))
typeof input.readDoubleLE
).toEqual('function');
expect(
typeof (output as Buffer).readDoubleLE
).toEqual('undefined');
expect(
output
).toEqual(
new Uint8Array([1, 2, 3, 128, 0, 10, 11, 12])
);
});
it('creates from a Buffer (hex)', (): void => {
expect(
u8aToU8a(Buffer.from('80000a', 'hex'))
).toEqual(
+9 -8
View File
@@ -25,13 +25,14 @@ import { stringToU8a } from '../string/toU8a';
* ```
*/
export function u8aToU8a (value?: U8aLike | null): Uint8Array {
return value
? Array.isArray(value) || isBuffer(value)
return isU8a(value)
// under Node, Buffer implements Uint8Array, check it first
? isBuffer(value)
? new Uint8Array(value)
: isU8a(value)
? value
: isHex(value)
? hexToU8a(value)
: stringToU8a(value)
: new Uint8Array();
: value
: isHex(value)
? hexToU8a(value)
: Array.isArray(value)
? new Uint8Array(value)
: stringToU8a(value);
}