aptos-gas-optimization
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.
- License 许可
- MIT
- 最近更新
- 2026-04-16
- Stars
- ★ 52
- Forks
- 13
01 前言 Front Matter
- name
- aptos-gas-optimization
- model
- sonnet
- license
- MIT
- metadata
- { "author": "raintree", "version": "1.0" }
- 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
03 SKILL.md 正文
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
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
inline fun min(a: u64, b: u64): u64 {
if (a < b) a else b
}
3. Cache Loop Lengths
// 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)
#[event]
struct TransferEvent has drop, store {
from: address,
to: address,
amount: u64,
}
event::emit(TransferEvent { from, to, amount });
5. Pack Struct Fields
// 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
// 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
// Single transaction for multiple transfers
public entry fun batch_transfer(
sender: &signer,
recipients: vector<address>,
amounts: vector<u64>
) { ... }
8. Early Returns
// Check cheapest conditions first
if (amount == 0) return;
if (amount >= MAX) return;
if (!is_valid(user)) return;
// Then do expensive work
Gas Profiling
# 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
04 同仓库更多 Skills
what-if-oracle
Run structured What-If scenario analysis with multi-branch possibility exploration. Use this skill when the user asks speculative questions like "what if...", "what would happen if...", "what are the possibilities", "explore scenarios", "scenario analysis", "possibility space", "what could go wrong", "best case / worst case", "risk analysis", "contingency planning", "strategic options", or any question about uncertain futures. Also trigger when the user faces a fork-in-the-road decision, wants to stress-test an idea, or needs to think through consequences before committing.
venue-templates
Access comprehensive LaTeX templates, formatting requirements, and submission guidelines for major scientific publication venues (Nature, Science, PLOS, IEEE, ACM), academic conferences (NeurIPS, ICML, CVPR, CHI), research posters, and grant proposals (NSF, NIH, DOE, DARPA). This skill should be used when preparing manuscripts for journal submission, conference papers, research posters, or grant proposals and need venue-specific formatting requirements and templates.
the-fool
Use when challenging ideas, plans, decisions, or proposals using structured critical reasoning. Invoke to play devil's advocate, run a pre-mortem, red team, or audit evidence and assumptions.
scientific-writing
Core skill for the deep research and writing tool. Write scientific manuscripts in full paragraphs (never bullet points). Use two-stage process with (1) section outlines with key points using research-lookup then (2) convert to flowing prose. IMRAD structure, citations (APA/AMA/Vancouver), figures/tables, reporting guidelines (CONSORT/STROBE/PRISMA), for research papers and journal submissions.
scientific-brainstorming
Creative research ideation and exploration. Use for open-ended brainstorming sessions, exploring interdisciplinary connections, challenging assumptions, or identifying research gaps. Best for early-stage research planning when you do not have specific observations yet. For formulating testable hypotheses from data use hypothesis-generation.
research-lookup
Look up current research information using the Parallel Chat API (primary) or Perplexity sonar-pro-search (academic paper searches). Automatically routes queries to the best backend. Use for finding papers, gathering research data, and verifying scientific information.