Skip to content

Commit

Permalink
feat: Change the layout of arrays and vectors to be a single pointer (#…
Browse files Browse the repository at this point in the history
…8448)

Previously, we used to store arrays as two stack elements (rc,
items_pointer) and vectors as three (rc, size, items_pointer). Now we
move away from that and we just represent them as one pointer to (rc,
...items) in the case of arrays and (rc, size, ...items) in the case of
vectors. This makes moving around arrays and vectors cheaper and avoids
needing to re-store arrays and vectors in references after modifying the
reference counters.

<details>
<summary> Changes in bytecode size </summary>

```
contracts: Transpiling  AppSubscription::constructor with size 1123 => 968
contracts: Transpiling  Auth::constructor with size 683 => 556
contracts: Transpiling  Auth::get_authorized with size 132 => 128
contracts: Transpiling  Auth::get_authorized_delay with size 181 => 175
contracts: Transpiling  Auth::get_scheduled_authorized with size 113 => 107
contracts: Transpiling  Auth::set_authorized with size 980 => 849
contracts: Transpiling  Auth::set_authorized_delay with size 938 => 818
contracts: Transpiling  AuthRegistry::_set_authorized with size 80 => 86
contracts: Transpiling  AuthRegistry::consume with size 634 => 514
contracts: Transpiling  AuthRegistry::is_consumable with size 127 => 123
contracts: Transpiling  AuthRegistry::is_reject_all with size 112 => 106
contracts: Transpiling  AuthRegistry::set_authorized with size 75 => 81
contracts: Transpiling  AuthRegistry::set_reject_all with size 60 => 64
contracts: Transpiling  AuthWitTest::consume_public with size 66 => 89
contracts: Transpiling  AvmInitializerTest::constructor with size 679 => 552
contracts: Transpiling  AvmInitializerTest::read_storage_immutable with size 97 => 89
contracts: Transpiling  AvmTest::add_args_return with size 17 => 17
contracts: Transpiling  AvmTest::add_storage_map with size 174 => 174
contracts: Transpiling  AvmTest::add_u128 with size 34 => 34
contracts: Transpiling  AvmTest::assert_nullifier_exists with size 18 => 18
contracts: Transpiling  AvmTest::assert_same with size 20 => 20
contracts: Transpiling  AvmTest::assert_timestamp with size 17 => 17
contracts: Transpiling  AvmTest::assertion_failure with size 16 => 16
contracts: Transpiling  AvmTest::check_selector with size 16 => 16
contracts: Transpiling  AvmTest::create_different_nullifier_in_nested_call with size 128 => 127
contracts: Transpiling  AvmTest::create_same_nullifier_in_nested_call with size 126 => 125
contracts: Transpiling  AvmTest::debug_logging with size 238 => 282
contracts: Transpiling  AvmTest::elliptic_curve_add_and_double with size 35 => 35
contracts: Transpiling  AvmTest::emit_nullifier_and_check with size 19 => 19
contracts: Transpiling  AvmTest::emit_unencrypted_log with size 501 => 524
contracts: Transpiling  AvmTest::get_address with size 15 => 15
contracts: Transpiling  AvmTest::get_args_hash with size 20 => 40
contracts: Transpiling  AvmTest::get_block_number with size 15 => 15
contracts: Transpiling  AvmTest::get_chain_id with size 15 => 15
contracts: Transpiling  AvmTest::get_da_gas_left with size 15 => 15
contracts: Transpiling  AvmTest::get_fee_per_da_gas with size 15 => 15
contracts: Transpiling  AvmTest::get_fee_per_l2_gas with size 15 => 15
contracts: Transpiling  AvmTest::get_function_selector with size 15 => 15
contracts: Transpiling  AvmTest::get_l2_gas_left with size 15 => 15
contracts: Transpiling  AvmTest::get_sender with size 15 => 15
contracts: Transpiling  AvmTest::get_storage_address with size 15 => 15
contracts: Transpiling  AvmTest::get_timestamp with size 15 => 15
contracts: Transpiling  AvmTest::get_transaction_fee with size 15 => 15
contracts: Transpiling  AvmTest::get_version with size 15 => 15
contracts: Transpiling  AvmTest::keccak_f1600 with size 63 => 77
contracts: Transpiling  AvmTest::keccak_hash with size 783 => 708
contracts: Transpiling  AvmTest::l1_to_l2_msg_exists with size 19 => 19
contracts: Transpiling  AvmTest::modulo2 with size 18 => 18
contracts: Transpiling  AvmTest::nested_call_to_add with size 204 => 202
contracts: Transpiling  AvmTest::nested_call_to_add_with_gas with size 204 => 204
contracts: Transpiling  AvmTest::nested_static_call_to_add with size 204 => 202
contracts: Transpiling  AvmTest::nested_static_call_to_set_storage with size 125 => 124
contracts: Transpiling  AvmTest::new_note_hash with size 13 => 13
contracts: Transpiling  AvmTest::new_nullifier with size 13 => 13
contracts: Transpiling  AvmTest::note_hash_exists with size 19 => 19
contracts: Transpiling  AvmTest::nullifier_collision with size 14 => 14
contracts: Transpiling  AvmTest::nullifier_exists with size 19 => 19
contracts: Transpiling  AvmTest::pedersen_commit with size 64 => 73
contracts: Transpiling  AvmTest::pedersen_hash with size 21 => 42
contracts: Transpiling  AvmTest::pedersen_hash_with_index with size 21 => 42
contracts: Transpiling  AvmTest::poseidon2_hash with size 376 => 306
contracts: Transpiling  AvmTest::read_storage_list with size 117 => 115
contracts: Transpiling  AvmTest::read_storage_map with size 109 => 103
contracts: Transpiling  AvmTest::read_storage_single with size 90 => 82
contracts: Transpiling  AvmTest::send_l2_to_l1_msg with size 14 => 14
contracts: Transpiling  AvmTest::set_opcode_big_field with size 21 => 21
contracts: Transpiling  AvmTest::set_opcode_small_field with size 15 => 15
contracts: Transpiling  AvmTest::set_opcode_u32 with size 15 => 15
contracts: Transpiling  AvmTest::set_opcode_u64 with size 15 => 15
contracts: Transpiling  AvmTest::set_opcode_u8 with size 15 => 15
contracts: Transpiling  AvmTest::set_read_storage_single with size 125 => 119
contracts: Transpiling  AvmTest::set_storage_list with size 45 => 47
contracts: Transpiling  AvmTest::set_storage_map with size 73 => 79
contracts: Transpiling  AvmTest::set_storage_single with size 41 => 43
contracts: Transpiling  AvmTest::sha256_hash with size 48 => 62
contracts: Transpiling  AvmTest::test_get_contract_instance with size 185 => 178
contracts: Transpiling  AvmTest::test_get_contract_instance_raw with size 60 => 69
contracts: Transpiling  AvmTest::to_radix_le with size 35 => 40
contracts: Transpiling  AvmTest::u128_addition_overflow with size 272 => 275
contracts: Transpiling  AvmTest::u128_from_integer_overflow with size 138 => 140
contracts: Transpiling  AvmTest::variable_base_msm with size 80 => 90
contracts: Transpiling  Benchmarking::broadcast with size 110 => 113
contracts: Transpiling  Benchmarking::increment_balance with size 264 => 261
contracts: Transpiling  CardGame::on_card_played with size 872 => 882
contracts: Transpiling  CardGame::on_cards_claimed with size 709 => 745
contracts: Transpiling  CardGame::on_game_joined with size 655 => 676
contracts: Transpiling  CardGame::start_game with size 1149 => 1150
contracts: Transpiling  Child::pub_get_value with size 24 => 24
contracts: Transpiling  Child::pub_inc_value with size 133 => 136
contracts: Transpiling  Child::pub_inc_value_internal with size 138 => 141
contracts: Transpiling  Child::pub_set_value with size 51 => 62
contracts: Transpiling  Child::set_value_twice_with_nested_first with size 168 => 178
contracts: Transpiling  Child::set_value_twice_with_nested_last with size 168 => 178
contracts: Transpiling  Child::set_value_with_two_nested_calls with size 100 => 135
contracts: Transpiling  Claim::constructor with size 790 => 656
contracts: Transpiling  Crowdfunding::_publish_donation_receipts with size 136 => 153
contracts: Transpiling  Crowdfunding::init with size 903 => 762
contracts: Transpiling  DelegatedOn::public_set_value with size 44 => 46
contracts: Transpiling  Delegator::public_delegate_set_value with size 97 => 94
contracts: Transpiling  DocsExample::get_shared_immutable_constrained_public with size 104 => 97
contracts: Transpiling  DocsExample::get_shared_immutable_constrained_public_indirect with size 66 => 92
contracts: Transpiling  DocsExample::get_shared_immutable_constrained_public_multiple with size 136 => 134
contracts: Transpiling  DocsExample::initialize_public_immutable with size 159 => 155
contracts: Transpiling  DocsExample::initialize_shared_immutable with size 159 => 155
contracts: Transpiling  DocsExample::spend_public_authwit with size 16 => 16
contracts: Transpiling  DocsExample::update_leader with size 52 => 54
contracts: Transpiling  EasyPrivateVoting::add_to_tally_public with size 225 => 212
contracts: Transpiling  EasyPrivateVoting::constructor with size 736 => 613
contracts: Transpiling  EasyPrivateVoting::end_vote with size 133 => 127
contracts: Transpiling  FeeJuice::_increase_public_balance with size 190 => 188
contracts: Transpiling  FeeJuice::balance_of_public with size 121 => 115
contracts: Transpiling  FeeJuice::check_balance with size 174 => 168
contracts: Transpiling  FeeJuice::claim_public with size 1553 => 1331
contracts: Transpiling  FeeJuice::set_portal with size 220 => 205
contracts: Transpiling  FPC::constructor with size 679 => 552
contracts: Transpiling  FPC::pay_refund with size 420 => 385
contracts: Transpiling  FPC::pay_refund_with_shielded_rebate with size 420 => 385
contracts: Transpiling  FPC::prepare_fee with size 340 => 297
contracts: Transpiling  ImportTest::pub_call_public_fn with size 125 => 124
contracts: Transpiling  InclusionProofs::constructor with size 595 => 477
contracts: Transpiling  InclusionProofs::push_nullifier_public with size 20 => 20
contracts: Transpiling  InclusionProofs::test_nullifier_inclusion_from_public with size 24 => 24
contracts: Transpiling  KeyRegistry::register_initial_keys with size 1405 => 1058
contracts: Transpiling  KeyRegistry::rotate_npk_m with size 1190 => 965
contracts: Transpiling  Lending::_borrow with size 2269 => 2274
contracts: Transpiling  Lending::_deposit with size 230 => 227
contracts: Transpiling  Lending::_repay with size 1351 => 1364
contracts: Transpiling  Lending::_withdraw with size 2322 => 2297
contracts: Transpiling  Lending::borrow_public with size 267 => 238
contracts: Transpiling  Lending::deposit_public with size 569 => 497
contracts: Transpiling  Lending::get_asset with size 159 => 158
contracts: Transpiling  Lending::get_assets with size 184 => 170
contracts: Transpiling  Lending::get_position with size 872 => 848
contracts: Transpiling  Lending::init with size 290 => 292
contracts: Transpiling  Lending::repay_public with size 501 => 443
contracts: Transpiling  Lending::update_accumulator with size 2414 => 2415
contracts: Transpiling  Lending::withdraw_public with size 267 => 238
contracts: Transpiling  Parent::pub_entry_point with size 58 => 81
contracts: Transpiling  Parent::pub_entry_point_twice with size 87 => 133
contracts: Transpiling  Parent::public_nested_static_call with size 1016 => 966
contracts: Transpiling  Parent::public_static_call with size 82 => 108
contracts: Transpiling  PriceFeed::get_price with size 117 => 112
contracts: Transpiling  PriceFeed::set_price with size 71 => 75
contracts: Transpiling  PrivateFPC::constructor with size 682 => 555
contracts: Transpiling  Router::_check_block_number with size 155 => 163
contracts: Transpiling  Router::_check_timestamp with size 158 => 166
contracts: Transpiling  StatefulTest::get_public_value with size 109 => 103
contracts: Transpiling  StatefulTest::increment_public_value with size 138 => 134
contracts: Transpiling  StatefulTest::increment_public_value_no_init_check with size 131 => 127
contracts: Transpiling  StatefulTest::public_constructor with size 748 => 613
contracts: Transpiling  StaticChild::pub_get_value with size 27 => 27
contracts: Transpiling  StaticChild::pub_illegal_inc_value with size 136 => 139
contracts: Transpiling  StaticChild::pub_inc_value with size 133 => 136
contracts: Transpiling  StaticChild::pub_set_value with size 51 => 62
contracts: Transpiling  StaticParent::public_call with size 58 => 81
contracts: Transpiling  StaticParent::public_get_value_from_child with size 136 => 148
contracts: Transpiling  StaticParent::public_nested_static_call with size 279 => 272
contracts: Transpiling  StaticParent::public_static_call with size 82 => 108
contracts: Transpiling  Test::assert_public_global_vars with size 42 => 42
contracts: Transpiling  Test::consume_message_from_arbitrary_sender_public with size 1105 => 915
contracts: Transpiling  Test::consume_mint_public_message with size 1330 => 1137
contracts: Transpiling  Test::create_l2_to_l1_message_arbitrary_recipient_public with size 14 => 14
contracts: Transpiling  Test::create_l2_to_l1_message_public with size 26 => 28
contracts: Transpiling  Test::dummy_public_call with size 16 => 16
contracts: Transpiling  Test::emit_nullifier_public with size 13 => 13
contracts: Transpiling  Test::emit_unencrypted with size 219 => 255
contracts: Transpiling  Test::is_time_equal with size 20 => 20
contracts: Transpiling  TestLog::emit_unencrypted_events with size 223 => 251
contracts: Transpiling  Token::_increase_public_balance with size 189 => 187
contracts: Transpiling  Token::_reduce_total_supply with size 168 => 162
contracts: Transpiling  Token::admin with size 100 => 92
contracts: Transpiling  Token::assert_minter_and_mint with size 243 => 228
contracts: Transpiling  Token::balance_of_public with size 128 => 122
contracts: Transpiling  Token::burn_public with size 729 => 628
contracts: Transpiling  Token::complete_refund with size 416 => 458
contracts: Transpiling  Token::constructor with size 1132 => 1027
contracts: Transpiling  Token::is_minter with size 119 => 113
contracts: Transpiling  Token::mint_private with size 508 => 519
contracts: Transpiling  Token::mint_public with size 370 => 350
contracts: Transpiling  Token::public_get_decimals with size 103 => 95
contracts: Transpiling  Token::public_get_name with size 100 => 92
contracts: Transpiling  Token::public_get_symbol with size 100 => 92
contracts: Transpiling  Token::set_admin with size 133 => 127
contracts: Transpiling  Token::set_minter with size 152 => 148
contracts: Transpiling  Token::shield with size 896 => 830
contracts: Transpiling  Token::total_supply with size 112 => 104
contracts: Transpiling  Token::transfer_public with size 748 => 651
contracts: Transpiling  TokenBlacklist::_increase_public_balance with size 189 => 187
contracts: Transpiling  TokenBlacklist::_reduce_total_supply with size 168 => 162
contracts: Transpiling  TokenBlacklist::balance_of_public with size 128 => 122
contracts: Transpiling  TokenBlacklist::burn_public with size 858 => 752
contracts: Transpiling  TokenBlacklist::constructor with size 1548 => 1307
contracts: Transpiling  TokenBlacklist::get_roles with size 181 => 179
contracts: Transpiling  TokenBlacklist::mint_private with size 563 => 578
contracts: Transpiling  TokenBlacklist::mint_public with size 550 => 529
contracts: Transpiling  TokenBlacklist::shield with size 1025 => 954
contracts: Transpiling  TokenBlacklist::total_supply with size 112 => 104
contracts: Transpiling  TokenBlacklist::transfer_public with size 1009 => 902
contracts: Transpiling  TokenBlacklist::update_roles with size 1197 => 1074
contracts: Transpiling  TokenBridge::_assert_token_is_same with size 103 => 95
contracts: Transpiling  TokenBridge::_call_mint_on_token with size 277 => 254
contracts: Transpiling  TokenBridge::claim_public with size 1644 => 1401
contracts: Transpiling  TokenBridge::constructor with size 707 => 582
contracts: Transpiling  TokenBridge::exit_to_l1_public with size 853 => 813
contracts: Transpiling  TokenBridge::get_portal_address_public with size 108 => 100
contracts: Transpiling  TokenBridge::get_token with size 100 => 92
contracts: Transpiling  Uniswap::_approve_bridge_and_exit_input_asset_to_L1 with size 3337 => 2622
contracts: Transpiling  Uniswap::_assert_token_is_same with size 68 => 93
contracts: Transpiling  Uniswap::constructor with size 679 => 552
contracts: Transpiling  Uniswap::swap_public with size 2114 => 2047

```

 </details>
  • Loading branch information
