From d1fb6e1f55c603241e1180f5ec80b5fd8b5f4aaf Mon Sep 17 00:00:00 2001 From: Roy Crihfield Date: Wed, 6 Sep 2023 20:12:59 +0800 Subject: [PATCH] symdiff - skip identical subtrees --- utils/iterator.go | 10 +++++----- utils/iterator_test.go | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/utils/iterator.go b/utils/iterator.go index a2486df..d02cff7 100644 --- a/utils/iterator.go +++ b/utils/iterator.go @@ -138,10 +138,13 @@ func (it *symmDiffIterator) seek() { it.yieldFromA = false return case 0: - if it.a.Next(true) { + // if A and B have the same path and non-zero hash, they are identical and we can skip + // the whole subtree + noHash := it.a.Hash() == common.Hash{} + if it.a.Next(noHash) { it.count++ } - if it.b.Next(true) { + if it.b.Next(noHash) { it.count++ } } @@ -156,9 +159,6 @@ func (it *symmDiffIterator) Error() error { } func compareNodes(a, b trie.NodeIterator) int { - // if cmp := bytes.Compare(a.Path(), b.Path()); cmp != 0 { - // return cmp - // } if a.Leaf() && !b.Leaf() { return -1 } else if b.Leaf() && !a.Leaf() { diff --git a/utils/iterator_test.go b/utils/iterator_test.go index 3473c63..0ce0078 100644 --- a/utils/iterator_test.go +++ b/utils/iterator_test.go @@ -50,7 +50,7 @@ func TestSymmetricDifferenceIterator(t *testing.T) { for di.Next(true) { t.Errorf("iterator should not yield any elements") } - assert.Equal(t, 4, *count) + assert.Equal(t, 2, *count) trieb := trie.NewEmpty(db) di, count = utils.NewSymmetricDifferenceIterator(triea.NodeIterator([]byte("food")), trieb.NodeIterator(nil))