solidity-gas-optimization by pseudoyu/agent-skills
npx skills add https://github.com/pseudoyu/agent-skills --skill solidity-gas-optimizationSolidity 智能合约的全面 Gas 优化指南,包含 8 个类别超过 80 种技术。基于 RareSkills Gas 优化手册。规则根据影响力和安全性进行优先级排序。
在以下情况下参考这些指南:
| 优先级 | 类别 | 影响力 | 风险 |
|---|---|---|---|
| 1 | 存储优化 | 关键 | 低 |
| 2 | 部署优化 | 高 | 低 |
| 3 | Calldata 优化 | 高 | 低 |
| 4 | 设计模式 | 高 | 中 |
| 5 |
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
| 跨合约调用 |
| 中高 |
| 中 |
| 6 | 编译器优化 | 中 | 低 |
| 7 | 汇编技巧 | 中 | 高 |
| 8 | 危险技术 | 低 | 关键 |
零到一写入:
变量打包:
// 差:占用 3 个存储槽
struct Unpacked {
uint64 time; // 槽 1
uint256 amount; // 槽 2
address user; // 槽 3
}
// 好:占用 2 个存储槽
struct Packed {
uint64 time; // 槽 1 (与 address 一起)
address user; // 槽 1
uint256 amount; // 槽 2
}
缓存:
// 差:两次读取存储
function increment() public {
require(count < 10);
count = count + 1;
}
// 好:一次读取存储
function increment() public {
uint256 _count = count;
require(_count < 10);
count = _count + 1;
}
常量与不可变量:
uint256 constant MAX = 100; // 不占用存储槽
address immutable owner; // 在构造函数中设置,不占用存储
自定义错误:
// 差:约 64+ 字节
require(amount <= limit, "Amount exceeds limit");
// 好:约 4 字节
error ExceedsLimit();
if (amount > limit) revert ExceedsLimit();
可支付构造函数:
// 部署时节省约 200 gas
constructor() payable {}
克隆模式:
Calldata 与 Memory:
// 差:复制到内存
function process(bytes memory data) external {}
// 好:直接从 calldata 读取
function process(bytes calldata data) external {}
避免有符号整数:
代币标准:
签名与默克尔树:
替代库:
减少交互:
访问列表:
循环模式:
// 好:使用 unchecked 递增,缓存长度
uint256 len = arr.length;
for (uint256 i; i < len; ) {
// 逻辑
unchecked { ++i; }
}
命名返回值:
// 字节码效率更高
function calc(uint256 x) pure returns (uint256 result) {
result = x * 2;
}
位移操作:
// 更便宜:3 gas
x << 1 // x * 2
x >> 2 // x / 4
// 更昂贵:5 gas
x * 2
x / 4
布尔短路运算:
&& 中,将更可能失败的条件放在前面|| 中,将更可能成功的条件放在前面高效检查:
// 使用汇编检查 address(0)
assembly {
if iszero(caller()) { revert(0, 0) }
}
// 奇偶检查
x & 1 // 替代 x % 2
内存复用:
这些技术在生产环境中不安全:
这些在现代 Solidity 中不再适用:
包含代码示例的完整文档:
references/solidity-gas-guidelines.md - 完整指南references/rules/ - 按类别划分的独立模式要查找特定模式:
grep -l "storage" references/rules/
grep -l "assembly" references/rules/
grep -l "struct" references/rules/
references/rules/ 中的规则类别storage-* - 存储优化模式deploy-* - 部署 Gas 节省calldata-* - Calldata 优化design-* - 设计模式选择crosscall-* - 跨合约调用优化compiler-* - 编译器优化模式assembly-* - 低级汇编技巧--via-ir - 现代编译器选项可能使一些技巧过时每周安装次数
88
代码仓库
GitHub Stars
4
首次出现
Jan 20, 2026
安全审计
安装于
opencode74
gemini-cli71
codex70
github-copilot60
claude-code59
cursor54
Comprehensive gas optimization guide for Solidity smart contracts, containing 80+ techniques across 8 categories. Based on the RareSkills Book of Gas Optimization. Rules are prioritized by impact and safety.
Reference these guidelines when:
| Priority | Category | Impact | Risk |
|---|---|---|---|
| 1 | Storage Optimization | CRITICAL | LOW |
| 2 | Deployment Optimization | HIGH | LOW |
| 3 | Calldata Optimization | HIGH | LOW |
| 4 | Design Patterns | HIGH | MEDIUM |
| 5 | Cross-Contract Calls | MEDIUM-HIGH | MEDIUM |
| 6 | Compiler Optimizations | MEDIUM | LOW |
| 7 | Assembly Tricks | MEDIUM | HIGH |
| 8 | Dangerous Techniques | LOW | CRITICAL |
Zero-to-One Writes:
Variable Packing:
// Bad: 3 slots
struct Unpacked {
uint64 time; // slot 1
uint256 amount; // slot 2
address user; // slot 3
}
// Good: 2 slots
struct Packed {
uint64 time; // slot 1 (with address)
address user; // slot 1
uint256 amount; // slot 2
}
Caching:
// Bad: reads storage twice
function increment() public {
require(count < 10);
count = count + 1;
}
// Good: reads storage once
function increment() public {
uint256 _count = count;
require(_count < 10);
count = _count + 1;
}
Constants & Immutables:
uint256 constant MAX = 100; // No storage slot
address immutable owner; // Set in constructor, no storage
Custom Errors:
// Bad: ~64+ bytes
require(amount <= limit, "Amount exceeds limit");
// Good: ~4 bytes
error ExceedsLimit();
if (amount > limit) revert ExceedsLimit();
Payable Constructors:
// Saves ~200 gas on deployment
constructor() payable {}
Clone Patterns:
Calldata vs Memory:
// Bad: copies to memory
function process(bytes memory data) external {}
// Good: reads directly from calldata
function process(bytes calldata data) external {}
Avoid Signed Integers:
Token Standards:
Signature vs Merkle:
Alternative Libraries:
Reduce Interactions:
Access Lists:
Loop Patterns:
// Good: unchecked increment, cached length
uint256 len = arr.length;
for (uint256 i; i < len; ) {
// logic
unchecked { ++i; }
}
Named Returns:
// More efficient bytecode
function calc(uint256 x) pure returns (uint256 result) {
result = x * 2;
}
Bitshifting:
// Cheaper: 3 gas
x << 1 // x * 2
x >> 2 // x / 4
// Expensive: 5 gas
x * 2
x / 4
Short-Circuit Booleans:
&&||Efficient Checks:
// Check address(0) with assembly
assembly {
if iszero(caller()) { revert(0, 0) }
}
// Even/odd check
x & 1 // instead of x % 2
Memory Reuse:
These are unsafe for production:
These no longer apply in modern Solidity:
Full documentation with code examples:
references/solidity-gas-guidelines.md - Complete guidereferences/rules/ - Individual patterns by categoryTo look up specific patterns:
grep -l "storage" references/rules/
grep -l "assembly" references/rules/
grep -l "struct" references/rules/
references/rules/storage-* - Storage optimization patternsdeploy-* - Deployment gas savingscalldata-* - Calldata optimizationdesign-* - Design pattern choicescrosscall-* - Cross-contract call optimizationcompiler-* - Compiler optimization patternsassembly-* - Low-level assembly tricks--via-ir - Modern compiler option may obsolete some tricksWeekly Installs
88
Repository
GitHub Stars
4
First Seen
Jan 20, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
opencode74
gemini-cli71
codex70
github-copilot60
claude-code59
cursor54
React 组合模式指南:Vercel 组件架构最佳实践,提升代码可维护性
120,000 周安装