Skip to content
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

Optimize SSZ collection tree creation #9088

Closed
wants to merge 17 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update SszOptionalSchemaImpl.java
  • Loading branch information
VolodymyrBg authored Feb 10, 2025
commit 1c1a1f153d4d9ed310bc6fa71981d44642cd9800
Original file line number Diff line number Diff line change
Expand Up @@ -228,20 +228,34 @@ public TreeNode loadBackingNodes(
return getDefaultTree();
}

final CompressedBranchInfo branchData = nodeSource.loadBranchNode(rootHash, rootGIndex);
checkState(
branchData.getChildren().length == 2, "Optional root node must have exactly two child");
checkState(branchData.getDepth() == 1, "Optional root node must have depth of 1");
final Bytes32 valueHash = branchData.getChildren()[0];
final Bytes32 optionalHash = branchData.getChildren()[1];
final int isPresent =
nodeSource
.loadLeafNode(optionalHash, GIndexUtil.gIdxRightGIndex(rootGIndex))
.getInt(0, ByteOrder.LITTLE_ENDIAN);
checkState(isPresent <= IS_PRESENT_PREFIX, "Selector is out of bounds");
final TreeNode valueNode =
childSchema.loadBackingNodes(nodeSource, valueHash, GIndexUtil.gIdxLeftGIndex(rootGIndex));
return createTreeNode(valueNode, isPresent == IS_PRESENT_PREFIX);
try {
final CompressedBranchInfo branchData = nodeSource.loadBranchNode(rootHash, rootGIndex);
checkState(
branchData.getChildren().length == 2, "Optional root node must have exactly two children");
checkState(branchData.getDepth() == 1, "Optional root node must have depth of 1");
final Bytes32 valueHash = branchData.getChildren()[0];
final Bytes32 optionalHash = branchData.getChildren()[1];

// Handle empty optional case
if (optionalHash.equals(Bytes32.ZERO)) {
return getDefaultTree();
}

final TreeNode optionalNode = nodeSource.loadLeafNode(optionalHash, GIndexUtil.gIdxRightGIndex(rootGIndex));
final int isPresent = optionalNode.get(0, ByteOrder.LITTLE_ENDIAN);
checkState(isPresent <= IS_PRESENT_PREFIX, "Selector is out of bounds");

if (isPresent == 0) {
return getDefaultTree();
}

final TreeNode valueNode =
childSchema.loadBackingNodes(nodeSource, valueHash, GIndexUtil.gIdxLeftGIndex(rootGIndex));
return createTreeNode(valueNode, true);
} catch (Exception e) {
// If loading fails, return default tree
return getDefaultTree();
}
}

@Override
Expand Down