From 3b5de80c544795e1f86257e67b6f999c7ad5ec58 Mon Sep 17 00:00:00 2001 From: Jaco Date: Wed, 25 May 2022 06:26:07 +0200 Subject: [PATCH] Compact loop unrolling (#1502) --- packages/util/src/compact/fromU8a.spec.ts | 3 ++- packages/util/src/compact/fromU8a.ts | 30 ++++++++++++++++----- packages/util/src/compact/toU8a.spec.ts | 12 ++++++++- packages/util/src/u8a/toBn.spec.ts | 32 +++++++++++++++++++++-- packages/util/src/u8a/toBn.ts | 22 +++++++++++++++- 5 files changed, 88 insertions(+), 11 deletions(-) diff --git a/packages/util/src/compact/fromU8a.spec.ts b/packages/util/src/compact/fromU8a.spec.ts index 6ea634831..c4899827b 100644 --- a/packages/util/src/compact/fromU8a.spec.ts +++ b/packages/util/src/compact/fromU8a.spec.ts @@ -76,5 +76,6 @@ 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('compactFromU8a (u64)', 250_000, [[hexToU8a('0x0b00407a10f35a')]], compactFromU8a); + 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); }); diff --git a/packages/util/src/compact/fromU8a.ts b/packages/util/src/compact/fromU8a.ts index 8db32d84b..787ee7c6f 100644 --- a/packages/util/src/compact/fromU8a.ts +++ b/packages/util/src/compact/fromU8a.ts @@ -30,16 +30,34 @@ export function compactFromU8a (input: U8aLike): [number, BN] { return [1, new BN(u8a[0] >>> 2)]; case 0b01: - return [2, new BN((u8a[0] >>> 2) + (u8a[1] << 6))]; + return [2, new BN((u8a[0] + (u8a[1] * 0x1_00)) >>> 2)]; case 0b10: - return [4, new BN((u8a[0] >>> 2) + (u8a[1] << 6) + (u8a[2] << 14) + (u8a[3] << 22))]; + return [4, new BN((u8a[0] + (u8a[1] * 0x1_00) + (u8a[2] * 0x1_00_00) + (u8a[3] * 0x1_00_00_00)) >>> 2)]; - default: { // 0b11 - // add 5 to shifted (4 for base length, 1 for this byte) - const offset = (u8a[0] >>> 2) + 5; + // 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, new BN(u8a[1] + (u8a[2] * 0x1_00) + (u8a[3] * 0x1_00_00) + (u8a[4] * 0x1_00_00_00))]; + + case 6: + return [6, new BN(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, new BN(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: return [offset, u8aToBn(u8a.subarray(1, offset))]; - } } } diff --git a/packages/util/src/compact/toU8a.spec.ts b/packages/util/src/compact/toU8a.spec.ts index a332d92d6..1fc94dcf2 100644 --- a/packages/util/src/compact/toU8a.spec.ts +++ b/packages/util/src/compact/toU8a.spec.ts @@ -61,7 +61,7 @@ describe('encode', (): void => { ); }); - it('encodes a large value', (): void => { + it('encodes a large value (6 bytes)', (): void => { expect( compactToU8a( new BN('00005af3107a4000', 16) @@ -71,6 +71,16 @@ describe('encode', (): void => { ); }); + it('encodes a large value (9 bytes)', (): void => { + expect( + compactToU8a( + new BN('1200005af3107a4034', 16) + ) + ).toEqual( + new Uint8Array([23, 52, 0x40, 0x7a, 0x10, 0xf3, 0x5a, 0, 0, 18]) + ); + }); + it('does not modify the original', (): void => { const original = new BN(123456); diff --git a/packages/util/src/u8a/toBn.spec.ts b/packages/util/src/u8a/toBn.spec.ts index 5ba202635..ef7f609c3 100644 --- a/packages/util/src/u8a/toBn.spec.ts +++ b/packages/util/src/u8a/toBn.spec.ts @@ -37,7 +37,7 @@ describe('u8aToBn', (): void => { ).toBe('1234'); }); - it('converts values (little-endian)', (): void => { + it('converts values (little-endian, u16)', (): void => { expect( u8aToBn( new Uint8Array([0x12, 0x34]), @@ -46,6 +46,33 @@ describe('u8aToBn', (): void => { ).toBe('3412'); }); + it('converts values (little-endian, u32)', (): void => { + expect( + u8aToBn( + new Uint8Array([0x12, 0x34, 0x56, 0x78]), + { isLe: true } + ).toString(16) + ).toBe('78563412'); + }); + + it('converts values (big-endian, u16)', (): void => { + expect( + u8aToBn( + new Uint8Array([0x12, 0x34]), + { isLe: false } + ).toString(16) + ).toBe('1234'); + }); + + it('converts values (big-endian, u32)', (): void => { + expect( + u8aToBn( + new Uint8Array([0x12, 0x34, 0x56, 0x78]), + { isLe: false } + ).toString(16) + ).toBe('12345678'); + }); + describe('empty creation', (): void => { it('handles unsigned (le)', (): void => { expect( @@ -136,5 +163,6 @@ describe('u8aToBn', (): void => { ).toBe(true); }); - performance('u8aToBn', 500000, [[new Uint8Array([0x68, 0x65, 0x6c, 0x6c])]], u8aToBn); + performance('u8aToBn (u32)', 500000, [[new Uint8Array([0x68, 0x65, 0x6c, 0x6c])]], u8aToBn); + performance('u8aToBn (u64)', 500000, [[new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x68, 0x65, 0x6c, 0x6c])]], u8aToBn); }); diff --git a/packages/util/src/u8a/toBn.ts b/packages/util/src/u8a/toBn.ts index 2b7edff87..bc0383013 100644 --- a/packages/util/src/u8a/toBn.ts +++ b/packages/util/src/u8a/toBn.ts @@ -38,7 +38,27 @@ function u8aToBn (value: Uint8Array, options: ToBnOptions | boolean = {}): BN { ? { isLe: options } : options ); - const bn = new BN(value, isLe ? 'le' : 'be'); + const count = value.length; + let bn: BN; + + // shortcut for <= u48 values - in this case the manual conversion + // here seems to be more efficient than passing the full array + // (and yes, it can be u48, e.g. in compacts with variable lengths) + if (count <= 6) { + let result = 0; + + for (let i = 0; i < count; i++) { + result = (result * 0x1_00) + ( + isLe + ? value[count - 1 - i] + : value[i] + ); + } + + bn = new BN(result); + } else { + bn = new BN(value, isLe ? 'le' : 'be'); + } return isNegative && value.length ? bn.fromTwos(value.length * 8)