-
-
owned this note
-
Published
Linked with GitHub
# Code-chunk-cost Analysis
**Author(s):** Hugo De la cruz, Ipsilon Team (Previously Ewasm)
[TOC]
## Introduction
In [a document posted by Vitalik](https://notes.ethereum.org/@vbuterin/code_chunk_gas_cost), it is proposed to add a gas cost for accessing code chunks, the document proposes a cold cost of `350` gas for accessing a chunk, where each chunk is `31` bytes long.
(By "cold" it is meant that, similarly to EIP-2930, a pre-payment is possible for a reduced cost.)
In this document, this additional cost for accessing chunks is analyzed using two different approaches:
1. [Chunk cost analysis at block level](#Block-Level): Using the infrastructure developed for [Code Merkleization](https://blog.ethereum.org/2021/04/26/ef-supported-teams-research-and-development-update-2021-pt-1/#code-merkleization). (See also [EIP-2926](https://eips.ethereum.org/EIPS/eip-2926))
The geth node was modified to also log the number of chunks accessed in a block execution along with the information about proof sizes. In this analysis the code is split into chunks of 32 bytes.
2. [Chunk cost analysis at transaction level](#Transaction-Level): Using a custom script to look for latest blocks and find transactions calling the most gas consuming contracts (list based on [ethgasstation](https://ethgasstation.info/gasguzzlers.php)), tracing the transactions to find the executed opcodes, and calculate to which chunk the opcode corresponds, also considering 32-byte chunks.
- 1Inch
- Aave
- Metamask Swap
- Uniswap
- USDT
- USDC
## Results
### Block-level Results
73739 blocks were analyzed, from block `9826938` to `9903677`.
Chunks' cost analysis is presented considering two possible costs: `350` and `330`.
#### Gas Usage
| | |
|----------------|----------|
|Average gas used| 8,905,782|
|Minimum gas used| 13,972|
|Maximum gas used| 9,999,999|
#### Accessed Code Chunks
| | |
|-----------------------|----------|
|Average accessed chunks| 3304|
|Minimum accessed chunks| 6|
|Maximum accessed chunks| 10315|
#### Chunks' access cost
| | 350 gas/chunk | 330 gas/chunk |
|--------|--------------:|--------------:|
|Average | 1156499.91 | 1090414.20 |
|Min | 2100.00 | 1980.00 |
|Max | 3610250.00 | 3403950.00 |
#### Block Gas consumed by chunks access
The following chart shows how much gas would be used by chunk cost access charging `350` or `330` gas per accessed chunk (in % relative to the total block gas):
![](https://i.imgur.com/avreEyJ.png)
![](https://i.imgur.com/JkvsCoc.png)
Here is a much smaller sample with only 200 blocks:
![](https://i.imgur.com/ZMu3syl.png)
![](https://i.imgur.com/8hUfS2c.png)
Indicating the average gas used by chunks' cost is about 20% of block gas. Which we confirm by analyzing the data in more detail:
**350 gas/chunk**:
| % Gas utilized by chunks cost|Blocks| % blocks|
|---------------|---------------|------------|
| 0-9% | 25,608 | 34.72% |
| 10-19% | 46,034 | 62.42% |
| 20-29% | 1,913 | 2.59% |
| 30-39% | 175 | 0.23% |
| 40-49% | 8 | 0.01% |
| 50-59% | 1 | 0.0013% |
| 60-100% | 0 | 0.00% |
**330 gas/chunk**:
| % Gas utilized by chunks cost|Blocks| % blocks|
|---------------|---------------|------------|
| 0-9% | 30006 | 40.69%
| 10-19% | 42358 | 57.44%
| 20-29% | 1256 | 1.70%
| 30-39% | 115 | 0.16%
| 40-49% | 4 | 0.01%
| 50-59% | 0 | 0.00%
| 60-100%| 0 | 0.00%
![](https://i.imgur.com/vz2zQsr.png)
We can see the percentage of gas used by chunks cost is between 0-19% of the total gas used (Block gas used + Chunks gas used):
![](https://i.imgur.com/UodhH9h.png)
----
### Transaction-level Results
For transaction-level analysis the traced transactions were selected from most recent blocks, starting from block `12444060`.
Based on the EIP-170 codesize limit a single contract can have at most 768 32-byte chunks. Notice that we get seemingly very large number of chunks, which is possible in the case multiple contracts are accessed in a single transaction. The numbers indicate that on average multiple contracts are touched. We have manually spot-checked a number of cases to verify this.
#### 1Inch
Total analyzed transactions: `15,858`
Comparing the increase respecting the gas usage in a transaction we get the following information:
In average the individual transaction gas cost is increased by about 80%, and the maximum up to 185%, according to the following table:
| | 350 gas/chunk| 330 gas/chunk|
|----|----------------:|-------------:|
|mean| 82.65855 %| 77.93520% |
|min | 1.66667 %| 1.57143% |
|max | 185.79620 %| 175.17927%
Grouping the transactions by the increase percentage we get the following information:
|Increase %| # Txs (350) | Txs (330)|
|----------|---------------:|---------:|
|0-49% | 347 | 514 |
|50-74% | 4802 | 6450 |
|75-99% | 8166 | 7141 |
|100-149% | 2450 | 1705 |
|150+% | 92 | 48 |
![](https://i.imgur.com/sQXTv8v.png)
We can see the majority of the transactions gets its gas usage increased by between 50 and 99%.
**These results are according to the % increased in relation to the transaction gas usage.**
We can also view the resulting data according to the total number of chunks accessed by each transaction:
| | Touched chunks| Cost (350 gas/chunk) | Cost (330 gas/chunk)|
|-----|--------------:|------------------:|----:|
|mean | 642.29 | 224804.43 | 211958.47 |
|min | 1 | 350 | 330 |
|max | 6205 | 2171750 | 2047650 |
If we group the information we get that the majority of transactions are touching between 0 and 999 chunks:
|Touched chunks| Chunks cost (350) | Chunks cost (330) | # Transactions |
|--------------|------------------:|------------------:|---------------:|
| 0-499 | 0-174650 | 0-164670 | 7787 |
| 500-999 | 175000-349650 | 165000-329670 | 5307 |
| 1000-1999 | 350000-699650 | 330000-659670 | 2422 |
| 2000-2999 | 700000-1049650 | 660000-989670 | 283 |
| 3000+ | 1050000+ | 990000+ | 58 |
![](https://i.imgur.com/T87vztZ.png)
#### AAVE
Total analyzed transactions: `3688`
| | 350 gas/chunk | 330 gas/chunk|
|------|--------------:|-------------:|
| Mean | 75.37295% | 71.06592% |
| Min | 14.34483% | 13.52513% |
| Max | 105.86312% | 99.81380% |
**Data by increase percentage**
Grouping the transactions by the increase percentage we get the following information:
| Increase % | # Txs (350) | # Txs (330)|
|------------|------------:|-----------:|
| 0-49% | 71 | 92 |
| 50-74% | 1584 | 2255 |
| 75-99% | 1993 | 1340 |
| 100-149% | 40 | 1 |
| 150+% | 0 | 0 |
![](https://i.imgur.com/6wtO0v8.png)
The majority of transactions have an increased cost from 50 to 99%.
**Touched chunks analysis**
Data according to the total number of chunks accessed by each transaction:
| | Touched chunks | Cost (350 gas/chunk) | Cost 330 (gas/chunk)|
|----|---------------:|---------------------:|--------------------:|
|Mean| 736.83 | 257889.64 | 243153.09 |
|Min | 55.00 | 19250.00 | 18150.00 |
|Max | 3471.00 | 1214850.00 | 1145430.00 |
Information grouped in number of touched chunks:
|Touched chunks| Chunks cost (350) | Chunks cost (330) | # Transactions |
|--------------|------------------:|------------------:|---------------:|
| 0-499 | 0-174650 | 0-164670 | 852 |
| 500-999 | 175000-349650 | 165000-329670 | 2284 |
| 1000-1999 | 350000-699650 | 330000-659670 | 501 |
| 2000-2999 | 700000-1049650 | 660000-989670 | 47 |
| 3000+ | 1050000+ | 990000+ | 4 |
![](https://i.imgur.com/O47OlR4.png)
#### Metamask Swap
Total analyzed transactions: `27127`
**Data by percentage increase**
| | 350 gas/chunk | 330 gas/chunk|
|------|--------------:|-------------:|
| Mean | 73.08351% | 68.90731% |
| Min | 0.00000% | 0.00000% |
| Max | 131.26183% | 123.76115% |
Grouping the transactions by the increase percentage we get the following information:
| Increase % | # Txs (350) | # Txs (330)|
|------------|------------:|-----------:|
| 0-49% | 1025 | 1668 |
| 50-74% | 14117 | 17147 |
| 75-99% | 10896 | 7862 |
| 100-149% | 1088 | 449 |
| 150+% | 0 | 0 |
![](https://i.imgur.com/tVWMOwy.png)
The majority of transactions gets the gas usage increased by 50 to 74% and 75 to 99%.
**Touched chunks analysis**
Data according to the total number of chunks accessed by each transaction:
| | Touched chunks | Cost (350 gas/chunk) | Cost 330 (gas/chunk)|
|----|---------------:|---------------------:|--------------------:|
|Mean| 470.73 | 164755.14 | 155340.56 |
|Min | 0.00 | 0.00 | 0.00 |
|Max | 5096.00 | 1783600.00 | 1681680.00 |
Information grouped in number of touched chunks:
|Touched chunks| Chunks cost (350) | Chunks cost (330) | # Transactions |
|--------------|------------------:|------------------:|---------------:|
| 0-499 | 0-174650 | 0-164670 | 19874 |
| 500-999 | 175000-349650 | 165000-329670 | 5854 |
| 1000-1999 | 350000-699650 | 330000-659670 | 1240 |
| 2000-2999 | 700000-1049650 | 660000-989670 | 147 |
| 3000+ | 1050000+ | 990000+ | 11 |
![](https://i.imgur.com/pwG1UuU.png)
#### USDT
Total analyzed transactions: `356319`
| | 350 gas/chunk | 330 gas/chunk|
|------|--------------:|-------------:|
| Mean | 33.34170% | 31.43646% |
| Min | 0.07361% | 0.06941% |
| Max | 66.35970% | 62.56771% |
**Data by percentage increase**
Grouping the transactions by the increase percentage we get the following information:
| Increase % | # Txs (350) | # Txs (330)|
|------------|------------:|-----------:|
| 0-19% | 14293 | 14297 |
| 20-29% | 117244 | 129775 |
| 30-39% | 110155 | 97635 |
| 40-49% | 114625 | 114610 |
| 50+% | 2 | 2 |
![](https://i.imgur.com/TcVaM3W.png)
The majority of transactions have an increased cost of less than 50%.
**Touched chunks analysis**
Data according to the total number of chunks accessed by each transaction:
| | Touched chunks | Cost (350 gas/chunk) | Cost 330 (gas/chunk)|
|----|---------------:|---------------------:|--------------------:|
|Mean| 41.32 | 14462.01 | 13635.61 |
|Min | 1.00 | 350.00 | 330.00 |
|Max | 49.00 | 17150.00 | 16170.00 |
Information grouped in number of touched chunks:
| Touched chunks | Chunks cost (350) | Chunks cost (330) | # Transactions |
|----------------|-------------------|-------------------|----------------|
| 0-29 | 0-10150 | 0-9570 | 13333 |
| 30-39 | 10500-13650 | 9900-12870 | 2545 |
| 40-49 | 1400-17150 | 13200-16170 | 340441 |
| 50+ | 17500+ | 16500+ | 0 |
![](https://i.imgur.com/hlkLVRp.png)
#### USDC
Total analyzed transactions: `104694`
**Data by percentage increase**
| | 350 gas/chunk | 330 gas/chunk|
|------|--------------:|-------------:|
| Mean | 37.06146% | 34.94366% |
| Min | 1.66667% | 1.57143% |
| Max | 64.51384% | 60.82734% |
Grouping the transactions by the increase percentage we get the following information:
| Increase % | # Txs (350) | # Txs (330)|
|------------|------------:|-----------:|
| 0-19% | 10 | 11 |
| 20-29% | 52827 | 53046 |
| 30-39% | 23633 | 23433 |
| 40-49% | 347 | 480 |
| 50+% | 27877 | 27724 |
![](https://i.imgur.com/MenYNpi.png)
The majority of transactions have an increased cost less than the 60%, more than half of transaction have an increased cost of less than 30%.
**Touched chunks analysis**
Data according to the total number of chunks accessed by each transaction:
| | Touched chunks | Cost (350 gas/chunk) | Cost 330 (gas/chunk)|
|----|---------------:|---------------------:|--------------------:|
|Mean| 51.71 | 18097.19 | 17063.07 |
|Min | 1.00 | 350.00 | 330.00 |
|Max | 68.00 | 23800.00 | 22440.00 |
Information grouped in number of touched chunks:
| Touched chunks | Chunks cost (350) | Chunks cost (330) | # Transactions |
|----------------|-------------------|-------------------|----------------|
| 0-29 | 0-10150 | 0-9570 | 36 |
| 30-39 | 10500-13650 | 9900-12870 | 516 |
| 40-49 | 1400-17150 | 13200-16170 | 10912 |
| 50-59 | 17500-20650 | 16500-19470 | 91134 |
| 60-69 | 21000-24150 | 19800-22770 | 2096 |
| 70+ | 24500+ | 23100+ | 0 |
![](https://i.imgur.com/tdLuI3o.png)
#### Uniswap
Total analyzed transactions: `345535`
**Data by percentage increase**
| | 350 gas/chunk | 330 gas/chunk|
|------|--------------:|-------------:|
| Mean | 85.40685% | 80.52646% |
| Min | 0.40000% | 0.37714% |
| Max | 146.89853% | 138.50433% |
Grouping the transactions by the increase percentage we get the following information:
| Increase % | # Txs (350) | # Txs (330)|
|------------|------------:|-----------:|
| 0-49% | 20029 | 20941 |
| 50-74% | 39393 | 52173 |
| 75-99% | 222265 | 243678 |
| 100-149% | 63848 | 28743 |
| 150+% | 0 | 0 |
![](https://i.imgur.com/Qs66Ijd.png)
We can see the majority of transactions get their gas usage increased between 75 and 99%.
**Touched chunks analysis**
Data according to the total number of chunks accessed by each transaction:
| | Touched chunks | Cost (350 gas/chunk) | Cost 330 (gas/chunk)|
|----|---------------:|---------------------:|--------------------:|
|Mean| 323.78 | 113323.99 | 106848.34 |
|Min | 1.00 | 350.00 | 330.00 |
|Max | 1111.00 | 388850.00 | 366630.00 |
Information grouped in number of touched chunks:
| Touched chunks | Chunks cost (350) | Chunks cost (330) | # Transactions |
|----------------|-------------------|-------------------|----------------|
| 0-99 | 0-34650 | 0-32670 | 45815 |
| 100-199 | 35000-69650 | 33000-12870 | 3263 |
| 200-299 | 70000-104650 | 66000-16170 | 55448 |
| 300-399 | 105000-139650 | 99000-19470 | 161617 |
| 400-499 | 140000-174650 | 132000-22770 | 58251 |
| 500-599 | 175000-209650 | 165000-197670 | 18085 |
| 600-699 | 210000-244650 | 198000-230670 | 2753 |
| 700+ | 245000-279650 | 231000+ | 303 |
![](https://i.imgur.com/cjPXn2r.png)
## Conclusion and Further Work
This initial analysis was conducted to provide a baseline for discussions on chunking costs. We think the average overhead of 20% on existing contracts is significant enough to have an effect on users. We are not judging the merits of it, but likely good reasons would be demanded during the acceptance process. Similarly to EIP-2929/2930, a potential way to significantly reduce the new overhead could be welcome by users. We think the discounted cost of 330 proposed in the document may not be significant enough to fulfil that role.
For further work we suggest to conduct more analysis with 31-byte chunks (and comparing it against the 32-byte data here); and analysis with other per-chunk-costs.