OpenZeppelin / OpenZeppelin/openzeppelin-contracts

Redundant SLOADs in Initializable

Open
#4,579 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

gas optimization
Dominant language
Solidity
Stars
27.2k
Forks
12.4k
Avg merge
2d 19h
Merged PRs (30d)
33

Description

Opening issue as mentioned in https://github.com/OpenZeppelin/openzeppelin-contracts/pull/4576#discussion_r1316443220

📝 Details

Since InitializableStorage is a struct with two fields packed in one storage slot, compiler need to load this slot when writing new value only for one of the fields, like here:

https://github.com/OpenZeppelin/openzeppelin-contracts/blob/175b1e9e09b19f90d96b472a0e0b4ebea9a47762/contracts/proxy/utils/Initializable.sol#L127-L130

Since (when) we know values for other fields, explicit usage of known values can let the compiler to prevent excessive SLOADs before SSTORE. Example:

$._initialized = 1;
$._initializing = false;

PS: this is applicable not only in this place, but also in some other contracts.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in contracts/proxy/utils/Initializable.sol, reading InitializableStorage and the writes around lines 127-130, then review the linked pull request discussion for the compiler behavior. Compare the generated storage operations for the known field values and identify other affected contracts. Done means redundant SLOADs are avoided without changing initialization behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
solidity
Domain
blockchain, performance
Issue type
Refactor
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.