code-423n4 / 2022-06-putty-findings

5 stars 0 forks source link

Gas Optimizations #364

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

1. ++I COSTS LESS GAS THAN ++I, ESPECIALLY WHEN IT’S USED IN FOR-LOOPS (--I/I-- TOO)

Examples of this issue in the codebase:

  1. File: contracts\src\PuttyV2.sol (Line 627)

  2. File: contracts\src\PuttyV2.sol (Line 637)


2. AVOIDING INITIALIZATION OF LOOP INDEX CAN SAVE A LITTLE GAS

Examples of this issue in the codebase:

  1. File: contracts\src\PuttyV2.sol (Line 647)

  2. File: contracts\src\PuttyV2.sol (Line 658)


3. <ARRAY>.LENGTH SHOULD NOT BE LOOKED UP IN EVERY LOOP OF A FOR-LOOP

Even memory arrays incur the overhead of bit tests and bit shifts to calculate the array length

Examples of this issue in the codebase:

  1. File: contracts\src\PuttyV2.sol (Line 611)

  2. File: contracts\src\PuttyV2.sol (Line 658)


4. EXPRESSIONS FOR CONSTANT VALUES SUCH AS A CALL TO KECCAK256(), SHOULD USE IMMUTABLE RATHER THAN CONSTANT

Examples of this issue in the codebase:

  1. File: contracts\src\PuttyV2.sol (Line 89)

  2. File: contracts\src\PuttyV2.sol (Line 95)


5. USING BOOLS FOR STORAGE INCURS OVERHEAD

refer to this.

Examples of this issue in the codebase:

  1. File: contracts\src\PuttyV2.sol (Line 143)

  2. File: contracts\src\PuttyV2.sol (Line 155)


6. USE CUSTOM ERRORS RATHER THAN REVERT()/REQUIRE() STRINGS TO SAVE DEPLOYMENT GAS

Examples of this issue in the codebase:

  1. File: contracts\src\PuttyV2.sol (Line 329)

  2. File: contracts\src\PuttyV2.sol (Line 405)


7. ++I COSTS LESS GAS THAN ++I, ESPECIALLY WHEN IT’S USED IN FOR-LOOPS (--I/I-- TOO)

Examples of this issue in the codebase:

  1. File: contracts\src\PuttyV2.sol (Line 556)

  2. File: contracts\src\PuttyV2.sol (Line 594)


8. USING > 0 COSTS MORE GAS THAN != 0 WHEN USED ON A UINT IN A REQUIRE() STATEMENT

Examples of this issue in the codebase:

  1. File: contracts\src\PuttyV2.sol (Line 598)

  2. File: contracts\src\PuttyV2.sol (Line 599)


9. USE CallData INSTEAD OF MEMORY

Examples of this issue in the codebase:

  1. File: contracts\src\PuttyV2.sol floorAssetTokenIds can be changed to calldata (Line 271)