← Back to Skills Marketplace
iskysun96

Analyze Gas Optimization

by iskysun96 · GitHub ↗ · v1.0.0 · MIT-0
cross-platform ✓ Security Clean
161
Downloads
0
Stars
0
Active Installs
1
Versions
Install in OpenClaw
/install analyze-gas-optimization
Description
Analyze and optimize Aptos Move contracts for gas efficiency, identifying expensive operations and suggesting optimizations. Triggers on: 'optimize gas', 're...
README (SKILL.md)

Skill: analyze-gas-optimization

Analyze and optimize Aptos Move contracts for gas efficiency, identifying expensive operations and suggesting optimizations.

When to Use This Skill

Trigger phrases:

  • "optimize gas", "reduce gas costs", "gas analysis"
  • "make contract cheaper", "gas efficiency"
  • "analyze gas usage", "gas optimization"
  • "reduce transaction costs"

Use cases:

  • Before mainnet deployment
  • When transaction costs are high
  • When optimizing for high-frequency operations
  • When building DeFi protocols with many transactions

Core Gas Optimization Principles

1. Storage Optimization

  • Minimize stored data size
  • Use efficient data structures
  • Pack struct fields efficiently
  • Remove unnecessary fields

2. Computation Optimization

  • Avoid loops over large collections
  • Cache repeated calculations
  • Use bitwise operations when possible
  • Minimize vector operations

3. Reference Optimization

  • Prefer borrowing over moving when possible
  • Use & and &mut efficiently
  • Avoid unnecessary copies

Gas Cost Analysis

Expensive Operations

1. Global Storage Operations

// EXPENSIVE: Writing to global storage
move_to(account, large_struct);

// EXPENSIVE: Reading and writing
let data = borrow_global_mut\x3CLargeData>(addr);

// EXPENSIVE: Checking existence
if (exists\x3CResource>(addr)) { ... }

2. Vector Operations

// EXPENSIVE: Growing vectors dynamically
vector::push_back(&mut vec, item); // O(n) worst case

// EXPENSIVE: Searching vectors
vector::contains(&vec, &item); // O(n)

// EXPENSIVE: Removing from middle
vector::remove(&mut vec, index); // O(n)

3. String Operations

// EXPENSIVE: String concatenation
string::append(&mut s1, s2);

// EXPENSIVE: UTF8 validation
string::utf8(bytes);

Optimization Patterns

1. Batch Operations

// BAD: Multiple storage accesses
public fun update_values(account: &signer, updates: vector\x3CUpdate>) {
    let i = 0;
    while (i \x3C vector::length(&updates)) {
        let update = vector::borrow(&updates, i);
        let data = borrow_global_mut\x3CData>(update.address);
        data.value = update.value;
        i = i + 1;
    }
}

// GOOD: Single storage access with batch update
public fun batch_update(account: &signer, updates: vector\x3CUpdate>) {
    let data = borrow_global_mut\x3CData>(signer::address_of(account));
    let i = 0;
    while (i \x3C vector::length(&updates)) {
        let update = vector::borrow(&updates, i);
        // Update in memory
        update_memory_data(data, update);
        i = i + 1;
    }
}

2. Storage Packing

// BAD: Wasteful storage
struct UserData has key {
    active: bool,      // 1 byte used, 7 wasted
    level: u8,         // 1 byte used, 7 wasted
    score: u64,        // 8 bytes
    timestamp: u64,    // 8 bytes
    // Total: 32 bytes (50% wasted)
}

// GOOD: Packed storage
struct UserData has key {
    // Pack small fields together
    flags: u8,         // Bits: [active, reserved...]
    level: u8,
    reserved: u16,     // Future use
    score: u64,
    timestamp: u64,
    // Total: 20 bytes (37.5% saved)
}

3. Lazy Evaluation

// BAD: Always compute expensive value
struct Pool has key {
    total_shares: u64,
    total_assets: u64,
    // Computed on every update
    share_price: u64,
}

// GOOD: Compute only when needed
struct Pool has key {
    total_shares: u64,
    total_assets: u64,
    // Don't store computed values
}

public fun get_share_price(pool_addr: address): u64 {
    let pool = borrow_global\x3CPool>(pool_addr);
    if (pool.total_shares == 0) {
        INITIAL_SHARE_PRICE
    } else {
        pool.total_assets * PRECISION / pool.total_shares
    }
}

4. Event Optimization

// BAD: Large event data
struct TradeEvent has drop, store {
    pool: Object\x3CPool>,
    trader: address,
    token_in: Object\x3CToken>,
    token_out: Object\x3CToken>,
    amount_in: u64,
    amount_out: u64,
    fees: u64,
    timestamp: u64,
    metadata: vector\x3Cu8>, // Large metadata
}

