Solidity theorytheory 0/50 · 0%
Gas · hard

41. Gas optimisation techniques

Where the savings actually are.

The biggest wins are structural: fewer storage writes, packed structs, `calldata` instead of `memory`, short-circuiting checks, and `unchecked` blocks for counters that provably cannot overflow.

for (uint256 i; i < n;) {
    // ...
    unchecked { ++i; }
}

Micro-tricks matter far less than avoiding an extra SSTORE. Measure with `forge snapshot` rather than guessing.

Check your understanding

  1. 1. Which change usually saves the most gas?

  2. 2. When is `unchecked` safe?

  3. 3. How should optimisation be validated?