Compact loop unrolling (#1502)

This commit is contained in:
Jaco
2022-05-25 07:26:07 +03:00
committed by GitHub
parent 041f5a7c2b
commit 3b5de80c54
5 changed files with 88 additions and 11 deletions
+2 -1
View File
@@ -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);
});
+24 -6
View File
@@ -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))];
}
}
}
+11 -1
View File
@@ -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);
+30 -2
View File
@@ -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);
});
+21 -1
View File
@@ -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)