Skip to content

feat: optimizing SharedMutable storage slots#11817

Merged
benesjan merged 18 commits into
masterfrom
02-07-feat_optimizing_sharedmutable_sotrage_slots
Feb 20, 2025
Merged

feat: optimizing SharedMutable storage slots#11817
benesjan merged 18 commits into
masterfrom
02-07-feat_optimizing_sharedmutable_sotrage_slots

Conversation

@benesjan

@benesjan benesjan commented Feb 7, 2025

Copy link
Copy Markdown
Contributor

It was possible to shrink the number of storage slots occupied by SharedMutable by 1 field by packing ScheduledDelayChange and ScheduledValueChange.block_of_change together in a single storage slot. This makes the code uglier but overall I think this change is worth it.

benesjan commented Feb 7, 2025

Copy link
Copy Markdown
Contributor Author

This stack of pull requests is managed by Graphite. Learn more about stacking.

@benesjan benesjan force-pushed the 02-07-feat_optimizing_sharedmutable_sotrage_slots branch from 1278371 to ce4f5ab Compare February 10, 2025 10:02
@benesjan benesjan marked this pull request as ready for review February 10, 2025 10:21
@benesjan benesjan marked this pull request as draft February 10, 2025 10:40
}

impl<T, let INITIAL_DELAY: u32, let N: u32> SharedMutable<T, INITIAL_DELAY, UnconstrainedContext>
impl<T, let INITIAL_DELAY: u32, let M: u32> SharedMutable<T, INITIAL_DELAY, UnconstrainedContext>

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

T now represents the whole WithHash thing and not just svc so I change N to M to keep it consistent with the rest of this file.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't think so, in the declaration above you wrote

SharedMutable<T, INITIAL_DELAY, Context>
where
WithHash<SharedMutableValues<T, INITIAL_DELAY>, _>: Packable<M

so T is not packed into M, WithHash<SharedMutableValues<T is packed into M.

You also do SharedMutableValues<T> below. T is whatever we're storing.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

You are right. Good catch. Reverted the change in c6329b7


global TEST_INITIAL_DELAY: u32 = 13;

unconstrained fn assert_equal_after_conversion(original: ScheduledDelayChange<TEST_INITIAL_DELAY>) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

sdc now doesn't implement Packable so I had to delete these tests. They were moved with modifications to tests of SharedMutableValues.


