aptos-gas-optimization

$npx mdskill add eric861129/SKILLS_All-in-one/aptos-gas-optimization

Optimizes gas costs and performance for Aptos Move smart contracts.

  • Reduces gas fees by optimizing storage, execution, and data structures.
  • Uses Read, Write, Edit, Grep, Glob, and Bash tools for analysis.
  • Recommends based on gas model, data structure costs, and profiling.
  • Provides code examples and optimization strategies in responses.

SKILL.md

.github/skills/aptos-gas-optimizationView on GitHub ↗
---
name: aptos-gas-optimization
description: Aptos gas optimization expert for Move smart contracts. Covers storage costs, execution efficiency, inline functions, aggregators for parallel execution, Table vs SmartTable vs vector tradeoffs, event optimization, struct packing, and gas profiling tools.
allowed-tools: Read, Write, Edit, Grep, Glob, Bash
model: sonnet
license: MIT
metadata:
  author: raintree
  version: "1.0"
---

# Aptos Gas & Performance Optimization Expert

Expert on optimizing gas costs and performance for Aptos Move smart contracts.

## Triggers

- gas optimization, gas cost, gas fees
- performance tuning, execution efficiency
- storage fees, storage optimization
- inline functions, aggregator
- parallel execution, throughput
- Table vs vector, SmartTable
- gas profiling, benchmarking

## Gas Model Overview

```
Total Gas Cost = Execution Gas + Storage Gas + IO Gas

Gas Fee (APT) = Gas Units x Gas Unit Price
1 APT = 100,000,000 octas
```

## Data Structure Costs

| Structure | Read | Write | Best For |
|-----------|------|-------|----------|
| vector | O(n) | O(n) | Small lists (<1000) |
| SimpleMap | O(n) | O(n) | Small maps (<1000) |
| Table | O(1) | O(1) | Large maps |
| SmartTable | O(1) | O(1) | Very large maps (100k+) |
| Aggregator | O(1) | O(1) | **Parallel counters** |

## Key Optimizations

### 1. Use Aggregators for Global Counters

```move
use aptos_framework::aggregator_v2::{Self, Aggregator};

struct Stats has key {
    total_users: Aggregator<u64>  // Concurrent-safe!
}

public fun increment() acquires Stats {
    let stats = borrow_global_mut<Stats>(@protocol);
    aggregator_v2::add(&mut stats.total_users, 1);
    // Multiple txns can do this in parallel!
}
```

### 2. Inline Small Functions

```move
inline fun min(a: u64, b: u64): u64 {
    if (a < b) a else b
}
```

### 3. Cache Loop Lengths

```move
// Bad: calls length() every iteration
while (i < vector::length(v)) { ... }

// Good: cache length
let len = vector::length(v);
while (i < len) { ... }
```

### 4. Use Event V2 (Cheaper)

```move
#[event]
struct TransferEvent has drop, store {
    from: address,
    to: address,
    amount: u64,
}

event::emit(TransferEvent { from, to, amount });
```

### 5. Pack Struct Fields

```move
// Bad: 8 bytes wasted on padding
struct Data { flag1: bool, value1: u64, flag2: bool, value2: u64 }

// Good: group bools together
struct Data { flag1: bool, flag2: bool, value1: u64, value2: u64 }

// Best: pack into bitmap
struct Data { flags: u8, value1: u64, value2: u64 }
```

### 6. Use Table/SmartTable for Large Data

```move
// Bad for large datasets
struct Registry { users: vector<User> }

// Good for large datasets
struct Registry { users: Table<address, User> }

// Best for very large datasets (100k+)
struct Registry { users: SmartTable<address, User> }
```

### 7. Batch Operations

```move
// Single transaction for multiple transfers
public entry fun batch_transfer(
    sender: &signer,
    recipients: vector<address>,
    amounts: vector<u64>
) { ... }
```

### 8. Early Returns

```move
// Check cheapest conditions first
if (amount == 0) return;
if (amount >= MAX) return;
if (!is_valid(user)) return;
// Then do expensive work
```

## Gas Profiling

```bash
# Run tests with gas profiling
aptos move test --gas

# Simulate transaction
aptos move run --function-id 0x1::module::func --profile gas
```

## Checklist Before Mainnet

- [ ] Use aggregators for global counters
- [ ] Inline small helper functions
- [ ] Use Event V2 for all events
- [ ] Use Table/SmartTable for large datasets
- [ ] Pack struct fields efficiently
- [ ] Cache loop lengths
- [ ] Batch operations where possible
- [ ] Early returns for validation
- [ ] Profile with --gas flag

More from eric861129/SKILLS_All-in-one

SkillDescription
agnixUse when user asks to 'lint agent configs', 'validate skills', 'check CLAUDE.md', 'validate hooks', 'lint MCP'. Validates agent configuration files against 231 rules across 10+ AI tools.
anthropic-expertExpert on Anthropic Claude API, models, prompt engineering, function calling, vision, and best practices. Triggers on anthropic, claude, api, prompt, function calling, vision, messages api, embeddings
aptos-cliExpert in Aptos CLI for account management, contract deployment, transaction submission, and node operations. Use when using the Aptos CLI to initialize profiles, deploy Move contracts, submit transactions, manage accounts, or operate Aptos nodes.
aptos-coreExpert in Aptos blockchain core architecture: consensus (AptosBFT), execution (Block-STM), and networking. Use when analyzing Aptos protocol design, the Rust codebase, validator operations, or understanding how Block-STM parallel execution works.
aptos-dapp-integrationExpert on building Aptos dApps with frontend integration. Covers wallet connectivity (Petra, Martian, Pontem), wallet adapter patterns, TypeScript SDK, transaction building and submission, account management, and React/Next.js integration.
aptos-frameworkExpert on Aptos Framework (0x1 standard library) modules including account, coin, fungible_asset, object, timestamp, table, smart_table, event, randomness, aggregator, and resource_account. Essential for all Aptos development.
aptos-indexerExpert in Aptos Indexer architecture and GraphQL API for querying blockchain data. Use when querying NFTs, coin balances, or transaction history via GraphQL, designing custom processors, or optimizing data retrieval from the Aptos blockchain.
aptos-move-languageExpert on Move programming language fundamentals including abilities (copy/drop/store/key), generics, phantom types, references, global storage operations (move_to/move_from/borrow_global), signer pattern, visibility modifiers, and advanced type system features.
aptos-move-testingExpert on testing Move smart contracts including unit tests, integration tests, Move Prover formal verification, debugging strategies, test coverage, and CI/CD integration for Aptos development.
aptos-object-modelExpert on Aptos Object Model for composable, transferable on-chain assets. Use when working with ObjectCore, Object wrappers, ConstructorRef, ExtendRef, DeleteRef, TransferRef capabilities, or implementing composable asset patterns on Aptos blockchain.