스토리지 슬롯과 가스 최적화
Solidity가 상태 변수를 32바이트 스토리지 슬롯에 배치하는 방식을 이해하고 이를 활용해 가스 효율적인 컨트랙트를 작성합니다.
스토리지 슬롯과 가스 최적화은(는) CoddyKit의 무료 Blockchain Smart Contracts with Solidity 강의입니다. 이것은 4개 중 4번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 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.
자주 묻는 질문
“스토리지 슬롯과 가스 최적화” 강의는 무료인가요?
네 — “스토리지 슬롯과 가스 최적화” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Blockchain Smart Contracts with Solidity 강의 전체를 잠금 해제할 수 있습니다. Blockchain Smart Contracts with Solidity 강의에는 총 4개의 강의가 포함되어 있습니다.
“스토리지 슬롯과 가스 최적화”에서 뭘 배우나요?
Solidity가 상태 변수를 32바이트 스토리지 슬롯에 배치하는 방식을 이해하고 이를 활용해 가스 효율적인 컨트랙트를 작성합니다. 브라우저에서 직접 실행하는 실습 코드로 Blockchain Smart Contracts with Solidity을(를) 배우며, 24/7 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 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- 상태 변수와 저장소 배치
- 매핑과 동적 배열
- 이벤트 및 로깅 데이터
- 스토리지 슬롯과 가스 최적화