Depolama Yuvaları ve Gas Optimizasyonu
Solidity'nin durum değişkenlerini 32 baytlık depolama yuvalarına nasıl yerleştirdiğini anlayın ve bu bilgiyi gas açısından verimli sözleşmeler yazmak için kullanın.
Depolama Yuvaları ve Gas Optimizasyonu, CoddyKit'te ücretsiz bir Blockchain Smart Contracts with Solidity dersidir. Bu, 4 dersinin 4. dersidir. Aşağıdan dersin tamamını ücretsiz okuyabilir, sonra tarayıcıda yerleşik kod editörü ve 7/24 yapay zeka koçu ile uygulamalı olarak pratik yapabilirsin. Bu, Blockchain Smart Contracts with Solidity öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. Blockchain Smart Contracts with Solidity kursu toplamda 4 dersten oluşur.
Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.
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.
Sıkça Sorulan Sorular
“Depolama Yuvaları ve Gas Optimizasyonu” dersi ücretsiz mi?
Evet — “Depolama Yuvaları ve Gas Optimizasyonu” dersin tüm metni burada web'de ücretsiz olarak okunabilir. Etkileşimli olarak pratik yapmak (yerleşik kod editörü ve 7/24 yapay zeka koçu) ve Blockchain Smart Contracts with Solidity kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. Blockchain Smart Contracts with Solidity kursu toplamda 4 dersten oluşur.
“Depolama Yuvaları ve Gas Optimizasyonu” dersinde ne öğreneceğim?
Solidity'nin durum değişkenlerini 32 baytlık depolama yuvalarına nasıl yerleştirdiğini anlayın ve bu bilgiyi gas açısından verimli sözleşmeler yazmak için kullanın. Blockchain Smart Contracts with Solidity ile uygulamalı kodu tarayıcıda doğrudan çalıştırarak pratik yaparsın ve 7/24 yapay zeka koçu dersi çalışırken sorularını yanıtlar.
Blockchain Smart Contracts with Solidity öğrenmeye başlamak için deneyim gerekli mi?
Önceden deneyim gerekmez. CoddyKit'te Blockchain Smart Contracts with Solidity, başlangıçtan ileri seviyeye kadar yapılandırıldığı için buradan başlayabilir veya başından başlayıp kendi hızında ilerleme yapabilirsin. Bu, 4 dersinin 4. dersidir.
“Depolama Yuvaları ve Gas Optimizasyonu” dersi ne kadar sürer?
Çoğu CoddyKit dersi yaklaşık 5–10 dakika sürer. Her biri kısa ve etkileşimli olduğu için sabit ilerleme yaparsın ve web ile uygulama arasında tam olarak bıraktığın yerden devam edebilirsin.
Bu Blockchain Smart Contracts with Solidity dersinde kod yazıp çalıştırabilir miyim?
Evet. Her Blockchain Smart Contracts with Solidity dersi yerleşik bir kod editörü içerir, bu sayede tarayıcıda gerçek kod yazıp çalıştırabilir ve anlık yapay zeka geri bildirimi alırsın — yerel kurulum gerekli değildir.
Bu kursun tüm dersleri
- Durum Değişkenleri ve Depolama Düzeni
- Eşlemeler ve Dinamik Diziler
- Olaylar ve Günlük Verileri
- Depolama Yuvaları ve Gas Optimizasyonu