aztec-packages icon indicating copy to clipboard operation
aztec-packages copied to clipboard

[DO_NOT_MERGE] feat: optimizing SharedImmutable

Open benesjan opened this issue 1 year ago โ€ข 2 comments

This is a demonstration of how we can optimize state variables by packing them (storing serialized values along with hash, then fetching value in unconstrained, fetching hash in constrained and checking the hash).

Not mergeable because:

  1. Arithmetics over generics is highly experimental and not ready to be used in "prod" yet,
  2. we have to solve proper storage slot allocation (this breaks current impl in macros),
  3. we should try optimizing the length = 1 case in which the value should not get packed.

PrivateFPC::fund_transaction_privately(...) gates Before: 13987 After: 10209

diff: 3778

benesjan avatar Aug 15 '24 14:08 benesjan

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

benesjan avatar Aug 15 '24 14:08 benesjan

Benchmark results

Metrics with a significant change:

  • proof_construction_time_sha256_100_ms (16): 5,545 (-17%)
  • protocol_circuit_simulation_time_in_ms (private-kernel-tail-to-public): 825 (-19%)
Detailed results

All benchmarks are run on txs on the Benchmarking contract on the repository. Each tx consists of a batch call to create_note and increment_balance, which guarantees that each tx has a private call, a nested private call, a public call, and a nested public call, as well as an emitted private note, an unencrypted log, and public storage read and write.

This benchmark source data is available in JSON format on S3 here.

Proof generation

Each column represents the number of threads used in proof generation.

Metric 1 threads 4 threads 16 threads 32 threads 64 threads
proof_construction_time_sha256_ms 5,843 (+1%) 1,594 (+2%) 736 (+4%) 782 (+3%) 801 (+5%)
proof_construction_time_sha256_30_ms 11,877 3,193 1,428 (+1%) 1,459 (+1%) 1,479
proof_construction_time_sha256_100_ms 47,854 (+5%) 12,069 (+1%) :warning: 5,545 (-17%) 5,423 5,382
proof_construction_time_poseidon_hash_ms 79.0 35.0 (+3%) 34.0 59.0 88.0 (+1%)
proof_construction_time_poseidon_hash_30_ms 1,538 423 202 226 274 (-1%)
proof_construction_time_poseidon_hash_100_ms 5,697 (+1%) 1,535 (+1%) 696 (+3%) 746 (+1%) 756 (+1%)

L2 block published to L1

Each column represents the number of txs on an L2 block published to L1.

Metric 4 txs 8 txs 16 txs
l1_rollup_calldata_size_in_bytes 4,324 7,844 14,852
l1_rollup_calldata_gas 49,708 92,414 177,764
l1_rollup_execution_gas 1,383,157 2,130,831 3,958,220
l2_block_processing_time_in_ms 240 (-5%) 446 (+1%) 803 (-1%)
l2_block_building_time_in_ms 9,247 18,049 35,990
l2_block_rollup_simulation_time_in_ms 9,247 18,049 35,990
l2_block_public_tx_process_time_in_ms 7,798 16,502 34,421

L2 chain processing

Each column represents the number of blocks on the L2 chain where each block has 8 txs.

Metric 3 blocks 5 blocks
node_history_sync_time_in_ms 2,915 (+1%) 3,861 (-1%)
node_database_size_in_bytes 12,619,856 16,703,568
pxe_database_size_in_bytes 16,254 26,813

Circuits stats

Stats on running time and I/O sizes collected for every kernel circuit run across all benchmarks.

Circuit simulation_time_in_ms witness_generation_time_in_ms input_size_in_bytes output_size_in_bytes proving_time_in_ms
private-kernel-init 98.1 (+7%) 384 (-4%) 21,673 44,858 N/A
private-kernel-inner 164 687 (-4%) 72,309 45,005 N/A
private-kernel-reset-tiny 505 (+1%) 884 (-7%) 65,974 44,844 N/A
private-kernel-tail 208 (+1%) 163 (-2%) 50,728 52,256 N/A
base-parity 5.62 (+1%) N/A 160 96.0 N/A
root-parity 33.3 N/A 69,084 96.0 N/A
base-rollup 2,855 (+1%) N/A 187,817 664 N/A
root-rollup 38.6 (-1%) N/A 54,525 716 N/A
public-kernel-setup 101 (+3%) N/A 103,760 71,222 N/A
public-kernel-app-logic 104 (+1%) N/A 103,599 71,222 N/A
public-kernel-tail 583 (+1%) N/A 409,190 16,414 N/A
private-kernel-reset-small 491 N/A 66,085 45,629 N/A
private-kernel-tail-to-public :warning: 825 (-19%) 660 492,960 1,697 N/A
public-kernel-teardown 95.0 (+2%) N/A 104,005 71,222 N/A
merge-rollup 19.1 N/A 35,742 664 N/A
undefined N/A N/A N/A N/A 69,409 (+7%)

