SolidityIn

Gas Golfing: 7 Tricks to Cut Your Solidity Transaction Costs in Half

November 5, 2026 · admin · Uncategorized


7 Tricks to Cut Your Solidity Transaction Costs in Half

In the world of Ethereum development, “Gas Golfing” is the art of optimizing smart contracts to use the minimum amount of gas possible. High gas costs can make your DApp unusable for retail users. By implementing these seven advanced Solidity patterns, you can significantly reduce the overhead of your transactions.

1. Use calldata Instead of memory

When passing large arrays or structs to external functions, always use the calldata keyword instead of memory. Unlike memory, calldata does not create a copy of the data in execution memory, saving thousands of gas units on large inputs.

// Expensive
function processData(uint256[] memory data) external { ... }
// Gas Optimized
function processData(uint256[] calldata data) external { ... }

2. Cache State Variables in Local Memory

Reading from storage (SLOAD) is one of the most expensive operations in the EVM. If you need to access a state variable multiple times within a loop or a function, assign it to a local variable first.

uint256 public total;
// Expensive: Reads 'total' from storage every iteration
for (uint256 i = 0; i < data.length; i++) {
total += data[i];
}
// Optimized: Reads 'total' once, then updates storage at the end
uint256 tempTotal = total;
for (uint256 i = 0; i < data.length; i++) {
tempTotal += data[i];
}
total = tempTotal;

3. The unchecked Block (Solidity 0.8+)

Since version 0.8.0, Solidity automatically checks for arithmetic overflows and underflows. While safe, this adds extra opcodes. If you are certain a calculation (like an array index increment) won’t overflow, wrap it in an unchecked block.

// Standard increment in loops
for (uint256 i = 0; i < length; ) {
// Logic here
unchecked { i++; }
}

4. Variable Packing (Storage Optimization)

Ethereum stores data in 32-byte slots. If you use smaller data types (like uint128, uint64, or bool) and place them next to each other in your contract state, Solidity will “pack” them into a single slot, reducing storage costs.

// Takes 3 slots
struct Bad {
uint256 a;
uint256 b;
uint128 c;
}
// Takes 2 slots (b and c are packed together)
struct Good {
uint256 a;
uint128 b;
uint128 c;
}

5. Short-Circuiting Rules

In if statements or require checks, order your conditions carefully. For OR (||), place the condition most likely to be true first. For AND (&&), place the condition most likely to be false first. This allows the EVM to skip the second check.

// If isFrequentUser is true, the expensive check is skipped
if (isFrequentUser || veryExpensiveCheck()) {
// Logic
}

6. Avoid Default Initialization

In Solidity, every variable is initialized to its default value (0, false, address(0)) automatically. Manually initializing a variable to its default value (e.g., uint256 i = 0;) is redundant and wastes gas.

// Gas Waste
for (uint256 i = 0; i < 10; i++) { ... }
// Optimized
for (uint256 i; i < 10; i++) { ... }

7. Use Events for Off-Chain Data

If your smart contract doesn't need to read a specific piece of data later, don't store it in a state variable. Emitting an event is significantly cheaper than updating storage. Off-chain services (like The Graph or Etherscan) can still track this data easily.

// Expensive
mapping(address => string) public userBios;
// Gas Optimized (if the contract doesn't need to use the bio)
event BioUpdated(address indexed user, string bio);

Tags: , , ,