sirasistant authored Sep 11, 2024
1 parent 3651936 commit 3ad624c
Show file tree
Hide file tree
Showing 19 changed files with 1,018 additions and 990 deletions.
43 changes: 20 additions & 23 deletions noir/noir-repo/acvm-repo/brillig_vm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,17 +449,16 @@ impl<'a, F: AcirField, B: BlackBoxFunctionSolver<F>> VM<'a, F, B> {
}
HeapValueType::Array { value_types, size } => {
let array_address = self.memory.read_ref(value_address);
let array_start = self.memory.read_ref(array_address);
self.read_slice_of_values_from_memory(array_start, *size, value_types)
let items_start = MemoryAddress(array_address.to_usize() + 1);
self.read_slice_of_values_from_memory(items_start, *size, value_types)
}
HeapValueType::Vector { value_types } => {
let vector_address = self.memory.read_ref(value_address);
let vector_start = self.memory.read_ref(vector_address);
let size_address: MemoryAddress =
(vector_address.to_usize() + 1).into();
let size_address = MemoryAddress(vector_address.to_usize() + 1);
let items_start = MemoryAddress(vector_address.to_usize() + 2);
let vector_size = self.memory.read(size_address).to_usize();
self.read_slice_of_values_from_memory(
vector_start,
items_start,
vector_size,
value_types,
)
Expand Down Expand Up @@ -646,8 +645,8 @@ impl<'a, F: AcirField, B: BlackBoxFunctionSolver<F>> VM<'a, F, B> {
current_pointer = MemoryAddress(current_pointer.to_usize() + 1);
}
HeapValueType::Array { .. } => {
let destination = self.memory.read_ref(current_pointer);
let destination = self.memory.read_ref(destination);
let destination =
MemoryAddress(self.memory.read_ref(current_pointer).0 + 1);
self.write_slice_of_values_to_memory(
destination,
values,
Expand Down Expand Up @@ -2005,33 +2004,31 @@ mod tests {
vec![MemoryValue::new_field(FieldElement::from(9u128))];

// construct memory by declaring all inner arrays/vectors first
// Declare v2
let v2_ptr: usize = 0usize;
let mut memory = v2.clone();
let v2_start = memory.len();
memory.extend(vec![MemoryValue::from(v2_ptr), v2.len().into(), MemoryValue::from(1_u32)]);
let mut memory = vec![MemoryValue::from(1_u32), v2.len().into()];
memory.extend(v2.clone());
let a4_ptr = memory.len();
memory.extend(vec![MemoryValue::from(1_u32)]);
memory.extend(a4.clone());
let a4_start = memory.len();
memory.extend(vec![MemoryValue::from(a4_ptr), MemoryValue::from(1_u32)]);
let v6_ptr = memory.len();
memory.extend(vec![MemoryValue::from(1_u32), v6.len().into()]);
memory.extend(v6.clone());
let v6_start = memory.len();
memory.extend(vec![MemoryValue::from(v6_ptr), v6.len().into(), MemoryValue::from(1_u32)]);
let a9_ptr = memory.len();
memory.extend(vec![MemoryValue::from(1_u32)]);
memory.extend(a9.clone());
let a9_start = memory.len();
memory.extend(vec![MemoryValue::from(a9_ptr), MemoryValue::from(1_u32)]);
// finally we add the contents of the outer array
let outer_ptr = memory.len();
memory.extend(vec![MemoryValue::from(1_u32)]);
let outer_start = memory.len();
let outer_array = vec![
MemoryValue::new_field(FieldElement::from(1u128)),
MemoryValue::from(v2.len() as u32),
MemoryValue::from(v2_start),
MemoryValue::from(a4_start),
MemoryValue::from(v2_ptr),
MemoryValue::from(a4_ptr),
MemoryValue::new_field(FieldElement::from(5u128)),
MemoryValue::from(v6.len() as u32),
MemoryValue::from(v6_start),
MemoryValue::from(a9_start),
MemoryValue::from(v6_ptr),
MemoryValue::from(a9_ptr),
];
memory.extend(outer_array.clone());

Expand Down Expand Up @@ -2075,7 +2072,7 @@ mod tests {
// input = 0
Opcode::Const {
destination: r_input,
value: (outer_ptr).into(),
value: (outer_start).into(),
bit_size: BitSize::Integer(IntegerBitSize::U32),
},
// some_function(input)
Expand Down
Loading

0 comments on commit 3ad624c

Please sign in to comment.