Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gas Optimizations #546

Open
code423n4 opened this issue Oct 10, 2022 · 1 comment
Open

Gas Optimizations #546

code423n4 opened this issue Oct 10, 2022 · 1 comment

Comments

@code423n4
Copy link
Contributor

code423n4 commented Oct 10, 2022

Gas optimizations

[G-01] Usage of boolean state variable in storage

Use uint256(1) and uint256(2) for true/false to avoid a Gwarmaccess (100 gas) for the extra SLOAD, and to avoid Gsset (20000 gas) when changing from ‘false’ to ‘true’, after having been ‘true’ in the past.
See more here

[G-02] Avoid emitting a storage variable when a memory vairable is available

When the argument of the function and the memory value are the same, you should rather emit the argument variable (memory value).

[G-03] Set to payable permissionened functions that are guaranteed to revert for normal users

It will skip test that will lower the deployment and usage costs.
The extra opcodes avoided are CALLVALUE(2), DUP1(3), ISZERO(3), PUSH2(3), JUMPI(10), PUSH1(3), DUP1(3), REVERT(0), JUMPDEST(1), POP(2), which costs an average of about 21 gas per call to the function, in addition to the extra deployment cost

  1. BlurExchange.sol
  1. ExecutionDelegate.sol
  1. PolicyManager.sol

[G-04] Use != 0 instead of > 0 on uint variables

uint variables will never be lower than 0. Therefore, > 0 and != 0 have same meanings. Using != 0 can reduce the gas deployment cost, so it is worth using != 0 wherever possible.

  1. BlurExchange.sol

[G-05] Use prefix not postfix especially in loops

Saves 6 gas per loop.

  1. BlurExchange.sol
  1. PolicyManager.sol
  1. EIP712.sol
  1. MerkleVerifier.sol

[G-06] Do not initialize non constant/non immutable vairable to zero

Not overwriting the default value for stack variable saves 8 gas per loop.

  1. BlurExchange.sol
  1. PolicyManager.sol
  1. EIP712.sol
  1. MerkleVerifier.sol

Recommended mitigation steps:
Do not initialize them to 0 since it is the default value.

[G-07] ++i/i++ should be unchecked{++i}/unchecked{i++} when it is not possible to overflow

In solidity 0.8.0 and above, checked arithmetic is implemented but can be deactivated by using unchecked keyword. This saves 30-40gas per loop. Since i is bounded in the loop, there is no need for checked arithmetic if orders.length, fee.length < 28-1 and if orders.length, length, proof.length < 2256-1

  1. BlurExchange.sol
  1. PolicyManager.sol
  1. EIP712.sol
  1. MerkleVerifier.sol

[G-8]variable.length should not be looked up in every loop of a for-loop

The overheads outlined below are PER LOOP, excluding the first loop

  • storage arrays incur a Gwarmaccess (100 gas)
  • memory arrays use MLOAD (3 gas)
  • calldata arrays use CALLDATALOAD (3 gas)

Caching the length changes each of these to a DUP<N> (3 gas), and gets rid of the extra DUP<N> needed to store the stack offset

  1. BlurExchange.sol
  1. EIP712.sol
  1. MerkleVerifier.sol

[G-09] require()/revert() strings longer than 32 bytes cost extra gas

Each extra memory word of bytes past the original 32 incurs an MSTORE which cost 3 gas.

  1. BlurExchange.sol
  1. ExecutionDelegate.sol
@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels Oct 10, 2022
code423n4 added a commit that referenced this issue Oct 10, 2022
code423n4 added a commit that referenced this issue Oct 10, 2022
@GalloDaSballo
Copy link
Collaborator

5k gas from nonReentrant, 400 from memory emissions, rest is 150 at best

5550

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants