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

Implement BitArray and replace trie.Key #2322

Open
wants to merge 31 commits into
base: main
Choose a base branch
from
Open

Conversation

weiihann
Copy link
Contributor

@weiihann weiihann commented Dec 15, 2024

This PR implements a new data structure BitArray to replace trie.Key. The reasons being:

  1. Misleading representation
    trie.Key doesn't actually just represent a Trie key in our use case. But rather, it’s a special data type that we use to just represent a fixed size of bit array with the number of bits used specified.

  2. Inefficient operations
    The current implementation of the bitwise operations is scattered all around the place. We implement ourselves for some, and for others, we rely on conversion. BitArray implements everything from scratch without unnecessary conversion.

Regarding BitArray:

  • It uses a little-endian format to perform bitwise operations more efficiently. All available operations are multiple times faster than trie.Key
  • It is alloc-free
  • It supports all use cases for trie and proofs (e.g. derive path from two bit arrays, check if path matches, etc)
  • Adds built-in support for conversion (e.g. felt to bit array, bytes to bit array, etc)

Credits: some implementation details are inspired by https://github.com/holiman/uint256

Copy link

codecov bot commented Dec 15, 2024

Codecov Report

Attention: Patch coverage is 91.79894% with 31 lines in your changes missing coverage. Please review.

Project coverage is 74.83%. Comparing base (4ba933f) to head (fa237e8).

Files with missing lines Patch % Lines
core/trie/bitarray.go 92.85% 16 Missing and 2 partials ⚠️
core/trie/proof.go 87.80% 3 Missing and 2 partials ⚠️
core/trie/trie.go 90.24% 2 Missing and 2 partials ⚠️
core/trie/node.go 84.21% 1 Missing and 2 partials ⚠️
migration/migration.go 85.71% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2322      +/-   ##
==========================================
+ Coverage   74.42%   74.83%   +0.41%     
==========================================
  Files         110      110              
  Lines       11771    11901     +130     
==========================================
+ Hits         8760     8906     +146     
+ Misses       2329     2322       -7     
+ Partials      682      673       -9     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link

@rodrigo-pino rodrigo-pino left a comment

Choose a reason for hiding this comment

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

Just some comments. Let. me know what you think. Thanks for detailing the implementation!

core/trie/bitarray.go Show resolved Hide resolved
core/trie/bitarray.go Outdated Show resolved Hide resolved
core/trie/bitarray.go Outdated Show resolved Hide resolved
core/trie/bitarray.go Show resolved Hide resolved
core/trie/bitarray.go Outdated Show resolved Hide resolved
core/trie/bitarray.go Outdated Show resolved Hide resolved
@weiihann weiihann marked this pull request as ready for review December 18, 2024 02:35
core/state.go Outdated Show resolved Hide resolved
core/trie/bitarray.go Show resolved Hide resolved
core/trie/bitarray.go Outdated Show resolved Hide resolved
core/trie/bitarray.go Outdated Show resolved Hide resolved
core/trie/bitarray.go Outdated Show resolved Hide resolved
core/trie/bitarray.go Outdated Show resolved Hide resolved
core/trie/bitarray.go Show resolved Hide resolved
core/trie/proof.go Show resolved Hide resolved
core/trie/bitarray.go Outdated Show resolved Hide resolved
@weiihann weiihann force-pushed the weiihann/bitarray branch 3 times, most recently from c7fd009 to 2692494 Compare December 24, 2024 03:02
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.

2 participants