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

Pure HashAndNibblize function #13453

Merged
merged 3 commits into from
Jan 16, 2025
Merged

Pure HashAndNibblize function #13453

merged 3 commits into from
Jan 16, 2025

Conversation

antonis19
Copy link
Member

@antonis19 antonis19 commented Jan 15, 2025

This PR extracts HashAndNibblize as a utility function, not dependent on HexPatriciaTrieHashed .

Doing some benchmark comparisons this change doesn't impact performance (in fact it looks to make an improvement).

These are the comparisons:

  • For 25 million keys:

    • (before) hph.HashAndNibblizeKey: 136.664s
    • (after) pure HashAndNibblizeKey : 108.215s
  • For 35 million keys:

    • (before) hph.HashAndNibblizeKey: 351.598s
    • (after) pure HashAndNibblizeKey : 326.218s
  • For 50 million keys:

    • (before) hph.HashAndNibblizeKey: 1055.217s
    • (after) pure HashAndNibblizeKey : 568.075s

@@ -138,7 +138,7 @@ func InitializeTrieAndUpdates(tv TrieVariant, mode Mode, tmpdir string) (Trie, *
default:

trie := NewHexPatriciaHashed(length.Addr, nil, tmpdir)
tree := NewUpdates(mode, tmpdir, trie.HashAndNibblizeKey)
tree := NewUpdates(mode, tmpdir, HashAndNibblizeKey)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets specify count of nibbles per byte. Since this is for hex trie, it should be 2.
HashKeyToNibbles2 or HashKeyForHexTrie so we could have different HashKeyFor*. usage of this exact function is strictly bounded to preparing hex trie paths from plain keys

@@ -0,0 +1,138 @@
package commitment
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

contents of file is not kind of utils but around plain key encoding back and forth. Keys.go or nibbles.go would better reflect it's contents.

ecrypto "github.com/erigontech/erigon-lib/crypto"
)

// Hash the account or storage key and return the resulting hashed key in the nibblized form
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update comment that this

  • expects plain key
  • splits each byte onto 2 nibbles => hex patricia only

// Hash the account or storage key and return the resulting hashed key in the nibblized form
func HashAndNibblizeKey(key []byte) []byte {
keyLen := length.Addr
if len(key) < length.Addr {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keyLen = min(a, b)

if len(key) < length.Addr {
keyLen = len(key)
}
compactedHashKey := ecrypto.Keccak256(key[:keyLen])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its not compacted, it's hashedKey of address part

@awskii awskii requested a review from shohamc1 January 16, 2025 15:53
@awskii awskii enabled auto-merge (squash) January 16, 2025 15:53
@awskii awskii merged commit 5fcefc6 into main Jan 16, 2025
13 checks passed
@awskii awskii deleted the pure-hash-and-nibblize branch January 16, 2025 16:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants