Memory and Storage in Yul
Tags: yul, ethereum, blockchain, web3
Published: 2023-04-12 16:52:51

>A contract's memory is cleared after execution, but its storage remains until explicitly removed

Memory and storage are two essential components of any computer system, including Yul. Yul is an assembly language used for smart contract development on the Ethereum blockchain.

Memory in Yul is a temporary storage area used to store data during the execution of a program. It is volatile, meaning that its contents are lost when the program terminates. Memory is used to store variables, arrays, and other data structures. Solidity, the high-level language used to write smart contracts, makes it easier for developers to work with memory by automatically allocating and deallocating memory as needed. Storage, on the other hand, is a permanent storage area used to store data that needs to persist even after the program terminates.

Memory variables are local variables that are allocated to the memory. They are declared with the let keyword and can be assigned with the := operator. Memory variables have a fixed size of 256 bits and are zero-initialized. For example:

```solidity
let x := 42 // declare and assign a memory variable
let y // declare a memory variable without assignment
y := add(x, 1) // assign a memory variable with an expression
```

Storage variables are global variables that are allocated on the storage. They are declared with the var keyword and can be assigned with the = operator. Storage variables have a variable size and are not initialized. For example:

```solidity
var z // declare a storage variable
z = mul(x, y) // assign a storage variable with an expression
```

Memory variables and storage variables have different trade-offs in terms of gas costs and performance. Memory variables are cheaper to read and write, but more expensive to allocate. Storage variables are cheaper to allocate, but more expensive to read and write. Therefore, it is important to choose the appropriate type of variable for each use case.

One way to optimize gas costs is to use memory variables for temporary or intermediate values, and use storage variables for persistent or shared values. Another way is to use built-in functions such as mload, mstore, sload, and sstore to directly manipulate memory or storage locations without using variables. For example:

```solidity
// store the value 42 at memory location 0x80
mstore(0x80, 42)
// load the value from memory location 0x80
let x := mload(0x80)
// store the value x at storage location 0x40
sstore(0x40, x)
// load the value from storage location 0x40
let y := sload(0x40)
```

However, using these functions requires more care and attention, as they can introduce errors or vulnerabilities if used incorrectly. For example, using an invalid or out-of-bounds memory or storage location can cause unexpected behavior or revert the transaction.

Storage is non-volatile, meaning that its contents are retained even when the power is turned off. In Yul, storage is used to store variables, arrays, and other data structures that need to be accessed across multiple function calls. When working with Yul, it is important to understand the difference between memory and storage and how to use them effectively. Here are some tips to keep in mind:

* Use memory for temporary data that does not need to persist beyond the current function call.
    
* Use storage for data that needs to persist across multiple function calls.
    
* Be mindful of the gas costs associated with accessing memory and storage. Accessing storage is more expensive than accessing memory, so try to minimize the number of storage reads and writes in your code.
    
* Use data structures like arrays and mappings to organize your data in memory and storage.
    

In conclusion, memory and storage are two essential components of any computer system, including Yul. Understanding how to use them effectively is crucial for writing efficient and effective smart contracts. By following the tips outlined in this blog, you can optimize your Yul code for maximum performance and cost-effectiveness.

For more content, follow me at - [https://linktr.ee/shlokkumar2303](https://linktr.ee/shlokkumar2303)