Skip to content

Implement BitArray and replace trie.Key#2322

Merged
weiihann merged 63 commits intomainfrom
weiihann/bitarray
Jan 23, 2025
Merged

Implement BitArray and replace trie.Key#2322
weiihann merged 63 commits intomainfrom
weiihann/bitarray

Conversation

@weiihann
Copy link
Copy Markdown
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

@codecov
Copy link
Copy Markdown

codecov Bot commented Dec 15, 2024

Codecov Report

Attention: Patch coverage is 75.57252% with 160 lines in your changes missing coverage. Please review.

Project coverage is 74.63%. Comparing base (b45cae5) to head (8f22ebd).
Report is 26 commits behind head on main.

Files with missing lines Patch % Lines
core/trie/bitarray.go 72.26% 143 Missing and 4 partials ⚠️
core/trie/proof.go 88.09% 3 Missing and 2 partials ⚠️
core/trie/trie.go 90.90% 2 Missing and 2 partials ⚠️
core/trie/node.go 83.33% 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.92%   74.63%   -0.29%     
==========================================
  Files         111      111              
  Lines       12171    12581     +410     
==========================================
+ Hits         9119     9390     +271     
- Misses       2353     2489     +136     
- Partials      699      702       +3     

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

Copy link
Copy Markdown
Contributor

@rodrodros rodrodros 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!

Comment thread core/trie/bitarray.go
Comment thread core/trie/bitarray.go Outdated
Comment thread core/trie/bitarray.go Outdated
Comment thread core/trie/bitarray.go
Comment thread core/trie/bitarray.go Outdated
Comment thread core/trie/bitarray.go Outdated
@weiihann weiihann marked this pull request as ready for review December 18, 2024 02:35
Comment thread core/state.go Outdated
Comment thread core/trie/bitarray.go
Comment thread core/trie/bitarray.go Outdated
Comment thread core/trie/bitarray.go Outdated
Comment thread core/trie/bitarray.go Outdated
Comment thread core/trie/bitarray.go Outdated
Comment thread core/trie/bitarray.go
Comment thread core/trie/proof.go
Comment thread core/trie/bitarray.go Outdated
@weiihann weiihann force-pushed the weiihann/bitarray branch 3 times, most recently from c7fd009 to 2692494 Compare December 24, 2024 03:02
@weiihann weiihann force-pushed the weiihann/bitarray branch 3 times, most recently from 7ba050a to 7f479c1 Compare December 30, 2024 10:56
Comment thread core/trie/bitarray.go Outdated
Comment thread core/trie/bitarray.go
Comment thread core/trie/bitarray.go
Comment thread core/trie/bitarray.go Outdated
Comment thread core/trie/bitarray.go
Comment thread core/trie/bitarray.go
Comment thread core/trie/bitarray.go
@weiihann weiihann force-pushed the weiihann/bitarray branch 4 times, most recently from 6e68d5f to b67ff0a Compare January 17, 2025 08:07
Comment thread core/trie/bitarray.go
Comment thread core/trie/bitarray.go
Comment thread core/trie/bitarray.go Outdated
Comment thread core/trie/bitarray.go
Comment thread core/trie/bitarray.go
Comment thread core/trie/bitarray.go
@weiihann weiihann merged commit 4d99dd0 into main Jan 23, 2025
@weiihann weiihann deleted the weiihann/bitarray branch January 23, 2025 02:09
@weiihann weiihann added the State All things related to state label Feb 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

State All things related to state

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants