Blockchain Smart Contracts with Solidity · Lezione

Slot di storage e ottimizzazione del gas

Comprenda come Solidity disponga le variabili di stato in slot di storage da 32 byte e usi queste informazioni per scrivere contract efficienti in termini di gas.

Lezione 4 di 413 passaggi

Slot di storage e ottimizzazione del gas è una lezione Blockchain Smart Contracts with Solidity gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Blockchain Smart Contracts with Solidity, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Blockchain Smart Contracts with Solidity include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

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.

Gratis per iniziare

Impara Blockchain Smart Contracts with Solidity con un tutor IA — gratis

Scrivi ed esegui vero codice nel tuo browser, ricevi aiuto istantaneo da un tutor IA disponibile 24/7, e riprendi da dove hai lasciato sul web o nell'app.

Corsi
12
Lezioni
48

Domande Frequenti

La lezione «Slot di storage e ottimizzazione del gas» è gratuita?

Sì — il testo completo di «Slot di storage e ottimizzazione del gas» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Blockchain Smart Contracts with Solidity, passa a CoddyKit PRO. Il corso Blockchain Smart Contracts with Solidity include 4 lezioni in totale.

Cosa imparerò in «Slot di storage e ottimizzazione del gas»?

Comprenda come Solidity disponga le variabili di stato in slot di storage da 32 byte e usi queste informazioni per scrivere contract efficienti in termini di gas. Eserciti Blockchain Smart Contracts with Solidity con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare Blockchain Smart Contracts with Solidity?

Non è richiesta alcuna esperienza precedente. Blockchain Smart Contracts with Solidity su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.

Quanto tempo richiede la lezione «Slot di storage e ottimizzazione del gas»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione Blockchain Smart Contracts with Solidity?

Sì. Ogni lezione Blockchain Smart Contracts with Solidity include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Variabili di stato e layout dello storage
  2. Mapping e array dinamici
  3. Eventi e logging dei dati
  4. Slot di storage e ottimizzazione del gas
← Torna a Blockchain Smart Contracts with Solidity