Stats on running time collected for app circuits

Function input_size_in_bytes output_size_in_bytes witness_generation_time_in_ms
ContractClassRegisterer:register 1,344 11,731 343
ContractInstanceDeployer:deploy 1,408 11,731 18.1 (-2%)
MultiCallEntrypoint:entrypoint 1,920 11,731 424
FeeJuice:deploy 1,376 11,731 386 (-1%)
SchnorrAccount:constructor 1,312 11,731 106 (+2%)
SchnorrAccount:entrypoint 2,304 11,731 434
Token:privately_mint_private_note 1,280 11,731 140 (-1%)
FPC:fee_entrypoint_public 1,344 11,731 28.6 (-2%)
Token:transfer 1,312 11,731 281 (-2%)
Benchmarking:create_note 1,344 11,731 105 (+1%)
SchnorrAccount:verify_private_authwit 1,280 11,731 27.8
Token:unshield 1,376 11,731 570 (+2%)
FPC:fee_entrypoint_private 1,376 11,731 760 (+1%)

AVM Simulation

Time to simulate various public functions in the AVM.

Function time_ms bytecode_size_in_bytes
FeeJuice:_increase_public_balance 53.9 (-1%) 8,139
FeeJuice:set_portal 12.8 (+9%) 2,362
Token:constructor 87.8 (+9%) 31,107
FPC:constructor 55.6 (+4%) 22,380
FeeJuice:mint_public 43.5 (+13%) 6,150
Token:mint_public 77.9 (-6%) 11,720
Token:assert_minter_and_mint 40.1 (+3%) 8,028
AuthRegistry:set_authorized 48.3 (+6%) 4,537
FPC:prepare_fee 245 (+2%) 8,812
Token:transfer_public 20.2 (-1%) 47,374
FPC:pay_refund 66.9 (+16%) 12,114
Benchmarking:increment_balance 966 7,450
Token:_increase_public_balance 39.6 (-2%) 8,960
FPC:pay_refund_with_shielded_rebate 67.5 (+2%) 12,663

Public DB Access

Time to access various public DBs.

Function time_ms
get-nullifier-index 0.165 (+4%)

Tree insertion stats

The duration to insert a fixed batch of leaves into each tree type.

Metric 1 leaves 16 leaves 64 leaves 128 leaves 256 leaves 512 leaves 1024 leaves
batch_insert_into_append_only_tree_16_depth_ms 2.17 3.91 (+2%) N/A N/A N/A N/A N/A
batch_insert_into_append_only_tree_16_depth_hash_count 16.8 31.7 N/A N/A N/A N/A N/A
batch_insert_into_append_only_tree_16_depth_hash_ms 0.112 (-1%) 0.111 (+2%) N/A N/A N/A N/A N/A
batch_insert_into_append_only_tree_32_depth_ms N/A N/A 11.5 17.8 (+1%) 30.9 (-3%) 60.6 (+3%) 112 (+1%)
batch_insert_into_append_only_tree_32_depth_hash_count N/A N/A 95.9 159 287 543 1,055
batch_insert_into_append_only_tree_32_depth_hash_ms N/A N/A 0.110 0.103 (+1%) 0.101 (-2%) 0.104 (+3%) 0.100 (+1%)
batch_insert_into_indexed_tree_20_depth_ms N/A N/A 14.4 26.2 (+3%) 43.4 84.7 (+3%) 159 (-2%)
batch_insert_into_indexed_tree_20_depth_hash_count N/A N/A 109 207 355 691 1,363
batch_insert_into_indexed_tree_20_depth_hash_ms N/A N/A 0.109 0.106 (+3%) 0.105 (+1%) 0.105 (+4%) 0.100 (-2%)
batch_insert_into_indexed_tree_40_depth_ms N/A N/A 16.5 (+1%) N/A N/A N/A N/A
batch_insert_into_indexed_tree_40_depth_hash_count N/A N/A 132 N/A N/A N/A N/A
batch_insert_into_indexed_tree_40_depth_hash_ms N/A N/A 0.105 (+1%) N/A N/A N/A N/A

Miscellaneous

Transaction sizes based on how many contract classes are registered in the tx.

Metric 0 registered classes 1 registered classes
tx_size_in_bytes 64,779 668,997

Transaction size based on fee payment method

| Metric | | | - | |

AztecBot avatar Aug 15 '24 15:08 AztecBot

Changes to public function bytecode sizes

Generated at commit: ba9b793d6829a4714b1168ebb57e3fdc0d1a7c9d, compared to commit: 2fd08babea610f97d63adcea5630cfd3065fd3a8

๐Ÿงพ Summary (100% most significant diffs)

