-
-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fast conversion from decimal to bytes #143
Comments
will investigate 👍 |
with ChainSafe/persistent-merkle-tree#52, we can get deserialized number if it contains less than 32 bits (right now we have to convert it.only("get deserialized number from h0", () => {
const BeaconState = new ContainerType({
fields: {
slot: number64Type,
}
});
for (let slot = 2 ** 31 - 100; slot <= 2 ** 31; slot++) {
const tb = BeaconState.createTreeBackedFromStruct({slot});
const node = tb.tree.getNode(BigInt(1));
expect(Math.abs(node.h0)).to.be.equal(slot, "failed at slot " + slot);
}
}); it should work with slot, committee index etc. but not with balance |
I've tested using TypedArrays to convert numbers and it's actually slower than the current implementation in as-sha256
/**
* Pass 8 numbers in an object and set that to inputArray.
* This function contains multiple same procedures but we intentionally
* do it step by step to improve performance a bit.
**/
export function hashObjectToByteArray(obj, byteArr, offset) {
const uint32 = new Uint32Array(byteArr.buffer);
uint32[0] = obj.h0;
uint32[1] = obj.h1;
uint32[2] = obj.h2;
uint32[3] = obj.h3;
uint32[4] = obj.h4;
uint32[5] = obj.h5;
uint32[6] = obj.h6;
uint32[7] = obj.h7;
}
/**
* Parse outputArray into an object of 8 numbers.
* This is the order that makes Uint32Array the same to Uint8Array
* This function contains multiple same procedures but we intentionally
* do it step by step to improve performance a bit.
**/
export function byteArrayToHashObject(byteArr) {
const uint32 = new Uint32Array(byteArr.buffer);
return {
h0: uint32[0],
h1: uint32[1],
h2: uint32[2],
h3: uint32[3],
h4: uint32[4],
h5: uint32[5],
h6: uint32[6],
h7: uint32[7],
};
} What makes it so slow is the constructor |
@dapplion just to confim, the |
Yes it's little endian |
|
@g11tech Thanks! Keep us updated |
@dapplion got 88% speedup on normal uint tree backed (will need to check on > 2^32 number increment test but should be 30% plus over there), marginal 4% better performance on the new uint64 tree backed. will continue checking this further a bit more. |
Lodestar spends significant CPU time converting uint types from decimal to bytes and back. For example, converting the balance tree to flat and back consist of doing 200_000 ops of those.
SSZ is the one hosting the code that does the conversions. This is one of the implementations that participate in this conversions.
ssz/src/types/basic/uint.ts
Lines 68 to 82 in be03a33
ssz/src/types/basic/uint.ts
Lines 84 to 98 in be03a33
We should investigate faster alternatives
The text was updated successfully, but these errors were encountered: