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

5 stars 0 forks source link

Gas Optimizations #344

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

Using external instead of public will save gas.

https://github.com/code-423n4/2022-06-putty/blob/main/contracts/test/differential/EIP712.t.sol#L17 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/test/differential/EIP712.t.sol#L37 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/test/differential/EIP712.t.sol#L55 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/test/differential/EIP712.t.sol#L75 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/test/differential/EIP712.t.sol#L92 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/test/unit/Admin.t.sol#L15 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/test/unit/Admin.t.sol#L22 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/test/unit/Admin.t.sol#L29 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/test/unit/Admin.t.sol#L40 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/test/unit/Admin.t.sol#L51 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/test/unit/Admin.t.sol#L60 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/test/unit/Admin.t.sol#L67 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/test/unit/Cancel.t.sol#L16 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/test/unit/Cancel.t.sol#L47 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/test/unit/Cancel.t.sol#L35 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/test/unit/Exercise.t.sol#L21 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/test/unit/Exercise.t.sol#L31 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/test/unit/Exercise.t.sol#L43 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/test/unit/Exercise.t.sol#L52 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/test/unit/Exercise.t.sol#L65 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/test/unit/Exercise.t.sol#L323 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/test/unit/Exercise.t.sol#L311 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/test/unit/Exercise.t.sol#L292 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/test/unit/Nft.t.sol#L15 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/test/unit/Nft.t.sol#L25 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/test/unit/Nft.t.sol#L30 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/test/unit/Withdraw.t.sol#L51

++i cost less gas than i++

https://github.com/code-423n4/2022-06-putty/blob/main/contracts/test/differential/EIP712.t.sol#L101 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/test/integration/Integration.t.sol#L44 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/test/integration/Integration.t.sol#L52 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/test/integration/Integration.t.sol#L58 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/test/integration/Integration.t.sol#L69 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/test/integration/Integration.t.sol#L104 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/test/integration/Integration.t.sol#L112 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/test/integration/Integration.t.sol#L118 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/test/integration/Integration.t.sol#L126

Use default for value instead of 0,flase,ect…This will save gas.

https://github.com/code-423n4/2022-06-putty/blob/main/contracts/test/differential/EIP712.t.sol#L101 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/test/integration/Integration.t.sol#L44 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/test/integration/Integration.t.sol#L52 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/test/integration/Integration.t.sol#L58 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/test/integration/Integration.t.sol#L112 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/test/integration/Integration.t.sol#L69 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/test/integration/Integration.t.sol#L104 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/test/integration/Integration.t.sol#L118 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/test/integration/Integration.t.sol#L138 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/test/unit/Withdraw.t.sol#L44 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/test/integration/Integration.t.sol#L126 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/test/integration/Integration.t.sol#L138 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/test/unit/Exercise.t.sol#L60 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/test/unit/Exercise.t.sol#L95 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/test/unit/Exercise.t.sol#L142 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/test/unit/Exercise.t.sol#L158 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/test/unit/Withdraw.t.sol#L93-L94

Use solidity version 0.8.4 to save gas

Use a solidity version of at least 0.8.4 to get custom errors, which are cheaper at deployment than revert()/require() strings Use a solidity version of at least 0.8.10 to have external calls skip contract existence checks if the external call has a return value

Array. Length should be looked up in for loops to save gas

https://github.com/code-423n4/2022-06-putty/blob/main/contracts/test/differential/EIP712.t.sol#L101 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/test/integration/Integration.t.sol#L52 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/test/integration/Integration.t.sol#L44 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/test/integration/Integration.t.sol#L58 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/test/integration/Integration.t.sol#L69 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/test/integration/Integration.t.sol#L104 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/test/integration/Integration.t.sol#L112 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/test/integration/Integration.t.sol#L118 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/test/integration/Integration.t.sol#L126

Usage of uint smaller than byte32 incurs overhead --When using elements that are smaller than 32 bytes, your contract’s gas usage may be higher. This is because the EVM operates on 32 bytes at a time. Therefore, if the element is smaller than that, the EVM must use more operations in order to reduce the size of the element from 32 bytes to the desired size.

https://github.com/code-423n4/2022-06-putty/blob/main/contracts/test/mocks/MockWeth.sol#L7

COMPARISON OPERATORS

--In the EVM, there is no opcode for >= or <=. When using greater than or equal, two operations are performed: > and =. Therefore, using strict comparison operators hence saves gas https://github.com/code-423n4/2022-06-putty/blob/main/contracts/test/mocks/MockWeth.sol#L51 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/test/mocks/MockWeth.sol#L48 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/test/mocks/MockWeth.sol#L23

Using calldata instead of memory will save gas on view functions.

https://github.com/code-423n4/2022-06-putty/blob/main/contracts/test/shared/Fixture.t.sol#L72