pub fn get_value_change_storage_slot(shared_mutable_storage_slot: Field) -> Field {
shared_mutable_storage_slot + (SCHEDULED_DELAY_CHANGE_PCKD_LEN as Field)
pub(crate) fn unpack_delay_change(packed: Field) -> ScheduledDelayChange<INITIAL_DELAY> {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Using modulo operation in this func was not much of an option since the packed value does not fit in U128 and Field does not support it. For this reason I went with the "tmp value approach".

@benesjan benesjan changed the title feat: optimizing SharedMutable sotrage slots feat: optimizing SharedMutable storage slots Feb 10, 2025
@benesjan benesjan marked this pull request as ready for review February 10, 2025 10:55
@benesjan benesjan force-pushed the 02-07-feat_optimizing_sharedmutable_sotrage_slots branch 2 times, most recently from 7358134 to 2c0ff83 Compare February 10, 2025 11:32
@github-actions

github-actions Bot commented Feb 10, 2025

Copy link
Copy Markdown
Contributor

Changes to public function bytecode sizes

Generated at commit: 952a78de9a66bd6763efc940b9f5e25d9ca41681, compared to commit: 5d12f9446ad868a825874c0db947bf165153960a

🧾 Summary (100% most significant diffs)

Program Bytecode size in bytes (+/-) %
Auth::set_authorized +499 ❌ +16.78%
Auth::set_authorized_delay +481 ❌ +15.67%
Auth::get_scheduled_authorized +118 ❌ +13.64%
TokenBlacklist::update_roles +523 ❌ +11.20%
Auth::get_authorized +99 ❌ +10.22%
TokenBlacklist::constructor +503 ❌ +9.98%
Auth::public_dispatch +597 ❌ +7.76%
TokenBlacklist::get_roles +114 ❌ +4.81%
TokenBlacklist::mint_public +114 ❌ +3.57%
TokenBlacklist::mint_private +109 ❌ +3.21%
TokenBlacklist::public_dispatch +740 ❌ +3.12%
TokenBlacklist::burn_public +109 ❌ +2.27%
TokenBlacklist::transfer_public +114 ❌ +2.10%
TokenBlacklist::shield +109 ❌ +2.00%
Auth::get_authorized_delay -62 ✅ -9.10%

Full diff report 👇
Program Bytecode size in bytes (+/-) %
Auth::set_authorized 3,472 (+499) +16.78%
Auth::set_authorized_delay 3,550 (+481) +15.67%
Auth::get_scheduled_authorized 983 (+118) +13.64%
TokenBlacklist::update_roles 5,192 (+523) +11.20%
Auth::get_authorized 1,068 (+99) +10.22%
TokenBlacklist::constructor 5,544 (+503) +9.98%
Auth::public_dispatch 8,290 (+597) +7.76%
TokenBlacklist::get_roles 2,486 (+114) +4.81%
TokenBlacklist::mint_public 3,308 (+114) +3.57%
TokenBlacklist::mint_private 3,503 (+109) +3.21%
TokenBlacklist::public_dispatch 24,465 (+740) +3.12%
TokenBlacklist::burn_public 4,916 (+109) +2.27%
TokenBlacklist::transfer_public 5,544 (+114) +2.10%
TokenBlacklist::shield 5,554 (+109) +2.00%
Auth::get_authorized_delay 619 (-62) -9.10%

@benesjan benesjan requested a review from nventuro February 10, 2025 12:23
Comment thread noir-projects/aztec-nr/aztec/src/state_vars/shared_mutable.nr
}

impl<T, let INITIAL_DELAY: u32, let N: u32> SharedMutable<T, INITIAL_DELAY, UnconstrainedContext>
impl<T, let INITIAL_DELAY: u32, let M: u32> SharedMutable<T, INITIAL_DELAY, UnconstrainedContext>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't think so, in the declaration above you wrote

SharedMutable<T, INITIAL_DELAY, Context>
where
WithHash<SharedMutableValues<T, INITIAL_DELAY>, _>: Packable<M

so T is not packed into M, WithHash<SharedMutableValues<T is packed into M.

You also do SharedMutableValues<T> below. T is whatever we're storing.

Comment thread noir-projects/aztec-nr/aztec/src/state_vars/shared_mutable.nr
@benesjan benesjan force-pushed the 02-07-feat_optimizing_sharedmutable_sotrage_slots branch from c6329b7 to b1729c5 Compare February 17, 2025 16:16
@benesjan benesjan marked this pull request as draft February 17, 2025 16:19
@benesjan benesjan force-pushed the 02-07-feat_optimizing_sharedmutable_sotrage_slots branch from e383986 to 9b57e85 Compare February 17, 2025 17:44
@benesjan benesjan changed the base branch from master to graphite-base/11817 February 19, 2025 08:54
@benesjan benesjan force-pushed the 02-07-feat_optimizing_sharedmutable_sotrage_slots branch from 9b57e85 to f2bf543 Compare February 19, 2025 08:54
@benesjan benesjan changed the base branch from graphite-base/11817 to 02-19-fix_sharedmutable_compilation_warnings February 19, 2025 08:54
@benesjan benesjan marked this pull request as ready for review February 19, 2025 10:32
Base automatically changed from 02-19-fix_sharedmutable_compilation_warnings to master February 19, 2025 10:49
@benesjan benesjan marked this pull request as draft February 19, 2025 11:37
@benesjan benesjan force-pushed the 02-07-feat_optimizing_sharedmutable_sotrage_slots branch from 2b03a57 to 7dcbc29 Compare February 19, 2025 11:37
pub(crate) post: Option<u32>,
// Block at which `post` value is used instead of `pre`
block_of_change: u32,
pub(crate) block_of_change: u32,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Had to do this change because now we need direct access to it when packing in shared_mutable_values.nr. Not great.

@nventuro nventuro left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks a lot!

It's unfortunate that this got much uglier and we need to change TS files etc given that we use it in TS for upgrades. But at least that's not our problem now.

Comment thread noir-projects/aztec-nr/aztec/src/state_vars/shared_mutable.nr Outdated
@benesjan benesjan force-pushed the 02-07-feat_optimizing_sharedmutable_sotrage_slots branch from e91050f to 5155ac6 Compare February 19, 2025 13:31
@benesjan benesjan marked this pull request as ready for review February 19, 2025 13:31
@benesjan benesjan marked this pull request as draft February 19, 2025 13:32
@benesjan benesjan marked this pull request as ready for review February 19, 2025 13:33
import type { UInt32 } from '../shared.js';

export class ScheduledDelayChange {
constructor(public previous: UInt32 | undefined, public blockOfChange: UInt32, public post: UInt32 | undefined) {}

@benesjan benesjan Feb 19, 2025

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Order of args was different here from the Noir implementation because @sirasistant hates me 😝

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ugh, I was also caught by this at some point, I thought I changed it to the noir one, clearly I didn't ):

@benesjan benesjan enabled auto-merge (squash) February 19, 2025 13:49
benesjan and others added 17 commits February 20, 2025 08:00
@benesjan benesjan force-pushed the 02-07-feat_optimizing_sharedmutable_sotrage_slots branch from 0b08c43 to 3668ecf Compare February 20, 2025 08:29
@benesjan benesjan merged commit 5a93221 into master Feb 20, 2025
@benesjan benesjan deleted the 02-07-feat_optimizing_sharedmutable_sotrage_slots branch February 20, 2025 08:44
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