خانات التخزين وتحسين استهلاك الغاز
افهم كيفية توزيع Solidity لمتغيرات الحالة على خانات تخزين بحجم 32 بايت، واستخدم هذه المعرفة لكتابة عقود فعّالة في استهلاك الغاز.
خانات التخزين وتحسين استهلاك الغاز درس مجاني في Blockchain Smart Contracts with Solidity على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في 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) وفتح باقي دورة Blockchain Smart Contracts with Solidity، انتقل إلى CoddyKit PRO. تتضمن دورة Blockchain Smart Contracts with Solidity 4 دروس في المجموع.
ماذا ستتعلم في «خانات التخزين وتحسين استهلاك الغاز»؟
افهم كيفية توزيع Solidity لمتغيرات الحالة على خانات تخزين بحجم 32 بايت، واستخدم هذه المعرفة لكتابة عقود فعّالة في استهلاك الغاز. تتمرن على Blockchain Smart Contracts with Solidity مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Blockchain Smart Contracts with Solidity؟
لا تُشترط خبرة سابقة. Blockchain Smart Contracts with Solidity على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «خانات التخزين وتحسين استهلاك الغاز»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Blockchain Smart Contracts with Solidity هذا؟
نعم. كل درس في Blockchain Smart Contracts with Solidity يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- متغيرات الحالة وتخطيط التخزين
- التعيينات والمصفوفات الديناميكية
- الأحداث وتسجيل البيانات
- خانات التخزين وتحسين استهلاك الغاز