// GOOD: Minimal event data
struct TradeEvent has drop, store {
    pool_id: u64,        // Use ID instead of Object
    trader: address,
    amounts: u128,       // Pack amount_in and amount_out
    fees: u64,
    // Compute other data from state
}

5. Collection Optimization

// BAD: Linear search
public fun find_item(items: &vector\x3CItem>, id: u64): Option\x3CItem> {
    let i = 0;
    while (i \x3C vector::length(items)) {
        let item = vector::borrow(items, i);
        if (item.id == id) {
            return option::some(*item)
        };
        i = i + 1;
    }
    option::none()
}

// GOOD: Use Table for O(1) lookup
struct Storage has key {
    items: Table\x3Cu64, Item>,
}

public fun find_item(storage: &Storage, id: u64): Option\x3CItem> {
    if (table::contains(&storage.items, id)) {
        option::some(*table::borrow(&storage.items, id))
    } else {
        option::none()
    }
}

Gas Measurement

1. Transaction Simulation

# Simulate to get gas estimate
aptos move run-function \
    --function-id 0x1::module::function \
    --args ... \
    --simulate

# Output includes:
# - gas_unit_price
# - max_gas_amount
# - gas_used

2. Gas Profiling

#[test]
public fun test_gas_usage() {
    // Measure gas for operation
    let gas_before = gas::remaining_gas();
    expensive_operation();
    let gas_used = gas_before - gas::remaining_gas();

    // Assert reasonable gas usage
    assert!(gas_used \x3C MAX_ACCEPTABLE_GAS, E_TOO_EXPENSIVE);
}

Optimization Checklist

Storage Checklist

  • Pack struct fields to minimize size
  • Use appropriate integer sizes (u8, u16, u32, u64)
  • Remove unnecessary fields
  • Consider off-chain storage for large data
  • Use events instead of storage for logs

Computation Checklist

  • Cache repeated calculations
  • Minimize loops over collections
  • Use early returns to skip unnecessary work
  • Batch similar operations
  • Avoid redundant checks

Collection Checklist

  • Use Table/TableWithLength for key-value lookups
  • Use SmartTable for large collections
  • Limit vector sizes
  • Consider pagination for large results
  • Use appropriate data structures

Best Practices

  • Profile before and after optimization
  • Test gas usage in unit tests
  • Document gas costs for public functions
  • Consider gas costs in contract design
  • Monitor mainnet gas usage

Common Gas Optimizations

1. Replace Vectors with Tables

// Before: O(n) search
struct Registry has key {
    users: vector\x3CUser>,
}

// After: O(1) lookup
struct Registry has key {
    users: Table\x3Caddress, User>,
    user_list: vector\x3Caddress>, // If iteration needed
}

2. Minimize Storage Reads

// Before: Multiple reads
public fun transfer(from: &signer, to: address, amount: u64) {
    assert!(get_balance(signer::address_of(from)) >= amount, E_INSUFFICIENT);
    let from_balance = borrow_global_mut\x3CBalance>(signer::address_of(from));
    let to_balance = borrow_global_mut\x3CBalance>(to);
    // ...
}

// After: Single read with validation
public fun transfer(from: &signer, to: address, amount: u64) {
    let from_addr = signer::address_of(from);
    let from_balance = borrow_global_mut\x3CBalance>(from_addr);
    assert!(from_balance.value >= amount, E_INSUFFICIENT);
    // ... rest of logic
}

3. Use Bitwise Flags

// Before: Multiple bool fields (8 bytes each)
struct Settings has copy, drop, store {
    is_active: bool,
    is_paused: bool,
    is_initialized: bool,
    allows_deposits: bool,
}

// After: Single u8 (1 byte)
struct Settings has copy, drop, store {
    flags: u8, // Bit 0: active, 1: paused, 2: initialized, 3: deposits
}

const FLAG_ACTIVE: u8 = 1;        // 0b00000001
const FLAG_PAUSED: u8 = 2;        // 0b00000010
const FLAG_INITIALIZED: u8 = 4;   // 0b00000100
const FLAG_DEPOSITS: u8 = 8;      // 0b00001000

public fun is_active(settings: &Settings): bool {
    (settings.flags & FLAG_ACTIVE) != 0
}

Gas Optimization Report Template

# Gas Optimization Report

## Summary

- Current average gas: X units
- Optimized average gas: Y units
- Savings: Z% reduction

## Optimizations Applied

### 1. Storage Optimization

- Packed struct fields (saved X bytes)
- Replaced vectors with tables (O(n) → O(1))
- Removed redundant fields

### 2. Computation Optimization

- Cached price calculations (saved X operations)
- Batched updates (N calls → 1 call)
- Early returns in validation

### 3. Event Optimization

- Reduced event size from X to Y bytes
- Removed redundant event fields

## Measurements

