Skip to content

Commit

Permalink
[move] Update some calls to is_empty in loops (#19918)
Browse files Browse the repository at this point in the history
## Description 

Optimizes some calls to `is_empty` in loop conditions. This saves on the
function call overhead and uses the bytecode instruction directly.

## Test plan 

Make sure existing tests pass.

---

## Release notes

Check each box that your changes affect. If none of the boxes relate to
your changes, release notes aren't required.

For each box you select, include information after the relevant heading
that describes the impact of your changes that a user might notice and
any actions they must take to implement updates.

- [X] Protocol: New protocol version added due to internal changes to
the Sui framework.
- [ ] Nodes (Validators and Full nodes): 
- [ ] Indexer: 
- [ ] JSON-RPC: 
- [ ] GraphQL: 
- [ ] CLI: 
- [ ] Rust SDK:
- [ ] REST API:
  • Loading branch information
tzakian authored Oct 29, 2024
1 parent 7378f3e commit 68ac44a
Show file tree
Hide file tree
Showing 14 changed files with 30 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ task 1, lines 9-22:
//# publish
created: object(1,0)
mutated: object(0,2)
gas summary: computation_cost: 1000000, storage_cost: 5677200, storage_rebate: 0, non_refundable_storage_fee: 0
gas summary: computation_cost: 1000000, storage_cost: 5563200, storage_rebate: 0, non_refundable_storage_fee: 0

task 2, lines 24-28:
//# programmable --sender A --inputs 100000 @A
Expand Down
4 changes: 2 additions & 2 deletions crates/sui-framework/docs/move-stdlib/vector.md
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ Pushes all of the elements of the <code>other</code> vector into the <code>lhs</

<pre><code><b>public</b> <b>fun</b> <a href="../move-stdlib/vector.md#0x1_vector_append">append</a>&lt;Element&gt;(lhs: &<b>mut</b> <a href="../move-stdlib/vector.md#0x1_vector">vector</a>&lt;Element&gt;, <b>mut</b> other: <a href="../move-stdlib/vector.md#0x1_vector">vector</a>&lt;Element&gt;) {
other.<a href="../move-stdlib/vector.md#0x1_vector_reverse">reverse</a>();
<b>while</b> (!other.<a href="../move-stdlib/vector.md#0x1_vector_is_empty">is_empty</a>()) lhs.<a href="../move-stdlib/vector.md#0x1_vector_push_back">push_back</a>(other.<a href="../move-stdlib/vector.md#0x1_vector_pop_back">pop_back</a>());
<b>while</b> (other.<a href="../move-stdlib/vector.md#0x1_vector_length">length</a>() != 0) lhs.<a href="../move-stdlib/vector.md#0x1_vector_push_back">push_back</a>(other.<a href="../move-stdlib/vector.md#0x1_vector_pop_back">pop_back</a>());
other.<a href="../move-stdlib/vector.md#0x1_vector_destroy_empty">destroy_empty</a>();
}
</code></pre>
Expand Down Expand Up @@ -504,7 +504,7 @@ Aborts if <code>i</code> is out of bounds.


<pre><code><b>public</b> <b>fun</b> <a href="../move-stdlib/vector.md#0x1_vector_swap_remove">swap_remove</a>&lt;Element&gt;(v: &<b>mut</b> <a href="../move-stdlib/vector.md#0x1_vector">vector</a>&lt;Element&gt;, i: <a href="../move-stdlib/u64.md#0x1_u64">u64</a>): Element {
<b>assert</b>!(!v.<a href="../move-stdlib/vector.md#0x1_vector_is_empty">is_empty</a>(), <a href="../move-stdlib/vector.md#0x1_vector_EINDEX_OUT_OF_BOUNDS">EINDEX_OUT_OF_BOUNDS</a>);
<b>assert</b>!(v.<a href="../move-stdlib/vector.md#0x1_vector_length">length</a>() != 0, <a href="../move-stdlib/vector.md#0x1_vector_EINDEX_OUT_OF_BOUNDS">EINDEX_OUT_OF_BOUNDS</a>);
<b>let</b> last_idx = v.<a href="../move-stdlib/vector.md#0x1_vector_length">length</a>() - 1;
v.<a href="../move-stdlib/vector.md#0x1_vector_swap">swap</a>(i, last_idx);
v.<a href="../move-stdlib/vector.md#0x1_vector_pop_back">pop_back</a>()
Expand Down
4 changes: 2 additions & 2 deletions crates/sui-framework/docs/sui-framework/vec_map.md
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ Pop the most recently inserted entry from the map. Aborts if the map is empty.


<pre><code><b>public</b> <b>fun</b> <a href="../sui-framework/vec_map.md#0x2_vec_map_pop">pop</a>&lt;K: <b>copy</b>, V&gt;(self: &<b>mut</b> <a href="../sui-framework/vec_map.md#0x2_vec_map_VecMap">VecMap</a>&lt;K, V&gt;): (K, V) {
<b>assert</b>!(!self.contents.<a href="../sui-framework/vec_map.md#0x2_vec_map_is_empty">is_empty</a>(), <a href="../sui-framework/vec_map.md#0x2_vec_map_EMapEmpty">EMapEmpty</a>);
<b>assert</b>!(self.contents.length() != 0, <a href="../sui-framework/vec_map.md#0x2_vec_map_EMapEmpty">EMapEmpty</a>);
<b>let</b> <a href="../sui-framework/vec_map.md#0x2_vec_map_Entry">Entry</a> { key, value } = self.contents.pop_back();
(key, value)
}
Expand Down Expand Up @@ -526,7 +526,7 @@ and are *not* sorted.
keys.reverse();
values.reverse();
<b>let</b> <b>mut</b> map = <a href="../sui-framework/vec_map.md#0x2_vec_map_empty">empty</a>();
<b>while</b> (!keys.<a href="../sui-framework/vec_map.md#0x2_vec_map_is_empty">is_empty</a>()) map.<a href="../sui-framework/vec_map.md#0x2_vec_map_insert">insert</a>(keys.pop_back(), values.pop_back());
<b>while</b> (keys.length() != 0) map.<a href="../sui-framework/vec_map.md#0x2_vec_map_insert">insert</a>(keys.pop_back(), values.pop_back());
keys.<a href="../sui-framework/vec_map.md#0x2_vec_map_destroy_empty">destroy_empty</a>();
values.<a href="../sui-framework/vec_map.md#0x2_vec_map_destroy_empty">destroy_empty</a>();
map
Expand Down
2 changes: 1 addition & 1 deletion crates/sui-framework/docs/sui-framework/vec_set.md
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ and are *not* sorted.
<pre><code><b>public</b> <b>fun</b> <a href="../sui-framework/vec_set.md#0x2_vec_set_from_keys">from_keys</a>&lt;K: <b>copy</b> + drop&gt;(<b>mut</b> keys: <a href="../move-stdlib/vector.md#0x1_vector">vector</a>&lt;K&gt;): <a href="../sui-framework/vec_set.md#0x2_vec_set_VecSet">VecSet</a>&lt;K&gt; {
keys.reverse();
<b>let</b> <b>mut</b> set = <a href="../sui-framework/vec_set.md#0x2_vec_set_empty">empty</a>();
<b>while</b> (!keys.<a href="../sui-framework/vec_set.md#0x2_vec_set_is_empty">is_empty</a>()) set.<a href="../sui-framework/vec_set.md#0x2_vec_set_insert">insert</a>(keys.pop_back());
<b>while</b> (keys.length() != 0) set.<a href="../sui-framework/vec_set.md#0x2_vec_set_insert">insert</a>(keys.pop_back());
set
}
</code></pre>
Expand Down
2 changes: 1 addition & 1 deletion crates/sui-framework/docs/sui-system/voting_power.md
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ Update validators with the decided voting power.


<pre><code><b>fun</b> <a href="voting_power.md#0x3_voting_power_update_voting_power">update_voting_power</a>(validators: &<b>mut</b> <a href="../move-stdlib/vector.md#0x1_vector">vector</a>&lt;Validator&gt;, <b>mut</b> info_list: <a href="../move-stdlib/vector.md#0x1_vector">vector</a>&lt;<a href="voting_power.md#0x3_voting_power_VotingPowerInfoV2">VotingPowerInfoV2</a>&gt;) {
<b>while</b> (!info_list.is_empty()) {
<b>while</b> (info_list.length() != 0) {
<b>let</b> <a href="voting_power.md#0x3_voting_power_VotingPowerInfoV2">VotingPowerInfoV2</a> {
validator_index,
<a href="voting_power.md#0x3_voting_power">voting_power</a>,
Expand Down
8 changes: 4 additions & 4 deletions crates/sui-framework/packages/move-stdlib/sources/vector.move
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public fun reverse<Element>(v: &mut vector<Element>) {
/// Pushes all of the elements of the `other` vector into the `lhs` vector.
public fun append<Element>(lhs: &mut vector<Element>, mut other: vector<Element>) {
other.reverse();
while (!other.is_empty()) lhs.push_back(other.pop_back());
while (other.length() != 0) lhs.push_back(other.pop_back());
other.destroy_empty();
}

Expand Down Expand Up @@ -156,7 +156,7 @@ public fun insert<Element>(v: &mut vector<Element>, e: Element, mut i: u64) {
/// This is O(1), but does not preserve ordering of elements in the vector.
/// Aborts if `i` is out of bounds.
public fun swap_remove<Element>(v: &mut vector<Element>, i: u64): Element {
assert!(!v.is_empty(), EINDEX_OUT_OF_BOUNDS);
assert!(v.length() != 0, EINDEX_OUT_OF_BOUNDS);
let last_idx = v.length() - 1;
v.swap(i, last_idx);
v.pop_back()
Expand All @@ -176,7 +176,7 @@ public macro fun tabulate<$T>($n: u64, $f: |u64| -> $T): vector<$T> {
/// Does not preserve the order of elements in the vector (starts from the end of the vector).
public macro fun destroy<$T>($v: vector<$T>, $f: |$T|) {
let mut v = $v;
while (!v.is_empty()) $f(v.pop_back());
while (v.length() != 0) $f(v.pop_back());
v.destroy_empty();
}

Expand All @@ -185,7 +185,7 @@ public macro fun destroy<$T>($v: vector<$T>, $f: |$T|) {
public macro fun do<$T>($v: vector<$T>, $f: |$T|) {
let mut v = $v;
v.reverse();
while (!v.is_empty()) $f(v.pop_back());
while (v.length() != 0) $f(v.pop_back());
v.destroy_empty();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public fun remove<K: copy, V>(self: &mut VecMap<K, V>, key: &K): (K, V) {

/// Pop the most recently inserted entry from the map. Aborts if the map is empty.
public fun pop<K: copy, V>(self: &mut VecMap<K, V>): (K, V) {
assert!(!self.contents.is_empty(), EMapEmpty);
assert!(self.contents.length() != 0, EMapEmpty);
let Entry { key, value } = self.contents.pop_back();
(key, value)
}
Expand Down Expand Up @@ -144,7 +144,7 @@ public fun from_keys_values<K: copy, V>(mut keys: vector<K>, mut values: vector<
keys.reverse();
values.reverse();
let mut map = empty();
while (!keys.is_empty()) map.insert(keys.pop_back(), values.pop_back());
while (keys.length() != 0) map.insert(keys.pop_back(), values.pop_back());
keys.destroy_empty();
values.destroy_empty();
map
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public fun into_keys<K: copy + drop>(self: VecSet<K>): vector<K> {
public fun from_keys<K: copy + drop>(mut keys: vector<K>): VecSet<K> {
keys.reverse();
let mut set = empty();
while (!keys.is_empty()) set.insert(keys.pop_back());
while (keys.length() != 0) set.insert(keys.pop_back());
set
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ module sui_system::voting_power {

/// Update validators with the decided voting power.
fun update_voting_power(validators: &mut vector<Validator>, mut info_list: vector<VotingPowerInfoV2>) {
while (!info_list.is_empty()) {
while (info_list.length() != 0) {
let VotingPowerInfoV2 {
validator_index,
voting_power,
Expand Down
Binary file modified crates/sui-framework/packages_compiled/move-stdlib
Binary file not shown.
Binary file modified crates/sui-framework/packages_compiled/sui-framework
Binary file not shown.
Binary file modified crates/sui-framework/packages_compiled/sui-system
Binary file not shown.
1 change: 1 addition & 0 deletions crates/sui-protocol-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ const MAX_PROTOCOL_VERSION: u64 = 68;
// Framework fix for fungible staking book-keeping.
// Version 67: Re-enable distributed vote scoring in mainnet.
// Version 68: Add G1Uncompressed group to group ops.
// Update to Move stdlib.

#[derive(Copy, Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
pub struct ProtocolVersion(u64);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,56 +240,56 @@ validators:
next_epoch_worker_address: ~
extra_fields:
id:
id: "0x03a63a15f9e8e024f98f47d797d2fa22baf2aeafee5aec4d66f09d9689c1a2c3"
id: "0x2f1c192f30b36b0d0a47520ff814ace58ce8a73580c9bf86c0fc729781351bcc"
size: 0
voting_power: 10000
operation_cap_id: "0x009c04b7e355d845db8c5c4e2c79f94e1579ae28531a59a9411d1c71923ad619"
operation_cap_id: "0x0e83ac0a1c9938e12a692c734f8f38dfe5858076b17611402d46afcd5887ba8e"
gas_price: 1000
staking_pool:
id: "0x758d1e73ea89a62fda28afdd7a0b327f8c37c849609d1badd6189557cc45a323"
id: "0x222477b804c11404854c3c14cf29a2840472651c91d8870e07ae852a98c0a2e3"
activation_epoch: 0
deactivation_epoch: ~
sui_balance: 20000000000000000
rewards_pool:
value: 0
pool_token_balance: 20000000000000000
exchange_rates:
id: "0x36ff3b4f616e5a027a671fc510c1db7baee6ec9159857d20b0ec458b38471d7a"
id: "0xf532945be4e9eb7ef597867c6dee34dc1d89f55f711d084bc6aa01c7c99ea179"
size: 1
pending_stake: 0
pending_total_sui_withdraw: 0
pending_pool_token_withdraw: 0
extra_fields:
id:
id: "0x6a92a767673917836d5ca96ec5035750f734a123e2dc00d9fa6823872c48af84"
id: "0x4b5abcdcefc7404834889f2890b2b626ab85c15a20b19130b56cbee9bbe2b0af"
size: 0
commission_rate: 200
next_epoch_stake: 20000000000000000
next_epoch_gas_price: 1000
next_epoch_commission_rate: 200
extra_fields:
id:
id: "0xb2cb5fe76aa57d4abbb22ba2e49015b3e70e4a13f9f1ec84233358c5a6b580c9"
id: "0xeb9ab0c31391cb533e672f2aa1a919f474a988620c5eac625dab9e28e15a7661"
size: 0
pending_active_validators:
contents:
id: "0x2ef68115e3f22e2280feb00c57956531e48f518da60dbaa3fa4edefae45b19c8"
id: "0x1e0beb565adb7f908bce1bb65d14b5da4c6e4e0ff281e91e4c79fd7a20947d35"
size: 0
pending_removals: []
staking_pool_mappings:
id: "0xaf4b76ff1f408e0bd825ef4942ffd31a62e928d8592edec331ff3a3221af2a14"
id: "0xabce5d04c1673e4e317e5c0f78bc346c4960d25473e095c9fb668ac32f5e216d"
size: 1
inactive_validators:
id: "0xd59da3a9b3aeb6b47f700dade9cc6c2a9c5054db52bf7efdff2d6b8864c02e75"
id: "0x9069998be467d392b0a8ce1f598a353c415729af75bb2ebafbe66d26114ad52f"
size: 0
validator_candidates:
id: "0xc14f302bf04a1debb67ed60b8a69fc743fc2d0112ee08a798b0329c97f2187f0"
id: "0x68667de51bea6086d3fd60059841df6da32a6fd475ad52ad10597797ec6a3ca9"
size: 0
at_risk_validators:
contents: []
extra_fields:
id:
id: "0x4933a6d414b1d9e9b4c7234899ebab473fa50ceea51b698779a6e043035647ef"
id: "0xfc98b9ca99540332ff24894fd810f83a85e726542c2119bc1325d350b0399434"
size: 0
storage_fund:
total_object_storage_rebates:
Expand All @@ -306,7 +306,7 @@ parameters:
validator_low_stake_grace_period: 7
extra_fields:
id:
id: "0x2e2f5f04aa912fa92c78411e5255460698199aa068b61148084b44f84616b5ef"
id: "0x16212fe3db87d96453a048041166f3f491c06f00c45a4efe181bf7708c292d3f"
size: 0
reference_gas_price: 1000
validator_report_records:
Expand All @@ -320,7 +320,7 @@ stake_subsidy:
stake_subsidy_decrease_rate: 1000
extra_fields:
id:
id: "0x23b2a35c924ac30a0ea2e8b7fa4643e0078ae345778a7083cda02b79428043fc"
id: "0x3110ada5ccc4394928c0116629587c1ad110099430f19ea183d799689eb5a8df"
size: 0
safe_mode: false
safe_mode_storage_rewards:
Expand All @@ -332,6 +332,6 @@ safe_mode_non_refundable_storage_fee: 0
epoch_start_timestamp_ms: 10
extra_fields:
id:
id: "0x5b1db70db5006a12cc40708a295a77502167e893c18326d6628cf947ed364c56"
id: "0x34587a89960874da16d01bb778a02f7603278b0da8ec9258668982948f9b9535"
size: 0

0 comments on commit 68ac44a

Please sign in to comment.