Program Bytecode size in bytes (+/-) %
DocsExample::initialize_public_immutable +1,431 โŒ +619.48%
FeeJuice::set_portal +1,146 โŒ +370.87%
DocsExample::public_dispatch +1,289 โŒ +35.45%
AvmInitializerTest::constructor +459 โŒ +24.34%
AppSubscription::constructor +670 โŒ +21.85%
Crowdfunding::init +488 โŒ +17.62%
AvmInitializerTest::public_dispatch +464 โŒ +17.55%
AppSubscription::public_dispatch +670 โŒ +15.91%
AMM::constructor +419 โŒ +15.56%
FPC::constructor +343 โŒ +12.95%
Crowdfunding::public_dispatch +483 โŒ +11.84%
TokenBridge::constructor +305 โŒ +11.35%
Token::constructor +483 โŒ +11.07%
FeeJuice::public_dispatch +440 โŒ +9.64%
EasyPrivateVoting::constructor +239 โŒ +9.40%
Uniswap::constructor +234 โŒ +9.34%
Auth::constructor +234 โŒ +9.19%
NFT::constructor +249 โŒ +5.81%
EasyPrivateVoting::public_dispatch +239 โŒ +5.20%
FPC::public_dispatch +361 โŒ +3.59%
Claim::constructor +97 โŒ +3.48%
Claim::public_dispatch +92 โŒ +2.59%
Auth::public_dispatch +220 โŒ +2.38%
TokenBridge::public_dispatch +440 โŒ +2.09%
AMM::public_dispatch +421 โŒ +1.62%
Token::public_dispatch +443 โŒ +1.39%
NFT::public_dispatch +228 โŒ +0.90%
Uniswap::public_dispatch +220 โŒ +0.84%
Auth::set_authorized +5 โŒ +0.10%
Auth::set_authorized_delay +5 โŒ +0.10%
TokenBridge::exit_to_l1_public +5 โŒ +0.06%
TokenBridge::claim_public +5 โŒ +0.04%

Full diff report ๐Ÿ‘‡
Program Bytecode size in bytes (+/-) %
DocsExample::initialize_public_immutable 1,662ย (+1,431) +619.48%
FeeJuice::set_portal 1,455ย (+1,146) +370.87%
DocsExample::public_dispatch 4,925ย (+1,289) +35.45%
AvmInitializerTest::constructor 2,345ย (+459) +24.34%
AppSubscription::constructor 3,736ย (+670) +21.85%
Crowdfunding::init 3,257ย (+488) +17.62%
AvmInitializerTest::public_dispatch 3,108ย (+464) +17.55%
AppSubscription::public_dispatch 4,880ย (+670) +15.91%
AMM::constructor 3,111ย (+419) +15.56%
FPC::constructor 2,992ย (+343) +12.95%
Crowdfunding::public_dispatch 4,564ย (+483) +11.84%
TokenBridge::constructor 2,993ย (+305) +11.35%
Token::constructor 4,847ย (+483) +11.07%
FeeJuice::public_dispatch 5,002ย (+440) +9.64%
EasyPrivateVoting::constructor 2,782ย (+239) +9.40%
Uniswap::constructor 2,740ย (+234) +9.34%
Auth::constructor 2,781ย (+234) +9.19%
NFT::constructor 4,538ย (+249) +5.81%
EasyPrivateVoting::public_dispatch 4,831ย (+239) +5.20%
FPC::public_dispatch 10,409ย (+361) +3.59%
Claim::constructor 2,884ย (+97) +3.48%
Claim::public_dispatch 3,644ย (+92) +2.59%
Auth::public_dispatch 9,480ย (+220) +2.38%
TokenBridge::public_dispatch 21,534ย (+440) +2.09%
AMM::public_dispatch 26,466ย (+421) +1.62%
Token::public_dispatch 32,282ย (+443) +1.39%
NFT::public_dispatch 25,467ย (+228) +0.90%
Uniswap::public_dispatch 26,288ย (+220) +0.84%
Auth::set_authorized 4,860ย (+5) +0.10%
Auth::set_authorized_delay 4,958ย (+5) +0.10%
TokenBridge::exit_to_l1_public 7,991ย (+5) +0.06%
TokenBridge::claim_public 12,716ย (+5) +0.04%

github-actions[bot] avatar Feb 02 '25 02:02 github-actions[bot]

Yes, it'd be good for public immutable to explicitly mention on its docs that it is optimized for this (both source code and docsite).

@nventuro docsite does not exist but I updated the source code.

benesjan avatar Feb 04 '25 19:02 benesjan

Docs Preview

Hey there! ๐Ÿ‘‹ You can check your preview at https://67a38af84be440658ae0f862--aztec-docs-dev.netlify.app

AztecBot avatar Feb 05 '25 10:02 AztecBot