---
eip: 3336
label: EIP-3336
title: Paged memory allocation for the EVM
status: Stagnant
type: Standards Track
category: Core
created: 2021-03-06
official: https://eips.ethereum.org/EIPS/eip-3336
source: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-3336.md
canonical: https://eips.huatuo.xyz/eip-3336/
lang: en
tldr: Proposed switching EVM memory from one linearly-priced array to 1 KiB pages paid for only when touched, so compilers could spread data across the address space without paying for the gaps.
tags: [evm, memory, gas, compilers, core]
---

# EIP-3336: Paged memory allocation for the EVM

> Changes the memory model for the EVM to use pagination.

**TL;DR:** Proposed switching EVM memory from one linearly-priced array to 1 KiB pages paid for only when touched, so compilers could spread data across the address space without paying for the gaps.

## What problem does it solve?

EVM memory is a single byte array that starts at address 0. You pay for it up to the **highest** address you have touched, using a cost that grows quadratically:

```
Cmem(a) = 3 * a + floor(a² / 512)      # a = words from 0 to the highest touched word
```

Touching address 1,000,000 means paying for every word below it, even if you never use them. Compilers therefore pack memory tightly and often copy data around to keep it compact. That wastes gas and rules out familiar layouts such as a separate heap and stack in different regions.

Ordinary computers solve this with virtual memory: a huge address space where only the pages actually used are backed by RAM. EIP-3336 proposes a simple version for the EVM.

## How it works

Parameters: `PAGE_BITS = 10` (pages of 1,024 bytes) and `PAGE_BASE_COST = 96`.

- The top 246 bits of an address are the page number; the low 10 bits are the offset within the page.
- A page is allocated (zero-filled) the first time any byte in it is read or written. Clients are encouraged to store pages in a hash map keyed by page number.
- Memory cost depends on the **number of allocated pages** `p`, not the highest address:

```
Cmem'(p) = max(96 * (p - 1) + floor(2 * (p - 1)²), 0)
```

  The first page is free, and the curve is designed to never charge more than the current formula for the same amount of memory.
- `MLOAD`/`MSTORE` stay at 3 gas when the 32-byte word sits in one page, but cost 6 gas if it straddles two pages, to pay for the extra work of splitting it.
- Every other memory-touching opcode (`CALLDATACOPY`, `CODECOPY`, `CALL`, `CREATE`, `MSTORE8`, …) allocates any pages it touches and charges the new expansion cost.

## Sample code

Comparing old and new memory cost for a sparse access pattern (Python):

```python
PAGE_BITS, PAGE_BASE_COST = 10, 96
PAGE_SIZE = 1 << PAGE_BITS

def old_cost(highest_byte: int) -> int:
    a = (highest_byte + 32) // 32          # words from 0 to highest touched word
    return 3 * a + a * a // 512

def new_cost(pages: int) -> int:
    return max(PAGE_BASE_COST * (pages - 1) + 2 * (pages - 1) ** 2, 0)

touched = [0x0000, 0x10_0000]              # two words, ~1 MiB apart
pages = {addr >> PAGE_BITS for addr in touched}
print(old_cost(max(touched)))              # 2,195,587 gas
print(new_cost(len(pages)))                # 98 gas
```

## Things to know

- **Status: Stagnant.** It was never scheduled for a hard fork; EVM memory pricing is still linear-from-zero.
- It was proposed together with [EIP-3337](/eip-3337/) (frame-pointer memory loads and stores) to improve the EVM's memory model.
- Programs doing unaligned 32-byte loads/stores across a page boundary would pay slightly more; the authors expected this to affect few contracts.
- The spec's test cases are listed as TBD.


---

This is an unofficial plain-language summary. Read the full specification: https://eips.ethereum.org/EIPS/eip-3336