| Function | Before | After  | Savings |
| -------- | ------ | ------ | ------- |
| mint     | 50,000 | 35,000 | 30%     |
| transfer | 30,000 | 25,000 | 17%     |
| swap     | 80,000 | 60,000 | 25%     |

## Recommendations

1. Consider further optimizations for high-frequency functions
2. Monitor mainnet usage patterns
3. Set up gas usage alerts

Integration Notes

  • Works with security-audit to ensure optimizations don't compromise security
  • Use with generate-tests to verify optimizations maintain correctness
  • Apply before deploy-contracts for mainnet deployments
  • Reference STORAGE_OPTIMIZATION.md for detailed patterns

NEVER Rules

  • ❌ NEVER optimize away security checks (access control, input validation)
  • ❌ NEVER deploy optimized code without re-testing
  • ❌ NEVER read .env or ~/.aptos/config.yaml during gas analysis (contain private keys)

References

Usage Guidance
This is an instruction-only Aptos Move gas-optimization guide that appears coherent and benign. Before installing or invoking it, confirm you have the aptos CLI and Move code available (the SKILL.md uses aptos commands but the skill metadata doesn't list aptos as a required binary). Be cautious about allowing any agent to run commands against your local repository or blockchain accounts — avoid exposing private keys or secrets. If you plan to let the agent operate autonomously, ensure it only has access to testnet/devnet credentials or sandboxed code. If you need greater assurance, ask the skill author to explicitly declare required tooling (aptos CLI) and to clarify how contract source will be provided and whether any network access or signing keys are needed.
Capability Analysis
Type: OpenClaw Skill Name: analyze-gas-optimization Version: 1.0.0 The skill bundle 'analyze-gas-optimization' provides legitimate instructions and patterns for optimizing Aptos Move contracts. It includes helpful 'NEVER Rules' in SKILL.md that explicitly forbid the agent from bypassing security checks or accessing sensitive files like .env and ~/.aptos/config.yaml, demonstrating a security-conscious design.
Capability Assessment
Purpose & Capability
The skill's name and description (Aptos Move gas optimization) match the provided guidance, examples, and commands. However, SKILL.md references using the 'aptos' CLI for simulation and profiling while the registry metadata declares no required binaries — the skill should ideally list aptos (or note it as optional) so users know what tooling is expected.
Instruction Scope
Instructions stay within the stated purpose (gas-cost heuristics, code snippets, and simulation commands). The SKILL.md implies the agent/operator will run simulations against local contracts or test harnesses; it does not explicitly say how the agent obtains contract source or whether it should read repository files. Users should be aware the skill expects access to Move sources and/or the ability to run aptos CLI commands.
Install Mechanism
No install spec or code files are present (instruction-only). This minimizes installation risk — nothing is downloaded or written to disk by the skill itself.
Credentials
The skill declares no environment variables or credentials, which is appropriate. Minor inconsistency: runtime examples use the 'aptos' CLI and running simulations/tests may rely on local node/config or credentials in some workflows; these are not declared. The skill does not request unrelated secrets.
Persistence & Privilege
The skill is not marked always:true and does not ask to persist or modify other skills or system-wide settings. It does not request elevated or persistent privileges.
How to Use
  1. Make sure OpenClaw is installed (local or Docker)
  2. Run the install command in chat: /install analyze-gas-optimization
  3. After installation, invoke the skill by name or use /analyze-gas-optimization
  4. Provide required inputs per the skill's parameter spec and get structured output
Version History
v1.0.0
- Initial release of the "analyze-gas-optimization" skill. - Analyzes Aptos Move contracts for gas-heavy patterns and suggests optimizations. - Provides actionable principles for storage, computation, reference, and event optimizations. - Offers detailed examples of inefficient code and improved alternatives. - Includes a comprehensive optimization checklist and best practices for gas efficiency. - Describes gas measurement and profiling methods for accurate cost estimation.
Metadata
Slug analyze-gas-optimization
Version 1.0.0
License MIT-0
All-time Installs 0
Active Installs 0
Total Versions 1
Frequently Asked Questions

What is Analyze Gas Optimization?

Analyze and optimize Aptos Move contracts for gas efficiency, identifying expensive operations and suggesting optimizations. Triggers on: 'optimize gas', 're... It is an AI Agent Skill for Claude Code / OpenClaw, with 161 downloads so far.

How do I install Analyze Gas Optimization?

Run "/install analyze-gas-optimization" in the OpenClaw or Claude Code chat to install it in one step — no extra setup required.

Is Analyze Gas Optimization free?

Yes, Analyze Gas Optimization is completely free, licensed under MIT-0. You can download, install and use it at no cost.

Which platforms does Analyze Gas Optimization support?

Analyze Gas Optimization is cross-platform and runs anywhere OpenClaw / Claude Code is available (cross-platform).

Who created Analyze Gas Optimization?

It is built and maintained by iskysun96 (@iskysun96); the current version is v1.0.0.

💬 Comments