Add compactFromU8aLim (Remove extraneous *Strict) (#1505)

* Add compactFromU8aLim (Remove extraoneous *Strict)

* Fix build
This commit is contained in:
Jaco
2022-05-27 11:10:56 +03:00
committed by GitHub
parent 4ae62c114b
commit 8b72584d53
10 changed files with 60 additions and 47 deletions
+2 -1
View File
@@ -10,7 +10,8 @@ Contributed:
Changes:
- Internal code maintainability enhancements
- Add `u8a*Strict` variants with Uint8Array-only inputs
- Add `compactFromU8a` variant with Uint8Array-only inputs
- Add `u8aConcatStrict` variant with Uint8Array-only inputs
- Adjust `u8aToU8a` checks with better `isU8a` shortcut
- Adjust compact conversion with unrolled loops
- Adjust `string{Camel, Pascal}Case` with loop (no map)
+2 -2
View File
@@ -4,7 +4,7 @@
import { BN } from '../bn/bn';
import { hexToU8a } from '../hex/toU8a';
import { performance } from '../test/performance';
import { compactFromU8a, compactFromU8aStrict } from '.';
import { compactFromU8a, compactFromU8aLim } from '.';
describe('compactFromU8a', (): void => {
it('decoded u8 value', (): void => {
@@ -76,7 +76,7 @@ describe('compactFromU8a', (): void => {
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('compactFromU8aStrict', 1_000_000, [[new Uint8Array([254, 255, 3, 0])]], compactFromU8aStrict);
performance('compactFromU8aLim (u32)', 1_000_000, [[new Uint8Array([254, 255, 3, 0])]], compactFromU8aLim);
performance('compactFromU8a (u48)', 1_000_000, [[hexToU8a('0x0b00407a10f35a')]], compactFromU8a);
performance('compactFromU8a (u96)', 250_000, [[new Uint8Array([23, 52, 0x40, 0x7a, 0x10, 0xf3, 0x5a, 0, 0, 18])]], compactFromU8a);
});
+45 -7
View File
@@ -21,14 +21,8 @@ import { u8aToBn, u8aToU8a } from '../u8a';
* ```
*/
export function compactFromU8a (input: U8aLike): [number, BN] {
return compactFromU8aStrict(u8aToU8a(input));
}
const u8a = u8aToU8a(input);
/**
* @name compactFromU8aStrict
* @description A strict version of [[compactFromU8a]], accepting only Uint8Array inputs
*/
export function compactFromU8aStrict (u8a: Uint8Array): [number, BN] {
// The u8a is manually converted here for 1, 2 & 4 lengths, it is 2x faster
// than doing an additional call to u8aToBn (as with variable length)
switch (u8a[0] & 0b11) {
@@ -67,3 +61,47 @@ export function compactFromU8aStrict (u8a: Uint8Array): [number, BN] {
return [offset, u8aToBn(u8a.subarray(1, offset))];
}
}
/**
* @name compactFromU8aLim
* @description A limited version of [[compactFromU8a]], accepting only Uint8Array inputs for values <= 48 bits
*/
export function compactFromU8aLim (u8a: Uint8Array): [number, number] {
// The u8a is manually converted here for 1, 2 & 4 lengths, it is 2x faster
// than doing an additional call to u8aToBn (as with variable length)
switch (u8a[0] & 0b11) {
case 0b00:
return [1, u8a[0] >>> 2];
case 0b01:
return [2, (u8a[0] + (u8a[1] * 0x1_00)) >>> 2];
case 0b10:
return [4, (u8a[0] + (u8a[1] * 0x1_00) + (u8a[2] * 0x1_00_00) + (u8a[3] * 0x1_00_00_00)) >>> 2];
// 0b11
default:
break;
}
// add 5 to shifted (4 for base length, 1 for this byte)
const offset = (u8a[0] >>> 2) + 5;
// we unroll the loop
switch (offset) {
// there still could be 4 bytes data, similar to 0b10 above (with offsets)
case 5:
return [5, u8a[1] + (u8a[2] * 0x1_00) + (u8a[3] * 0x1_00_00) + (u8a[4] * 0x1_00_00_00)];
case 6:
return [6, u8a[1] + (u8a[2] * 0x1_00) + (u8a[3] * 0x1_00_00) + (u8a[4] * 0x1_00_00_00) + (u8a[5] * 0x1_00_00_00_00)];
// 6 bytes data is the maximum, 48 bits (56 would overflow)
case 7:
return [7, u8a[1] + (u8a[2] * 0x1_00) + (u8a[3] * 0x1_00_00) + (u8a[4] * 0x1_00_00_00) + (u8a[5] * 0x1_00_00_00_00) + (u8a[6] * 0x1_00_00_00_00_00)];
// for anything else, use the non-unrolled version
default:
throw new Error('Compact input is > Number.MAX_SAFE_INTEGER');
}
}
+1 -1
View File
@@ -24,5 +24,5 @@
export { compactAddLength } from './addLength';
export { compactStripLength } from './stripLength';
export { compactFromU8a, compactFromU8aStrict } from './fromU8a';
export { compactFromU8a, compactFromU8aLim } from './fromU8a';
export { compactToU8a } from './toU8a';
+1 -9
View File
@@ -33,16 +33,8 @@ export function isAscii (value?: U8aLike | null): boolean {
if (value) {
return isStringIn && !isHex(value)
? value.toString().split('').every(isAsciiChar)
: isAsciiStrict(u8aToU8a(value));
: u8aToU8a(value).every(isAsciiByte);
}
return isStringIn;
}
/**
* @name isAsciiStrict
* @description A strict version of [[isAscii]], accepting only Uint8Array inputs
*/
export function isAsciiStrict (u8a: Uint8Array): boolean {
return u8a.every(isAsciiByte);
}
+2 -2
View File
@@ -5,7 +5,7 @@
* @summary Type checking utilities
*/
export { isArray } from './array';
export { isAscii, isAsciiStrict } from './ascii';
export { isAscii } from './ascii';
export { isBigInt } from './bigInt';
export { isBn } from './bn';
export { isBuffer } from './buffer';
@@ -30,5 +30,5 @@ export { isToBigInt } from './toBigInt';
export { isToBn } from './toBn';
export { isU8a } from './u8a';
export { isUndefined } from './undefined';
export { isUtf8, isUtf8Strict } from './utf8';
export { isUtf8 } from './utf8';
export { isWasm } from './wasm';
+1 -8
View File
@@ -19,14 +19,7 @@ export function isUtf8 (value?: HexString | number[] | Buffer | Uint8Array | str
return isString(value);
}
return isUtf8Strict(u8aToU8a(value));
}
/**
* @name isUtf8Strict
* @description A strict version of [[isUtf8]], acception only Uint8Array inpouts
*/
export function isUtf8Strict (u8a: Uint8Array): boolean {
const u8a = u8aToU8a(value);
const len = u8a.length;
let i = 0;
+2 -8
View File
@@ -22,14 +22,8 @@ import { u8aToU8a } from './toU8a';
* ```
*/
export function u8aCmp (a: HexString | Uint8Array | string, b: HexString | Uint8Array | string): number {
return u8aCmpStrict(u8aToU8a(a), u8aToU8a(b));
}
/**
* @name u8aCmpStrict
* @description A strict version of [[u8aCmp]], accepting only Uint8Array inputs
*/
export function u8aCmpStrict (u8aa: Uint8Array, u8ab: Uint8Array): number {
const u8aa = u8aToU8a(a);
const u8ab = u8aToU8a(b);
let i = 0;
while (true) {
+2 -7
View File
@@ -20,14 +20,9 @@ import { u8aToU8a } from './toU8a';
* ```
*/
export function u8aEq (a: HexString | Uint8Array | string, b: HexString | Uint8Array | string): boolean {
return u8aEqStrict(u8aToU8a(a), u8aToU8a(b));
}
const u8aa = u8aToU8a(a);
const u8ab = u8aToU8a(b);
/**
* @name u8aEqStrict
* @description A strict version of [[u8aCmp]], accepting only Uint8Array inputs
*/
export function u8aEqStrict (u8aa: Uint8Array, u8ab: Uint8Array): boolean {
if (u8aa.length === u8ab.length) {
const dvA = new DataView(u8aa.buffer, u8aa.byteOffset);
const dvB = new DataView(u8ab.buffer, u8ab.byteOffset);
+2 -2
View File
@@ -5,10 +5,10 @@
* @summary Utility methods to convert to and from `Uint8Array` objects
*/
export { u8aCmp, u8aCmpStrict } from './cmp';
export { u8aCmp } from './cmp';
export { u8aConcat, u8aConcatStrict } from './concat';
export { u8aEmpty } from './empty';
export { u8aEq, u8aEqStrict } from './eq';
export { u8aEq } from './eq';
export { u8aFixLength } from './fixLength';
export { u8aSorted } from './sorted';
export { u8aToBigInt } from './toBigInt';