ストレージスロットとガス最適化
Solidityが状態変数を32バイトのストレージスロットに配置する仕組みを理解し、その知識を使ってガス効率の高いコントラクトを作成します。
「ストレージスロットとガス最適化」はCoddyKit上の無料Blockchain Smart Contracts with Solidityレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはBlockchain Smart Contracts with Solidity学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Blockchain Smart Contracts with Solidityコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
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 1Declaration 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 onceAvoiding 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.
AI チューターと学ぶ Blockchain Smart Contracts with Solidity — 無料
ブラウザでリアルコードを書いて実行し、24/7 の AI チューターから瞬時にサポートを受け、ウェブまたはアプリで続きから学習できます。
- コース
- 12
- レッスン
- 48
よくある質問
「ストレージスロットとガス最適化」レッスンは無料ですか?
はい。「ストレージスロットとガス最適化」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Blockchain Smart Contracts with Solidityコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Blockchain Smart Contracts with Solidityコースには全4レッスンが含まれています。
「ストレージスロットとガス最適化」で何を学びますか?
Solidityが状態変数を32バイトのストレージスロットに配置する仕組みを理解し、その知識を使ってガス効率の高いコントラクトを作成します。 ブラウザで直接実行するハンズオンコードでBlockchain Smart Contracts with Solidityを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Blockchain Smart Contracts with Solidityを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのBlockchain Smart Contracts with Solidityは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「ストレージスロットとガス最適化」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このBlockchain Smart Contracts with Solidityレッスンでコードを書いて実行できますか?
はい。すべてのBlockchain Smart Contracts with Solidityレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- 状態変数とストレージレイアウト
- マッピングと動的配列
- イベントとログデータ
- ストレージスロットとガス最適化