Can be Optimized Linked Library Address Bytecode Fragment
- Dominant language
- C++
- Stars
- 25.7k
- Forks
- 6.2k
- Avg merge
- 2d 19h
- Merged PRs (30d)
- 29
Description
---
## Abstract
In Solidity, we can delegatecall public functions of a library to execute external library code, a pattern originally introduced to help contracts stay below the byte-size limit at deployment time. Unfortunately, the current compiler implementation defeats this purpose: on every delegatecall the generated bytecode embeds a redundant `PUSH20 ` instruction—often longer than the library code itself. As a result, in many scenarios the delegatecall pattern simultaneously increases gas consumption **and** bytecode size. This is a problem that the compiler could solve through optimization.
## Motivation
Consider the following example:
```solidity
pragma solidity >=0.7.0 <0.9.0;
library Ballot {
function B() public { }
function C() public { }
}
contract A {
function C() public {
Ballot.B();
}
function D() public {
Ballot.C();
}
}
```
The bytecode of contract `A` is roughly when Optimization is set.
```
60806040..73__$17280f6b09cfc0597fda7b4937e1c059ab$__63c89e43..
73__$17280f6b09cfc0597fda7b4937e1c059ab$__63fe073d1160..
```
Because `A.C()` and `A.D()` each make a library call, we see **two** separate `PUSH20 ` sequences in the bytecode. A feature meant to compress code actually injects sizeable, duplicate address literals every time the library is invoked. Hoisting this address into a shared basic block would eliminate the redundancy.
Most libraries function are far smaller than 20 bytes, yet each external call still emits a full `PUSH20`.
## Proposal
For the library delegatecall scenario, users care primarily about shrinking deployment bytecode, not marginal gas savings. We therefore propose that the compiler, by default, consolidate all identical `PUSH20
```
PUSH20 Address
...
PUSH20 Address
```
TO
```
JUMPDEST
PUSH20 Address
JUMP
...
PUSH
PUSH
JUMP
...
PUSH
PUSH
JUMP
```
Contributor guide
Research direction
The issue names no files, tests, or entry points. Start by locating the compiler's library delegatecall code generation and compare optimized bytecode for multiple calls to the same library. Done means redundant library-address fragments are consolidated without changing behavior, with regression coverage for the generated bytecode.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp, solidity
- Domain
- blockchain, compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100