Gas Optimizations #56
Labels
bug
Something isn't working
G (Gas Optimization)
sponsor confirmed
Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity")
Nested Finance Gas Optimization Report
Unless otherwise noted, manual auditing and testing were done using Visual Studio Code and Remix. The sponsor-provided test suite was used to verify the findings.
The audit was done from February 10-12, 2022 by ye0lde through code4rena.
store
can be more efficient (NestedRecords.sol)unchecked
keyword (NestedFactory.sol)Findings
G-1 - Function
store
can be more efficient (NestedRecords.sol)Impact
Caching the references to
records[_nftId]
in thestore
function will decrease gas usage asstore
is called frequently from NestedFactory.solBelow are the relevant numbers from the sponsor's test suite before and after the change:
Proof of Concept
The
store
function is here:https://github.com/code-423n4/2022-02-nested/blob/fe6f9ef7783c3c84798c8ab5fc58085a55cebcfc/contracts/NestedRecords.sol#L111-L132
Recommended Mitigation Steps
I suggest the following changes:
G-2 - Save gas and retain code clarity with the
unchecked
keyword (NestedFactory.sol)Impact
In a previous Code4rena audit, various "unchecked" optimizations were suggested. Some of which were implemented and some were not because the sponsor's focus was code clarity over optimization.
I believe this suggestion meets the requirements for both optimization and clarity. Below are the relevant numbers from the sponsor's test suite before and after the change:
Proof of Concept
The code that can be
unchecked
is here:https://github.com/code-423n4/2022-02-nested/blob/fe6f9ef7783c3c84798c8ab5fc58085a55cebcfc/contracts/NestedFactory.sol#L339-L347
The "unchecked" keyword can be applied here since there is a
require
statement at #337 that ensures the arithmetic operations would not cause an integer underflow or overflow.Recommended Mitigation Steps
Add
unchecked
around #L339-L347 as shown below.The text was updated successfully, but these errors were encountered: