-
Notifications
You must be signed in to change notification settings - Fork 623
docs: add docs on state vars docs re packing in structs #20747
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
Merged
nventuro
merged 4 commits into
merge-train/fairies
from
nico/f-341-add-note-on-state_vars-docs-re-packing-in-structs
Feb 23, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6a367a7
refactor: split traits module into separate files per trait
nventuro d408337
Add docs on packing and tight packing
nventuro e34aba0
Apply suggestions from code review
nventuro 3bc7f5b
Merge branch 'merge-train/fairies' into nico/f-341-add-note-on-state_…
nventuro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -7,6 +7,62 @@ | |||||||
| //! Due to Aztec contracts being able to store both public and private state, there are many more different types of | ||||||||
| //! state variables, each with their nuance and use cases. Understanding these is key to understanding how a contract | ||||||||
| //! works. | ||||||||
| //! | ||||||||
| //! ## Packing for Efficient Access | ||||||||
| //! | ||||||||
| //! Because all state variables are fully independent, when a contract reads or writes one of them all others are left | ||||||||
| //! untouched. This is good for isolation, but in some cases users may want to group variables together for more | ||||||||
| //! efficient access, for example to access multiple values in a single storage read or write. | ||||||||
| //! | ||||||||
| //! The pattern to follow is to **group related values in a `struct`**, just like in Solidity. How values are packed | ||||||||
| //! inside this `struct` is governed by the [`crate::protocol::traits::Packable`] trait, which must be `#[derive]`'d or | ||||||||
| //! manually implemented - see the [`Packable`](crate::protocol::traits::Packable)'s docs on how to do this. | ||||||||
| //! | ||||||||
| //! ```noir | ||||||||
| //! // Inefficient reads and writes - each bool is assigned a distinct storage slot, so reading or writing both | ||||||||
| //! requires | ||||||||
| //! // executing `SLOAD` or `SSTORE` twice. | ||||||||
| //! #[storage] | ||||||||
| //! struct Storage<C> { | ||||||||
| //! a: PublicMutable<bool, C>, | ||||||||
| //! b: PublicMutable<bool, C>, | ||||||||
| //! } | ||||||||
| //! | ||||||||
| //! // By storing the booleans in a single struct and implementing the Packable trait with tight packing, we can now | ||||||||
| //! // read and write both values in a single `SLOAD` or `SSTORE` opcode. | ||||||||
| //! struct TwoBooleans { | ||||||||
| //! a: bool, | ||||||||
| //! b: bool, | ||||||||
| //! } | ||||||||
| //! | ||||||||
| //! impl aztec::protocol::traits::Packable for TwoBooleans { | ||||||||
| //! let N: u32 = 1; | ||||||||
| //! | ||||||||
| //! fn pack(self) -> [Field; Self::N] { | ||||||||
| //! [(self.a as Field) * 2.pow_32(1) + (self.b as Field)] | ||||||||
| //! } | ||||||||
| //! | ||||||||
| //! fn unpack(packed: [Field; Self::N]) -> Self { | ||||||||
| //! let b = (packed[0] as u1) != 0; | ||||||||
| //! let a = (((packed[0] - b as Field) / 2.pow_32(1)) as u1) != 0; | ||||||||
| //! | ||||||||
| //! Self { a, b } | ||||||||
| //! } | ||||||||
| //! } | ||||||||
| //! | ||||||||
| //! #[storage] | ||||||||
| //! struct Storage<C> { | ||||||||
| //! a_and_b: PublicMutable<TwoBooleans, C>, | ||||||||
| //! } | ||||||||
| //! ``` | ||||||||
| //! | ||||||||
| //! Note that private state variables and public ones that can be read from private (like | ||||||||
| //! [`PublicImmutable`](crate::state_vars::PublicImmutable) and | ||||||||
| //! [`DelayedPublicMutable`](crate::state_vars::DelayedPublicMutable)) benefit from packing multiple values in the same | ||||||||
| //! `struct` even if there is no need for tight packing (e.g. if all values are `Field`s), since they often work by | ||||||||
| //! reading the _hash_ of the entire value. Many values in the same `struct` will result in a single hash, and | ||||||||
| //! therefore | ||||||||
| //! a single read. | ||||||||
|
Comment on lines
+64
to
+65
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
broken formatting |
||||||||
|
|
||||||||
| mod state_variable; | ||||||||
| pub use state_variable::StateVariable; | ||||||||
|
|
||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.