$npx -y skills add aptos-labs/aptos-agent-skills --skill analyze-gas-optimizationAnalyze and optimize Aptos Move contracts for gas efficiency, identifying expensive operations and suggesting optimizations. Triggers on: 'optimize gas', 'reduce gas costs', 'gas analysis', 'make contract cheaper', 'gas efficiency', 'analyze gas usage', 'reduce transaction costs'
| 1 | # Skill: analyze-gas-optimization |
| 2 | |
| 3 | Analyze and optimize Aptos Move contracts for gas efficiency, identifying expensive operations and suggesting |
| 4 | optimizations. |
| 5 | |
| 6 | ## When to Use This Skill |
| 7 | |
| 8 | **Trigger phrases:** |
| 9 | |
| 10 | - "optimize gas", "reduce gas costs", "gas analysis" |
| 11 | - "make contract cheaper", "gas efficiency" |
| 12 | - "analyze gas usage", "gas optimization" |
| 13 | - "reduce transaction costs" |
| 14 | |
| 15 | **Use cases:** |
| 16 | |
| 17 | - Before mainnet deployment |
| 18 | - When transaction costs are high |
| 19 | - When optimizing for high-frequency operations |
| 20 | - When building DeFi protocols with many transactions |
| 21 | |
| 22 | ## Core Gas Optimization Principles |
| 23 | |
| 24 | ### 1. Storage Optimization |
| 25 | |
| 26 | - Minimize stored data size |
| 27 | - Use efficient data structures |
| 28 | - Pack struct fields efficiently |
| 29 | - Remove unnecessary fields |
| 30 | |
| 31 | ### 2. Computation Optimization |
| 32 | |
| 33 | - Avoid loops over large collections |
| 34 | - Cache repeated calculations |
| 35 | - Use bitwise operations when possible |
| 36 | - Minimize vector operations |
| 37 | |
| 38 | ### 3. Reference Optimization |
| 39 | |
| 40 | - Prefer borrowing over moving when possible |
| 41 | - Use `&` and `&mut` efficiently |
| 42 | - Avoid unnecessary copies |
| 43 | |
| 44 | ## Gas Cost Analysis |
| 45 | |
| 46 | ### Expensive Operations |
| 47 | |
| 48 | #### 1. Global Storage Operations |
| 49 | |
| 50 | ```move |
| 51 | // EXPENSIVE: Writing to global storage |
| 52 | move_to(account, large_struct); |
| 53 | |
| 54 | // EXPENSIVE: Reading and writing |
| 55 | let data = borrow_global_mut<LargeData>(addr); |
| 56 | |
| 57 | // EXPENSIVE: Checking existence |
| 58 | if (exists<Resource>(addr)) { ... } |
| 59 | ``` |
| 60 | |
| 61 | #### 2. Vector Operations |
| 62 | |
| 63 | ```move |
| 64 | // EXPENSIVE: Growing vectors dynamically |
| 65 | vector::push_back(&mut vec, item); // O(n) worst case |
| 66 | |
| 67 | // EXPENSIVE: Searching vectors |
| 68 | vector::contains(&vec, &item); // O(n) |
| 69 | |
| 70 | // EXPENSIVE: Removing from middle |
| 71 | vector::remove(&mut vec, index); // O(n) |
| 72 | ``` |
| 73 | |
| 74 | #### 3. String Operations |
| 75 | |
| 76 | ```move |
| 77 | // EXPENSIVE: String concatenation |
| 78 | string::append(&mut s1, s2); |
| 79 | |
| 80 | // EXPENSIVE: UTF8 validation |
| 81 | string::utf8(bytes); |
| 82 | ``` |
| 83 | |
| 84 | ### Optimization Patterns |
| 85 | |
| 86 | #### 1. Batch Operations |
| 87 | |
| 88 | ```move |
| 89 | // BAD: Multiple storage accesses |
| 90 | public fun update_values(account: &signer, updates: vector<Update>) { |
| 91 | let i = 0; |
| 92 | while (i < vector::length(&updates)) { |
| 93 | let update = vector::borrow(&updates, i); |
| 94 | let data = borrow_global_mut<Data>(update.address); |
| 95 | data.value = update.value; |
| 96 | i = i + 1; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | // GOOD: Single storage access with batch update |
| 101 | public fun batch_update(account: &signer, updates: vector<Update>) { |
| 102 | let data = borrow_global_mut<Data>(signer::address_of(account)); |
| 103 | let i = 0; |
| 104 | while (i < vector::length(&updates)) { |
| 105 | let update = vector::borrow(&updates, i); |
| 106 | // Update in memory |
| 107 | update_memory_data(data, update); |
| 108 | i = i + 1; |
| 109 | } |
| 110 | } |
| 111 | ``` |
| 112 | |
| 113 | #### 2. Storage Packing |
| 114 | |
| 115 | ```move |
| 116 | // BAD: Wasteful storage |
| 117 | struct UserData has key { |
| 118 | active: bool, // 1 byte used, 7 wasted |
| 119 | level: u8, // 1 byte used, 7 wasted |
| 120 | score: u64, // 8 bytes |
| 121 | timestamp: u64, // 8 bytes |
| 122 | // Total: 32 bytes (50% wasted) |
| 123 | } |
| 124 | |
| 125 | // GOOD: Packed storage |
| 126 | struct UserData has key { |
| 127 | // Pack small fields together |
| 128 | flags: u8, // Bits: [active, reserved...] |
| 129 | level: u8, |
| 130 | reserved: u16, // Future use |
| 131 | score: u64, |
| 132 | timestamp: u64, |
| 133 | // Total: 20 bytes (37.5% saved) |
| 134 | } |
| 135 | ``` |
| 136 | |
| 137 | #### 3. Lazy Evaluation |
| 138 | |
| 139 | ```move |
| 140 | // BAD: Always compute expensive value |
| 141 | struct Pool has key { |
| 142 | total_shares: u64, |
| 143 | total_assets: u64, |
| 144 | // Computed on every update |
| 145 | share_price: u64, |
| 146 | } |
| 147 | |
| 148 | // GOOD: Compute only when needed |
| 149 | struct Pool has key { |
| 150 | total_shares: u64, |
| 151 | total_assets: u64, |
| 152 | // Don't store computed values |
| 153 | } |
| 154 | |
| 155 | public fun get_share_price(pool_addr: address): u64 { |
| 156 | let pool = borrow_global<Pool>(pool_addr); |
| 157 | if (pool.total_shares == 0) { |
| 158 | INITIAL_SHARE_PRICE |
| 159 | } else { |
| 160 | pool.total_assets * PRECISION / pool.total_shares |
| 161 | } |
| 162 | } |
| 163 | ``` |
| 164 | |
| 165 | #### 4. Event Optimization |
| 166 | |
| 167 | ```move |
| 168 | // BAD: Large event data |
| 169 | struct TradeEvent has drop, store { |
| 170 | pool: Object<Pool>, |
| 171 | trader: address, |
| 172 | token_in: Object<Token>, |
| 173 | token_out: Object<Token>, |
| 174 | amount_in: u64, |
| 175 | amount_out: u64, |
| 176 | fees: u64, |
| 177 | timestamp: u64, |
| 178 | metadata: vector<u8>, // Large metadata |
| 179 | } |
| 180 | |
| 181 | // GOOD: Minimal event data |
| 182 | struct TradeEvent has drop, store { |
| 183 | pool_id: u64, // Use ID instead of Object |
| 184 | trader: address, |
| 185 | amounts: u128, // Pack amount_in and amount_out |
| 186 | fees: u64, |
| 187 | // Compute other data from state |
| 188 | } |
| 189 | ``` |
| 190 | |
| 191 | #### 5. Collection Optimization |
| 192 | |
| 193 | ```move |
| 194 | // BAD: Linear search |
| 195 | public fun find_item(items: &vector<Item>, id: u64): Option<Item> { |
| 196 | let i = 0; |
| 197 | while (i < vector::length(it |