From fffd7620218db9f1b8f3cadf305975ad68aac004 Mon Sep 17 00:00:00 2001 From: Tuyen Nguyen Date: Wed, 12 Mar 2025 15:53:34 +0700 Subject: [PATCH] fix: do not rebind node if unchanged child ViewDUs --- packages/ssz/src/viewDU/arrayComposite.ts | 9 ++++++--- packages/ssz/src/viewDU/container.ts | 9 ++++++--- packages/ssz/test/unit/unchangedViewDUs.test.ts | 2 +- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/packages/ssz/src/viewDU/arrayComposite.ts b/packages/ssz/src/viewDU/arrayComposite.ts index aa7e2be4..e2374cd1 100644 --- a/packages/ssz/src/viewDU/arrayComposite.ts +++ b/packages/ssz/src/viewDU/arrayComposite.ts @@ -195,9 +195,12 @@ export class ArrayCompositeTreeViewDU< for (const [index, view] of this.viewsChanged) { const node = this.type.elementType.commitViewDU(view, offsetView, byLevelView); - // Set new node in nodes array to ensure data represented in the tree and fast nodes access is equal - this.nodes[index] = node; - nodesChanged.push({index, node}); + // there's a chance the view is not changed, no need to rebind nodes in that case + if (this.nodes[index] !== node) { + // Set new node in nodes array to ensure data represented in the tree and fast nodes access is equal + this.nodes[index] = node; + nodesChanged.push({index, node}); + } // Cache the view's caches to preserve it's data after 'this.viewsChanged.clear()' const cache = this.type.elementType.cacheOfViewDU(view); diff --git a/packages/ssz/src/viewDU/container.ts b/packages/ssz/src/viewDU/container.ts index ab71749c..f38c1925 100644 --- a/packages/ssz/src/viewDU/container.ts +++ b/packages/ssz/src/viewDU/container.ts @@ -101,9 +101,12 @@ export class BasicContainerTreeViewDU { const state = sszAltair.BeaconState.toViewDU(getRandomState(100)); - it.skip("should not recompute batchHashTreeRoot() when no fields is changed", () => { + it("should not recompute batchHashTreeRoot() when no fields is changed", () => { const root = state.batchHashTreeRoot(); // this causes viewsChanged inside BeaconState container state.validators.length;