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

feat(avm): Track gas usage based on memory accesses #5518

Conversation

spalladino
Copy link
Collaborator

@spalladino spalladino commented Mar 29, 2024

Defines gas cost for each instruction based on its memory accesses: reads, indirect reads (priced as 2 reads), writes. Each instruction defines its expected number of memory operations, and gas cost is computed based on that and a base gas cost defined in the gas cost table. There is an additional check after the instruction is run that verifies that the number of memory operations matches the ones declared by the instruction. Note that, since we need to charge gas before execution (as specified in the yellow paper), we cannot just track accesses during execution and then charge that.

The new workflow for an instruction, which no instruction needs to fully override, is:

  • Invokes loadInputs to load from memory the inpiuts needed for execution (not needed if ops can compute their gas cost from their wires only)
  • Requests the expected number of memoryOperations to compute the memoryGasCost (defaults to zero)
  • Computes gasCost based on the loaded inputs, using the sum of baseGasCost and memoryGasCost (no need to override unless there's an additional gas component)
  • Consumes gas based on the computed gas cost, throwing an exceptional halt if not enough
  • Executes actual logic from internalExecute (all instructions override)
  • Asserts the expected memory operations against the actual memory operations (can be turned off for performance if we wanted to)

This needs a bunch more tests, but I can add them in a subsequent PR.

Copy link
Collaborator Author

spalladino commented Mar 29, 2024

Warning

This pull request is not mergeable via GitHub because a downstack PR is open. Once all requirements are satisfied, merge this PR as a stack on Graphite.
Learn more

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

Join @spalladino and the rest of your teammates on Graphite Graphite

@AztecBot
Copy link
Collaborator

Benchmark results

Metrics with a significant change:

  • note_successful_decrypting_time_in_ms (8): 243 (-18%)
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.

Values are compared against data from master at commit 9ffe4571 and shown if the difference exceeds 1%.

L2 block published to L1

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

Metric 8 txs 32 txs 64 txs
l1_rollup_calldata_size_in_bytes 676 676 676
l1_rollup_calldata_gas 6,424 6,424 6,424
l1_rollup_execution_gas 585,757 585,757 585,757
l2_block_processing_time_in_ms 1,307 (-2%) 4,673 (-2%) 9,114 (-3%)
note_successful_decrypting_time_in_ms ⚠️ 243 (-18%) 577 (-14%) 1,013 (-1%)
note_trial_decrypting_time_in_ms 46.1 (-54%) 97.7 (+12%) 38.0 (-21%)
l2_block_building_time_in_ms 12,487 (+2%) 45,243 (+1%) 91,196 (+3%)
l2_block_rollup_simulation_time_in_ms 7,570 (+1%) 26,390 (+1%) 52,318 (+2%)
l2_block_public_tx_process_time_in_ms 4,888 (+3%) 18,801 (+1%) 38,776 (+4%)

L2 chain processing

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

Metric 5 blocks 10 blocks
node_history_sync_time_in_ms 14,436 (-1%) 27,499
note_history_successful_decrypting_time_in_ms 1,310 (+1%) 2,457 (+3%)
note_history_trial_decrypting_time_in_ms 110 (+18%) 145 (+18%)
node_database_size_in_bytes 18,677,840 35,000,400
pxe_database_size_in_bytes 29,859 59,414

Circuits stats

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

Circuit circuit_simulation_time_in_ms circuit_input_size_in_bytes circuit_output_size_in_bytes
private-kernel-init 184 (+1%) 44,379 28,214
private-kernel-ordering 170 (-1%) 52,880 14,296
base-parity 3,477 (-1%) 128 311
root-parity 1,315 (+4%) 1,244 311
base-rollup 15,244 (+1%) 165,760 861
root-rollup 49.4 (+1%) 4,359 725
private-kernel-inner 228 73,795 28,214
public-kernel-app-logic 97.4 (+2%) 35,252 28,217
public-kernel-tail 140 (+1%) 40,928 28,217
merge-rollup 8.90 (+6%) 2,568 861
public-kernel-teardown 89.8 (-1%) 35,252 28,217
public-kernel-setup 88.9 (-1%) 35,252 28,217

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 512 leaves 1024 leaves 2048 leaves 4096 leaves 32 leaves
batch_insert_into_append_only_tree_16_depth_ms 10.0 16.0 (-1%) N/A N/A N/A N/A N/A N/A N/A
batch_insert_into_append_only_tree_16_depth_hash_count 16.8 31.6 N/A N/A N/A N/A N/A N/A N/A
batch_insert_into_append_only_tree_16_depth_hash_ms 0.584 0.494 (-1%) N/A N/A N/A N/A N/A N/A N/A
batch_insert_into_append_only_tree_32_depth_ms N/A N/A 48.3 (+5%) 72.8 236 (+1%) 448 (-1%) 881 (+1%) 1,728 N/A
batch_insert_into_append_only_tree_32_depth_hash_count N/A N/A 96.0 159 543 1,055 2,079 4,127 N/A
batch_insert_into_append_only_tree_32_depth_hash_ms N/A N/A 0.495 (+5%) 0.449 (+1%) 0.428 (+1%) 0.419 (-1%) 0.418 (+1%) 0.413 N/A
batch_insert_into_indexed_tree_20_depth_ms N/A N/A 54.9 (+1%) 107 (-1%) 342 (+1%) 666 1,325 (+1%) 2,613 N/A
batch_insert_into_indexed_tree_20_depth_hash_count N/A N/A 105 207 691 1,363 2,707 5,395 N/A
batch_insert_into_indexed_tree_20_depth_hash_ms N/A N/A 0.483 (+1%) 0.482 (-1%) 0.462 (+1%) 0.460 0.460 (+1%) 0.454 N/A
batch_insert_into_indexed_tree_40_depth_ms N/A N/A N/A N/A N/A N/A N/A N/A 61.7
batch_insert_into_indexed_tree_40_depth_hash_count N/A N/A N/A N/A N/A N/A N/A N/A 109
batch_insert_into_indexed_tree_40_depth_hash_ms N/A N/A N/A N/A N/A N/A N/A N/A 0.539

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 14,968 495,320

Transaction size based on fee payment method

Metric none fee payment method native fee payment method fpc_public fee payment method fpc_private fee payment method
tx_with_fee_size_in_bytes 773 905 1,161 1,377

Transaction processing duration by data writes.

Metric 0 new note hashes 1 new note hashes 2 new note hashes
tx_pxe_processing_time_ms 1,845 (+3%) 1,210 5,568 (+1%)
Metric 0 public data writes 1 public data writes 2 public data writes 3 public data writes 4 public data writes 5 public data writes 8 public data writes
tx_sequencer_processing_time_ms 10.1 (+3%) 588 (+1%) 482 (+12%) 1,136 567 1,971 557 (+1%)

@spalladino spalladino marked this pull request as draft April 3, 2024 20:39
spalladino added a commit that referenced this pull request Apr 4, 2024
Tracks gas usage for all AVM instructions based on memory consumption.
Adds an optional wrapper for TaggedMemory (enabled on test only) that
tracks all memory reads and writes to validate that the number of memory
operations charged match the actual ones.

Replaces existing #5514 and #5518 in favor of a more explicit approach,
at the expense of more duplicated code in each instruction, but
flattening the instruction hierarchy.

Closes #5518 
Closes #5514
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.

None yet

2 participants