> For the complete documentation index, see [llms.txt](https://calnix.gitbook.io/eth-dev/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://calnix.gitbook.io/eth-dev/gas-opt/caching-sload-into-mload.md).

# caching sload into mload

## Cache read variables in memory

### Before:

```solidity
function liquidation(address user) external onlyOwner { 
    uint collateralRequired = getCollateralRequired(debts[user]);

    if (collateralRequired > deposits[user]){
        emit Liquidation(address(collateral), address(debt), user, debts[user], deposits[user]); 
        delete deposits[user];
        delete debts[user];
    }
```

Both `debts[user]` & `deposits[user]` are storage variables, given we are looking to pass a value from a mapping that was declared and as a public variable.

* A single SLOAD costs 2100 gas, and is unavoidable the first time.&#x20;
* The second instance, (line 5), we are reloading the SLOAD from cache and it will cost 100 gas.
* Hence the repetitive use of debts\[user] twice will cost us **2200** gas.

{% hint style="info" %}
Ever since [EIP-2929](https://eips.ethereum.org/EIPS/eip-2929) the first `SLOAD` operation costs 2100 gas, but once that memory is read, it is cached and considered considered warm, which has a cost of 100 gas to load again.
{% endhint %}

### After:

To combat that, you could always store the object in memory, and load it from there, which is much cheaper.&#x20;

So what you could do is write from storage to memory once (`SLOAD` + `MSTORE` = 2103 gas), then read the memory variable twice (`MLOAD` + `MLOAD`) = 6 gas.

* `SLOAD` + `MSTORE` = **2103 gas**
* `MLOAD` + `MLOAD` = 6 gas
* Total = **2109** gas

```solidity
function liquidation(address user) external onlyOwner { 
    uint userDebt = debts[user];             //saves an extra SLOAD
    uint userDeposit = deposits[user];       //saves an extra SLOAD

    require(!_isCollateralized(userDebt, userDeposit), "Not undercollateralized");

    delete deposits[user];
    delete debts[user];
    emit Liquidation(address(collateral), address(debt), user, userDebt, userDeposit); 
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://calnix.gitbook.io/eth-dev/gas-opt/caching-sload-into-mload.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
