0Pricing
Blockchain Smart Contracts with Solidity · Lesson

Storage Slots and Gas Optimization

Understand how Solidity lays out state variables into 32-byte storage slots and use that knowledge to write gas-efficient contracts.

Storage Slots and Gas Optimization is a free Blockchain Smart Contracts with Solidity lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Blockchain Smart Contracts with Solidity learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why Storage Costs Matter

Writing to contract storage is one of the most expensive operations on Ethereum. Understanding the storage layout lets you reduce slots written and save users real money.

The 32-Byte Slot

Contract storage is an array of slots, each 32 bytes (256 bits). State variables are assigned to slots in declaration order, starting at slot 0.

Variable Packing

Multiple small variables that fit within 32 bytes are packed into the same slot. A uint128 and another uint128 share one slot, halving storage writes if updated together.

uint128 a; // slot 0
uint128 b; // slot 0 (packed)
uint256 c; // slot 1

Declaration Order Matters

Packing only works for adjacent variables that fit. Poor ordering wastes slots. Group small types together rather than interleaving them with full-size uint256 variables.

Bad vs Good Ordering

Interleaving a small type between large ones prevents packing and uses extra slots.

// Bad: 3 slots
uint128 x;
uint256 y;
uint128 z;
// Good: 2 slots
uint256 y;
uint128 x;
uint128 z;

Mappings and Dynamic Arrays

Mappings and dynamic arrays do not pack. Their slot holds metadata, and actual values are stored at hashed locations. Each element write is its own storage cost.

Constants and Immutables

Values marked constant or immutable are stored in the contract bytecode, not in storage slots. Reading them is far cheaper than reading a state variable.

uint256 public constant MAX = 1000;
address public immutable owner;

Caching Storage in Memory

Reading the same storage variable repeatedly inside a loop is wasteful. Cache it in a memory local variable once, operate on it, then write back once.

uint256 total = balance; // read once
for (uint i; i < n; i++) total += amounts[i];
balance = total; // write once

Avoiding Redundant Writes

Writing a value identical to the current one still costs gas. Skip a storage write if the new value equals the existing one, or only update when something actually changed.

Cheaper Storage Refunds

Clearing a storage slot back to zero gives a partial gas refund. Patterns that delete unused entries can reclaim some cost, though refund rules have changed across network upgrades.

Measuring Gas Usage

Use a gas reporter to see the cost of each function. Optimize the hottest, most-called functions first; micro-optimizing rarely-used code is not worth the readability cost.

Quick Check

Check your storage optimization knowledge.

Recap

You learned to optimize contract storage:

  • Storage is 32-byte slots assigned in declaration order
  • Pack small adjacent variables to share slots
  • Use constant/immutable for fixed values
  • Cache storage in memory, avoid redundant writes, and measure gas

Smart storage layout directly lowers the cost of using your contract.

Frequently asked questions

Is the “Storage Slots and Gas Optimization” lesson free?

Yes — the full text of “Storage Slots and Gas Optimization” is free to read here on the web, and the Blockchain Smart Contracts with Solidity course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Blockchain Smart Contracts with Solidity course, upgrade to CoddyKit PRO.

What will I learn in “Storage Slots and Gas Optimization”?

Understand how Solidity lays out state variables into 32-byte storage slots and use that knowledge to write gas-efficient contracts. You practise Blockchain Smart Contracts with Solidity with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Blockchain Smart Contracts with Solidity?

No prior experience is required. Blockchain Smart Contracts with Solidity on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Storage Slots and Gas Optimization” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Blockchain Smart Contracts with Solidity lesson?

Yes. Every Blockchain Smart Contracts with Solidity lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. State Variables and Storage Layout
  2. Mappings and Dynamic Arrays
  3. Events and Logging Data
  4. Storage Slots and Gas Optimization
← Back to Blockchain Smart Contracts with Solidity