# Cold access VS Warm access - Gas Cost Question

### Questions

1. Does cold access means simply accessing storage?
2. Does hot access means simply accessing memory?
3. The total gass cost difference between the 2 examples are in the `NonCaching` example: `51,209 - 49,657 = 1,552` gas. If each cold access cost 1200 gas, how can it be right if the first example access the cold storage every loop and it cost 1200 gas each read? The diff should be `1200*10` (`myArray.length` 10 times) + `1200*10` (`myArray[i]` 10 times) = `1200*20`.

Those articles didn't use a good example to explain the situation, that's why you are confused.

Cold/warm access refers always to storage, never to memory. You have cold access the first time you read a variable, warm access when you read it again.

The only difference between the methods `Caching` and `NotCaching` is that `myArray.length` is read 10 times in `Caching` from storage, while in `NotCaching` it's from memory (so even cheaper than warm storage). In both cases the elements of `myArray` are read each once (so cold access in both). The difference in gas should be then around 10*100 = 1000. The extra 500 is probably due to multiple `keccak256` calculations of the state slots in the loop.

You can write better examples on remix, for example I made this:

``````contract ColdWarmGas {
uint256 x = 1;  // storage variable
uint256 y = 2;  // storage variable

// 2381 gas
function test1() external view returns (uint256 a) {
a = x;      // cold read
a = a + x;  // warm read
}

// 4473 gas
function test2() external view returns (uint256 a) {
a = x;      // cold read
a = a + y;  // cold read
}

// 4632 gas
function test3() external view returns (uint256 a) {
a = x;      // cold read
a = a + y;  // cold read
a = a + y;  // warm read
}
}
``````

You can see that adding a warm read adds around 100 gas, while a cold one over 1000.

• Thank you very much! In general when mapping storage array to memory array, should I make sure that the array is small sized or else the transaction will revert? What about copying maps from storage to memory? Apr 30, 2023 at 14:21
• The general rule is that you should copy to memory only the parts you're going to read multiple times. I wouldn't blindly copy a big array to memory if I only need a part of it for example. Regarding the mapping, unfortunately it's not possible to copy it all to memory, so you need to look at every element first. Apr 30, 2023 at 15:43
• Thank you again, is this possible to clone only part of an array? What did you mean by that? Apr 30, 2023 at 16:01
• Yes you can slice a storage array and copy it to memory (you need some inline assembly though iirc). For example, if you have a function that needs only the first half of the array, there's no need to copy all of it. Apr 30, 2023 at 22:46

Does cold access means simply accessing storage?

No

Does hot access means simply accessing memory?

No.

It's not possible to know how an individual node software uses disk and memory. However, in any computer science, CPU design since the late 80s, and such, recently used values are cached and thus cheaper to access. This is the theory why hot access should be given cheaper gas cost, because it will likely to consume less wall clock time and electricity.

• Can you pls explain more what am I wrong about, what is a hot and cold access and what the article tried to explain? Apr 30, 2023 at 11:51
• I am not interested to verify someone else's calculation - there are calculators for that. Also I am not interested to read any random articles on Internet. If you are unsure just go to read GoEthereum source code how it works. Apr 30, 2023 at 12:27