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

2 stars 0 forks source link

Gas Optimizations #190

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

Disclaimer

Table of contents

[GQ-01] Use calldataload for read-only arguments in functions

Description:

All occurances:

Description:

All occurances:

[GQ-03] Consider a = a + b instead of a += b, in case of a being state variable

Description:

All occurances:

Description:

All occurances:

All occurances:

Description:

All occurances:

[GQ-07] Add `require()/reverts before some computations

Description:

All occurances:

[GQ-08] Cache only the needless field of any heavy object instead of caching all attributes

Description:

All occurances:

[GQ-09] Use private/internal for public const/immutable/state variables

Description:

All occurances:

[GQ-10] Cache state variables into a memory to avoid extra warm-accessed sload

Description:

All occurances:

Description:

All occurances:

[G-12] Try ++i/--i instead of i++/i--

Description:

All occurances:

[G-13] Try unchecked{++i}; instead of i++;/++i; in loops

Description:

All occurances:

[G-14] Consider marking onlyOwner functions as payable

Description:

All occurances:

[G-15] Use the recent version of solidity

Description:

All occurances:

[G-16] Possible names optimization

Description:

All occurances:

Description:

All occurances:

[G-18] Use const values instead of computing type(uint256).max every time

Description:

All occurances:

[G-19] Avoid memory expansions

Description:

All occurances:

Optimization Table

GQ-xx instances Saved intrinsic gas per inst Saved deployment gas per inst Overall deployment gas saved Loss in readability?
GQ-01 47 ~20-25k ~50k ~2,5bil not at all
GQ-02 2 ~200 ~15k ~30k not at all
GQ-03 5 ~30 ~2k ~10k not at all
GQ-04 8 N/A N/A N/A low
GQ-05 4 ~200 ~34k ~130k not at all
GQ-06 1 0 ~22 ~22 not at all
GQ-07 2 N/A N/A N/A not at all
GQ-08 6 ~6-10k ~20k ~120k not at all
GQ-09 3 N/A ~40k ~120k not at all
GQ-10 1 ~5k ~10k ~10k not at all
GQ-11 1 ~15k N/A N/A not at all
G-12 3 ~120 ~800 ~2,5k not at all
G-13 3 ~6k ~30k ~90k low
G-14 16 ~50 ~4k ~64k medium
G-15 N/A N/A N/A N/A not at all
G-16 N/A ~1.5k 0 0 high
G-17 13 ~50 ~2k ~30k low
G-18 68 0 ~12k ~816k medium
G-19 N/A N/A N/A N/A not at all

Conclusion

GalloDaSballo commented 1 year ago

[GQ-01] Use calldataload for read-only arguments in functions

1k

Cache state variables into a memory to avoid extra warm-accessed sload

100

[G-15] Use the recent version of solidity

1k

Rest looks minor, really thorough report, well done!

2100

c4-judge commented 1 year ago

GalloDaSballo marked the issue as grade-b