0Pricing
Blockchain Smart Contracts with Solidity · Lekcja

Sloty storage i optymalizacja gasu

Poznaj sposób, w jaki Solidity rozmieszcza zmienne stanu w 32-bajtowych slotach storage, i wykorzystaj tę wiedzę do pisania kontraktów zużywających mniej gasu.

Sloty storage i optymalizacja gasu to bezpłatna lekcja Blockchain Smart Contracts with Solidity na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Blockchain Smart Contracts with Solidity, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Blockchain Smart Contracts with Solidity zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

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.

Często zadawane pytania

Czy lekcja „Sloty storage i optymalizacja gasu” jest bezpłatna?

Tak — pełny tekst „Sloty storage i optymalizacja gasu” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Blockchain Smart Contracts with Solidity, przejdź na CoddyKit PRO. Kurs Blockchain Smart Contracts with Solidity zawiera 4 lekcji w sumie.

Co nauczysz się w „Sloty storage i optymalizacja gasu”?

Poznaj sposób, w jaki Solidity rozmieszcza zmienne stanu w 32-bajtowych slotach storage, i wykorzystaj tę wiedzę do pisania kontraktów zużywających mniej gasu. Ćwiczysz Blockchain Smart Contracts with Solidity z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć Blockchain Smart Contracts with Solidity?

Nie wymagamy żadnego doświadczenia. Blockchain Smart Contracts with Solidity w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.

Ile czasu zajmuje lekcja „Sloty storage i optymalizacja gasu”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji Blockchain Smart Contracts with Solidity?

Tak. Każda lekcja Blockchain Smart Contracts with Solidity zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Zmienne stanu i układ pamięci
  2. Mapowania i tablice dynamiczne
  3. Zdarzenia i logowanie danych
  4. Sloty storage i optymalizacja gasu
← Powrót do Blockchain Smart Contracts with Solidity