Skip to content

Commit

Permalink
feat: optimize ContainerNodeStruct.valueToTree()
Browse files Browse the repository at this point in the history
  • Loading branch information
twoeths committed Jun 10, 2024
1 parent 8d5fcf9 commit 5dd3f23
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 9 deletions.
8 changes: 7 additions & 1 deletion packages/ssz/src/type/byteArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export abstract class ByteArrayType extends CompositeType<ByteArray, ByteArray,
return this.commitViewDU(view);
}

// TODO: batch
// TODO - batch
commitViewDU(view: ByteArray, hashComps: HashComputationGroup | null = null): Node {
const uint8Array = new Uint8Array(this.value_serializedSize(view));
const dataView = new DataView(uint8Array.buffer, uint8Array.byteOffset, uint8Array.byteLength);
Expand Down Expand Up @@ -70,6 +70,12 @@ export abstract class ByteArrayType extends CompositeType<ByteArray, ByteArray,
return Uint8Array.prototype.slice.call(data.uint8Array, start, end);
}

value_toTree(value: ByteArray): Node {
// this saves 1 allocation of Uint8Array
const dataView = new DataView(value.buffer, value.byteOffset, value.byteLength);
return this.tree_deserializeFromBytes({uint8Array: value, dataView}, 0, value.length);
}

// Merkleization

protected getRoots(value: ByteArray): Uint8Array[] {
Expand Down
10 changes: 4 additions & 6 deletions packages/ssz/src/type/containerNodeStruct.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Node} from "@chainsafe/persistent-merkle-tree";
import {Node, subtreeFillToContents} from "@chainsafe/persistent-merkle-tree";
import {Type, ByteViews} from "./abstract";
import {isCompositeType} from "./composite";
import {ContainerType, ContainerOptions, renderContainerTypeName} from "./container";
Expand Down Expand Up @@ -106,11 +106,9 @@ export class ContainerNodeStructType<Fields extends Record<string, Type<unknown>
return new BranchNodeStruct(this.valueToTree.bind(this), value);
}

// TODO: Optimize conversion
private valueToTree(value: ValueOfFields<Fields>): Node {
const uint8Array = new Uint8Array(this.value_serializedSize(value));
const dataView = new DataView(uint8Array.buffer, uint8Array.byteOffset, uint8Array.byteLength);
this.value_serializeToBytes({uint8Array, dataView}, 0, value);
return super.tree_deserializeFromBytes({uint8Array, dataView}, 0, uint8Array.length);
// TODO - batch get hash computations while creating tree
const nodes = this.fieldsEntries.map(({fieldName, fieldType}) => fieldType.value_toTree(value[fieldName]));
return subtreeFillToContents(nodes, this.depth);
}
}
2 changes: 1 addition & 1 deletion packages/ssz/src/type/optional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export class OptionalType<ElementType extends Type<unknown>> extends CompositeTy
}

// TODO add an OptionalViewDU
// TODO: batch
// TODO - batch
commitViewDU(view: ValueOfType<ElementType>, hashComps: HashComputationGroup | null = null): Node {
return this.value_toTree(view);
}
Expand Down
6 changes: 6 additions & 0 deletions packages/ssz/src/type/uint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ export class UintNumberType extends BasicType<number> {
}
}

value_toTree(value: number): Node {
const node = LeafNode.fromZero();
node.setUint(this.byteLength, 0, value, this.clipInfinity);
return node;
}

tree_serializeToBytes(output: ByteViews, offset: number, node: Node): number {
const value = (node as LeafNode).getUint(this.byteLength, 0, this.clipInfinity);
this.value_serializeToBytes(output, offset, value);
Expand Down
2 changes: 1 addition & 1 deletion packages/ssz/src/type/union.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export class UnionType<Types extends Type<unknown>[]> extends CompositeType<
return this.value_toTree(view);
}

// TODO: batch
// TODO - batch
commitViewDU(view: ValueOfTypes<Types>, hashComps: HashComputationGroup | null = null): Node {
return this.value_toTree(view);
}
Expand Down

0 comments on commit 5dd3f23

Please sign in to comment.