Gas optimization is the systematic reduction of computational resources required for smart contract execution on blockchain networks, achieved through efficient storage patterns, opcode selection, data structure design, and batching strategies to minimise transaction costs and improve economic vi…

Semantic Classification

Content

Core Principles

  • Storage Minimization: Reduce expensive storage operations (SSTORE)

  • Computation Efficiency: Use cheaper opcodes when possible

  • Data Structure Optimization: Choose appropriate data structures for access patterns

  • Batch Operations: Combine multiple operations to amortize fixed costs

    Gas Cost Hierarchy (Ethereum)

    Expensive Operations

  • Storage writes (SSTORE): 20,000-5,000 gas

  • Contract creation: 32,000+ gas

  • External calls: 2,600+ gas

  • Logging (LOG): 375+ gas per log

    Moderate Operations

  • Memory expansion: 3-6 gas per word

  • SHA3/Keccak256: 30+ gas

  • Storage reads (SLOAD): 2,100 gas (warm) / 800 gas (cold)

    Cheap Operations

  • Arithmetic: 3-5 gas

  • Stack operations: 2-3 gas

  • Memory reads: 3 gas

    Optimization Techniques

    Storage Optimization

  • Variable Packing: Combine multiple small variables into single storage slot

    // Optimized: 1 storage slot
    uint128 a;
    uint128 b;
     
    // Unoptimized: 2 storage slots
    uint256 a;
    uint256 b;
  • Use Memory: Prefer memory variables for temporary data

  • Immutable/Constant: Use for values set once or never change

  • Storage Pointers: Avoid redundant SLOAD operations

    Data Structure Selection

  • Mapping vs Array: Mappings for sparse data, arrays for iteration

  • bytes vs string: Use bytes for non-UTF8 data

  • uint256 vs smaller types: uint256 cheapest for single variables

    Function Optimization

  • Function Visibility: External cheaper than public for external calls

  • Short-Circuiting: Order conditions by likelihood/cost

  • Unchecked Math: Use unchecked {} for Solidity 0.8+ when overflow impossible

  • Custom Errors: Replace require strings with custom errors (Solidity 0.8.4+)

    Loop Optimization

  • Avoid storage access in loops

  • Cache array length

  • Use ++i instead of i++

  • Consider batch processing limits

    Batching Strategies

  • Batch token transfers

  • Batch state updates

  • Amortize signature verification costs

    Analysis Tools

  • Hardhat Gas Reporter: Per-function gas usage

  • eth-gas-reporter: Mocha integration for gas tracking

  • Foundry Gas Snapshots: Regression testing for gas costs

  • Tenderly: Gas profiler with visualization

  • Solidity Visual Developer: Gas annotation in IDE

    Benchmarking Techniques

  • A/B testing of alternative implementations

  • Gas usage regression tests

  • Comparative analysis against reference implementations

  • Profiling gas usage distribution

    Common Pitfalls

  • Premature optimization (readability vs efficiency)

  • Over-optimization leading to security issues

  • Ignoring worst-case gas costs

  • Not accounting for future EVM changes

    EVM-Specific Optimizations

  • Solidity: Optimizer enabled with appropriate runs parameter

  • Vyper: Inherent optimizations with explicit bounds

  • Yul/Assembly: Direct control over opcodes for maximum efficiency

  • Bytecode Size: Affects deployment cost

    Real-World Examples

    High-Impact Optimizations

  • Uniswap V3: Bitmap for tick tracking

  • ERC-1155: Batch transfers vs ERC-721

  • ERC-2535 Diamonds: Modular upgradeable contracts

    Gas Optimization Patterns

  • Bit manipulation for flags

  • Merkle proofs for large datasets

  • Signature aggregation for multi-party operations

    Trade-offs

  • Code Clarity: Optimized code may be harder to audit

  • Flexibility: Tight coupling for optimization limits upgradability

  • Development Time: Optimization adds complexity

  • Contract Size: Some optimizations increase bytecode size

    Future Developments

  • EVM improvements (e.g., Verkle trees reducing storage costs)

  • Layer 2 solutions with different cost models

  • Alternative VMs (e.g., eWASM) with different optimization profiles

  • Gas

  • Smart Contract

  • Transaction Fee

  • EVM (Ethereum Virtual Machine)

Provenance