From 31775158335a1bf07123fa54d52871dccec86610 Mon Sep 17 00:00:00 2001 From: Sam Blackshear Date: Sat, 30 Nov 2024 10:13:36 -0800 Subject: [PATCH] [move stdlib] minimize calls to length in vector functions Calling length in a loop is less gas and wall time efficient than calling it once and using a simple loop. --- .../tests/programmable/split_coins.exp | 2 +- .../sui-framework/docs/move-stdlib/vector.md | 6 ++-- .../packages/move-stdlib/sources/vector.move | 8 ++--- .../move-stdlib/tests/vector_tests.move | 10 ++++++ .../packages_compiled/move-stdlib | Bin 16954 -> 17028 bytes .../packages_compiled/sui-framework | Bin 67439 -> 67501 bytes crates/sui-protocol-config/src/lib.rs | 1 + ..._populated_genesis_snapshot_matches-2.snap | 29 +++++++++--------- 8 files changed, 32 insertions(+), 24 deletions(-) diff --git a/crates/sui-adapter-transactional-tests/tests/programmable/split_coins.exp b/crates/sui-adapter-transactional-tests/tests/programmable/split_coins.exp index a14e830ed4f04..d3ea43588eaf2 100644 --- a/crates/sui-adapter-transactional-tests/tests/programmable/split_coins.exp +++ b/crates/sui-adapter-transactional-tests/tests/programmable/split_coins.exp @@ -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: 5563200, storage_rebate: 0, non_refundable_storage_fee: 0 +gas summary: computation_cost: 1000000, storage_cost: 5791200, storage_rebate: 0, non_refundable_storage_fee: 0 task 2, lines 24-28: //# programmable --sender A --inputs 100000 @A diff --git a/crates/sui-framework/docs/move-stdlib/vector.md b/crates/sui-framework/docs/move-stdlib/vector.md index ec746effa9560..55060c3d8c628 100644 --- a/crates/sui-framework/docs/move-stdlib/vector.md +++ b/crates/sui-framework/docs/move-stdlib/vector.md @@ -312,10 +312,8 @@ Pushes all of the elements of the other vector into the lhsImplementation -
public fun append<Element>(lhs: &mut vector<Element>, mut other: vector<Element>) {
-    other.reverse();
-    while (other.length() != 0) lhs.push_back(other.pop_back());
-    other.destroy_empty();
+
public fun append<Element>(lhs: &mut vector<Element>, other: vector<Element>) {
+    other.do!(|e| lhs.push_back(e));
 }
 
diff --git a/crates/sui-framework/packages/move-stdlib/sources/vector.move b/crates/sui-framework/packages/move-stdlib/sources/vector.move index e9557b4d3f6ac..a24d0cd3db9e3 100644 --- a/crates/sui-framework/packages/move-stdlib/sources/vector.move +++ b/crates/sui-framework/packages/move-stdlib/sources/vector.move @@ -84,10 +84,8 @@ public fun reverse(v: &mut vector) { } /// Pushes all of the elements of the `other` vector into the `lhs` vector. -public fun append(lhs: &mut vector, mut other: vector) { - other.reverse(); - while (other.length() != 0) lhs.push_back(other.pop_back()); - other.destroy_empty(); +public fun append(lhs: &mut vector, other: vector) { + other.do!(|e| lhs.push_back(e)); } /// Return `true` if the vector `v` has no elements and `false` otherwise. @@ -185,7 +183,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.length() != 0) $f(v.pop_back()); + v.length().do!(|_| $f(v.pop_back())); v.destroy_empty(); } diff --git a/crates/sui-framework/packages/move-stdlib/tests/vector_tests.move b/crates/sui-framework/packages/move-stdlib/tests/vector_tests.move index eac18f10653a2..cc1d041f4a077 100644 --- a/crates/sui-framework/packages/move-stdlib/tests/vector_tests.move +++ b/crates/sui-framework/packages/move-stdlib/tests/vector_tests.move @@ -37,6 +37,16 @@ fun append_empties_is_empty() { assert!(v1.is_empty()); } +#[test] +fun append_singletons() { + let mut v1 = vector[0]; + let v2 = vector[1]; + v1.append(v2); + assert!(v1.length() == 2); + assert!(v1[0] == 0); + assert!(v1[1] == 1); +} + #[test] fun append_respects_order_empty_lhs() { let mut v1 = vector[]; diff --git a/crates/sui-framework/packages_compiled/move-stdlib b/crates/sui-framework/packages_compiled/move-stdlib index 084b953c118afac1208266a5048747221d92f9ca..8b532886dc69d6904179b6f511ab7f0d56109302 100644 GIT binary patch delta 289 zcmYk0F-k*05QcYVb|-l=vyYb;BS;YSS){W~BPggvDsNzEBU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