การเพิ่มประสิทธิภาพพื้นที่จัดเก็บ
การแพ็กและสล็อต
การเพิ่มประสิทธิภาพพื้นที่จัดเก็บ เป็นบทเรียน Web3 & DApp Development Fundamentals ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Web3 & DApp Development Fundamentals และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Web3 & DApp Development Fundamentals มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Storage Slots Basics
Contract storage is a giant array of 32-byte slots, indexed from slot 0. State variables are assigned to slots in declaration order.
Every slot you write costs gas, so the fewer slots your data occupies, the cheaper your contract is to use. Understanding the slot layout is the foundation of storage optimization.
contract Layout {
uint256 a; // slot 0
uint256 b; // slot 1
uint256 c; // slot 2
}Variable Packing
The Solidity compiler packs multiple small variables into a single 32-byte slot when they fit consecutively.
A uint128 uses 16 bytes, so two of them share one slot. A bool uses 1 byte and a uint8 uses 1 byte. Packing reduces both deployment and runtime storage costs.
contract Packed {
uint128 a; // slot 0 (bytes 0-15)
uint128 b; // slot 0 (bytes 16-31)
uint256 c; // slot 1
}Declaration Order Matters
Packing only works for variables that are adjacent in declaration. A poorly ordered layout wastes slots.
In the bad example below, the uint256 forces the two uint128s into separate slots. Reordering so the small types are together lets them pack.
// BAD: 3 slots
uint128 a; // slot 0
uint256 b; // slot 1
uint128 c; // slot 2
// GOOD: 2 slots
uint128 a; // slot 0
uint128 c; // slot 0
uint256 b; // slot 1Choosing Integer Sizes
Smaller integers only save gas when they pack. A lone uint8 in its own slot is not cheaper than a uint256, and may even cost more because of masking operations.
Rule of thumb: use uint256 by default, and only downsize when you can fit several small values into one slot.
Packing Structs
Structs follow the same packing rules. Order struct members so small types group together. A well-packed struct can cut the number of SSTORE operations dramatically.
struct Order {
uint128 amount; // slot 0
uint64 timestamp;// slot 0
uint64 id; // slot 0
address maker; // slot 1
}Constants and Immutables
Values that never change should not live in storage at all.
constant— inlined into bytecode at compile time, zero storageimmutable— set once in the constructor, stored in bytecode not storage
Both avoid expensive SLOAD operations entirely.
uint256 public constant MAX_SUPPLY = 10000;
address public immutable owner;
constructor() {
owner = msg.sender;
}Mappings and Dynamic Arrays
Mappings and dynamic arrays do not pack. Each entry occupies its own slot computed by hashing the key with the base slot.
You cannot pack across mapping entries, but you can pack the struct stored as a mapping value. Designing the value struct to fit in fewer slots saves gas on every write.
mapping(address => Order) public orders;
// each Order packs into 2 slots regardless of mappingCaching Storage Reads
Reading a storage variable repeatedly inside a function pays for each access. Copy it into a local memory variable once, then use the local.
This is especially valuable inside loops, where each iteration would otherwise re-read storage.
uint256 len = items.length; // cache length once
for (uint256 i = 0; i < len; i++) {
// process items[i]
}Batched Writes
When updating multiple fields of a packed struct, load the struct into memory, modify it, then write it back once. The compiler can combine adjacent fields into a single SSTORE.
Writing fields one at a time may trigger multiple read-modify-write cycles on the same slot.
Order memory o = orders[id];
o.amount = newAmount;
o.timestamp = uint64(block.timestamp);
orders[id] = o; // single packed write of slot 0Zero to Non-Zero Penalty
Initializing a slot from zero to non-zero costs 20000 gas, while updating an already non-zero slot costs only 5000 gas.
This is why some contracts pre-warm slots or use a sentinel value (like 1 instead of 0) to keep slots non-zero, trading a one-time cost for cheaper repeated updates.
// Using 1 as 'false' and 2 as 'true' keeps the slot non-zero
uint256 private locked = 1; // never goes back to 0Layout Audit Checklist
When auditing storage for gas:
- Group small types so they pack into shared slots
- Use
constant/immutablefor fixed values - Cache repeated storage reads in memory
- Batch struct writes
- Avoid unnecessary zero to non-zero transitions
Always verify with a gas report after changes.
Quick Check
You declare a uint128, then a uint256, then another uint128. How many storage slots do they use?
Recap
You learned to optimize storage layout:
- Storage is 32-byte slots; adjacent small types pack together
- Declaration order determines packing
constantandimmutableavoid storage entirely- Cache reads and batch struct writes
- Mind the zero to non-zero write penalty
Next we look at optimizing loops and calldata handling.
คำถามที่พบบ่อย
บทเรียน “การเพิ่มประสิทธิภาพพื้นที่จัดเก็บ” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การเพิ่มประสิทธิภาพพื้นที่จัดเก็บ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Web3 & DApp Development Fundamentals ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Web3 & DApp Development Fundamentals มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การเพิ่มประสิทธิภาพพื้นที่จัดเก็บ”
การแพ็กและสล็อต คุณปฏิบัติ Web3 & DApp Development Fundamentals ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Web3 & DApp Development Fundamentals หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Web3 & DApp Development Fundamentals บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “การเพิ่มประสิทธิภาพพื้นที่จัดเก็บ” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Web3 & DApp Development Fundamentals นี้ได้ไหม
ได้ บทเรียน Web3 & DApp Development Fundamentals ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- แบบจำลองต้นทุนแก๊ส
- การเพิ่มประสิทธิภาพพื้นที่จัดเก็บ
- เทคนิคเกี่ยวกับลูปและ Calldata
- การวัดแก๊ส