0Pricing
Web3 & DApp Development Fundamentals · درس

التخزين مقابل الذاكرة

موقع البيانات

التخزين مقابل الذاكرة درس مجاني في Web3 & DApp Development Fundamentals على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Web3 & DApp Development Fundamentals، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Web3 & DApp Development Fundamentals 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

Why Data Location Matters

In Solidity, reference types must specify a data location. The three locations are storage, memory, and calldata.

Choosing the right one affects persistence, mutability, and most importantly gas cost.

Storage

Storage is the contract's permanent on-chain state. State variables live here, and writing to storage is the most expensive operation.

Data in storage persists between transactions.

<code>contract Bank {
    uint256 public total; // lives in storage forever

    function add(uint256 v) public {
        total += v; // writes to storage (expensive)
    }
}</code>

Memory

Memory is a temporary scratchpad that exists only during a function call. It is much cheaper than storage and is cleared when the function returns.

Use memory for intermediate calculations and data you do not need to persist.

<code>function build() public pure returns (uint[] memory) {
    uint[] memory temp = new uint[](3);
    temp[0] = 1;
    temp[1] = 2;
    return temp; // discarded after call
}</code>

Calldata

Calldata is a read-only, non-modifiable location holding function arguments for external calls. It is the cheapest option because nothing is copied.

Use it for function parameters you only need to read.

<code>function sum(uint[] calldata values) external pure returns (uint total) {
    for (uint i = 0; i < values.length; i++) {
        total += values[i];
    }
}</code>

Storage Pointer to State

Inside a function, a storage local variable is a pointer to existing state. Modifying it changes the contract's actual storage.

<code>struct User { uint balance; }
mapping(address => User) users;

function credit() public {
    User storage u = users[msg.sender];
    u.balance += 100; // updates real state
}</code>

Memory Copy of State

If you instead load state into a memory variable, you get an independent copy. Changes to the copy do NOT update the contract's storage.

This is a common source of bugs for beginners.

<code>function noEffect() public view returns (uint) {
    User memory u = users[msg.sender];
    u.balance += 100; // only the copy changes
    return u.balance;  // storage is untouched
}</code>

Gas Cost Comparison

The cost ranking, from cheapest to most expensive:

  • calldata - read-only external args, no copy
  • memory - temporary, copied into RAM
  • storage - persistent on-chain, very costly to write

A fresh storage write (zero to non-zero) is one of the priciest EVM operations.

Prefer calldata for external

For external functions whose array or struct parameters are only read, use calldata instead of memory to avoid an unnecessary copy and save gas.

<code>// Cheaper: no copy is made
function process(bytes calldata data) external {
    // read data directly
}</code>

Where Defaults Apply

State variables are always storage. Function parameters and locals of reference type must declare a location explicitly. Value types and mapping members default to storage rules automatically.

Local mappings must always be storage - they cannot live in memory.

<code>// mapping locals must be storage
function f() public {
    // mapping(...) memory m;  // ERROR
}</code>

Caching Storage Reads

A gas optimization: if you read the same storage variable multiple times, copy it into a memory local once. Each storage read (SLOAD) costs gas.

<code>function loop() public view returns (uint) {
    uint cached = total; // one SLOAD
    uint acc;
    for (uint i = 0; i < 10; i++) {
        acc += cached; // reads memory, cheap
    }
    return acc;
}</code>

Returning Memory

Functions that build and return new arrays or structs use memory for the return value, since storage cannot be returned by value and calldata is input-only.

<code>function pair(uint a, uint b) public pure returns (uint[] memory) {
    uint[] memory out = new uint[](2);
    out[0] = a;
    out[1] = b;
    return out;
}</code>

Quick Check

Test your understanding of data locations.

Recap

You learned how Solidity data locations work:

  • storage - persistent, on-chain, most expensive to write
  • memory - temporary scratchpad, copied, cleared after the call
  • calldata - read-only external args, cheapest, no copy

Use storage references to mutate state, memory for temporary work, and calldata for read-only external inputs to save gas.

الأسئلة الشائعة

هل درس «التخزين مقابل الذاكرة» مجاني؟

نعم — نص درس «التخزين مقابل الذاكرة» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Web3 & DApp Development Fundamentals، انتقل إلى CoddyKit PRO. تتضمن دورة Web3 & DApp Development Fundamentals 4 دروس في المجموع.

ماذا ستتعلم في «التخزين مقابل الذاكرة»؟

موقع البيانات تتمرن على Web3 & DApp Development Fundamentals مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Web3 & DApp Development Fundamentals؟

لا تُشترط خبرة سابقة. Web3 & DApp Development Fundamentals على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.

كم من الوقت يستغرق درس «التخزين مقابل الذاكرة»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Web3 & DApp Development Fundamentals هذا؟

نعم. كل درس في Web3 & DApp Development Fundamentals يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. أنواع القيم
  2. الأنواع المرجعية
  3. التخزين مقابل الذاكرة
  4. الثوابت والقيم غير القابلة للتغيير
← العودة إلى Web3 & DApp Development Fundamentals