SolidityIn

The Gas Optimizer’s Bible: 10 Ways to Cut Execution Costs in Solidity

November 5, 2026 · admin · Uncategorized


10 Essential Strategies to Cut Execution Costs in Solidity

In the world of Ethereum development, gas is money. High execution costs don’t just hurt the user’s wallet; they can make your protocol unusable during periods of network congestion. To build production-ready smart contracts, mastering gas optimization is not optional—it’s a necessity.

Here are 10 battle-tested strategies to optimize your Solidity code for maximum efficiency.

1. Use calldata Instead of memory

When defining external functions, use calldata for read-only arguments. Unlike memory, calldata does not copy the data to a new memory location; it points directly to the input data, saving significant gas on large arrays and strings.


// Expensive
function processData(string memory _text) public { ... }
// Efficient
function processData(string calldata _text) external { ... }

2. Optimize Storage Slots (Variable Packing)

The EVM stores data in 32-byte slots. By grouping smaller data types (like uint128, uint64, or bool) next to each other in your state variables, Solidity will pack them into a single slot, reducing expensive SSTORE operations.


// Bad: Uses 3 slots
uint128 a;
uint256 b;
uint128 c;
// Good: Uses 2 slots
uint128 a;
uint128 c;
uint256 b;

3. Use Custom Errors instead of Require Strings

Starting with Solidity 0.8.4, custom errors are available. They are significantly cheaper than require(condition, "Long error string") because they don’t store long strings in the contract bytecode.


error Unauthorized();
function withdraw() public {
if (msg.sender != owner) revert Unauthorized();
// ...
}

4. Cache Storage Variables in Memory

Accessing state variables (SLOAD) is much more expensive than accessing local variables (MLOAD). If you need to read a storage variable multiple times within a function, copy it to a local variable first.


uint256 total;
function sumValues(uint256[] calldata data) external {
uint256 _total = total; // Cache to memory
for(uint256 i = 0; i < data.length; i++) {
_total += data[i];
}
total = _total; // Write back once
}

5. Use unchecked for Arithmetic

Solidity 0.8.x has built-in overflow/underflow checks. This is safe but adds gas. If you are certain a value won’t overflow (e.g., a loop incrementer), use unchecked to save gas.


for (uint256 i = 0; i < 10; ) {
// Loop logic...
unchecked { i++; }
}

6. Constant and Immutable Variables

Variables that don’t change should be marked constant or immutable. These are stored directly in the contract bytecode rather than in a storage slot, making them nearly free to read.

  • Constant: Value is fixed at compile time.
  • Immutable: Value is fixed at deployment time.

7. Leverage Short-Circuiting

In logical expressions (&&, ||), put the cheaper condition first. If the first part of an OR is true, or the first part of an AND is false, the EVM won’t execute the second (potentially expensive) part.


// If isCheap() is true, isExpensive() is never called
if (isCheap() || isExpensive()) { ... }

8. Don’t Initialize Variables with Default Values

In Solidity, every variable is initialized to its “zero” value (0, false, address(0)) by default. Explicitly setting uint256 i = 0; is redundant and costs extra gas.


// Wasteful
uint256 x = 0;
// Efficient
uint256 x;

9. Use Mappings Over Arrays (Where Possible)

Iterating through arrays is gas-heavy and carries the risk of hitting the block gas limit. Mappings are O(1), meaning the gas cost remains constant regardless of how many items are stored.

10. Choose external over public

If you only expect a function to be called from outside the contract, use the external visibility modifier. public functions must copy arguments to memory because they might be called internally, while external functions can read them directly from calldata.

The Golden Rule: Always test! Use tools like hardhat-gas-reporter or Foundry’s --gas-report to measure the impact of your optimizations. Sometimes, readability is more valuable than saving 50 gas units.

© 2023 Solidity Gas Optimization Guide. Happy Coding!

Tags: , , ,