code-423n4 / 2022-10-zksync-findings

3 stars 0 forks source link

Gas Optimizations #347

Closed code423n4 closed 1 year ago

code423n4 commented 1 year ago

**G-01 Always use pre-computed constant values**

Defining constants as an expression like this uint256 constant *INITIAL_STORAGE_CHANGES_COMMITMENT_BYTES* **=** *4* **+** *64* ***** *4896*; results in the expression being executed every time the constant is read, instead of caching the value. Always pre-compute the value and just add an explaining comment.

****G-02 Replace all require statements with custom errors**

Solidity now supports custom errors that cost much less gas than require statements with revert strings. Replace all require statements with custom errors

****G-03 Pack struct variables in 256 byte slots always****

Every time you define a struct it is best to define it’s fields in a sequence so that they fill 256 byte slots - this will save gas on reads from storage.

****G-04 Always compare uint with != 0 instead of > 0**

Comparing uint values with != 0 is cheaper than > 0

****G-05 Indexed params are cheaper on events, always use them****

Make sure all events use the maximum of 3 indexed parameters to save gas

****G-06 ++i/i++ should be unchecked{++i}/unchecked{i++} when it is not possible for them to overflow, as is the case when used in forand whileloops**

All for loops in Diamond.sol use ++i and it can be wrapped in an unchecked block to save gas

****G-07 Use ++var instead of var += 1**

The code is s.diamondCutStorage.currentProposalId **+=** *1*; change it to ++s.diamondCutStorage.currentProposalId; to save gas.

**G-08 Using a storage pointer instead of memory for structs when doing only read-only operations is cheaper**

The structure reference in the fallback function in DiamondProxy can be of type storage to save gas.

GalloDaSballo commented 1 year ago

G-01 Always use pre-computed constant values

Not anymore https://twitter.com/GalloDaSballo/status/1543729080926871557

G-03 Pack struct variables in 256 byte slots always

Skipping in lack of suggested refactoring / benchmarks

**G-06 ++i/i++ should be unchecked{++i}/unchecked{i++} when it is not possible for them to overflow, as is the case when used in forand whileloops

200

**G-07 Use ++var instead of var += 1

5

****G-08 Using a storage pointer instead of memory for structs when doing only read-only operations is cheaper

100 gas

305

c4-judge commented 1 year ago

GalloDaSballo marked the issue as grade-c