code-423n4 / 2022-05-velodrome-findings

0 stars 0 forks source link

Gas Optimizations #215

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

Gas Optimizations

[G01] uint default value is 0 so we can remove = 0:

Gauge.sol
  179,21:         for (uint i = 0; i < numRewards; i++) {
  222,20:         uint lower = 0;
  254,20:         uint lower = 0;
  286,20:         uint lower = 0;
  353,21:         for (uint i = 0; i < tokens.length; i++) {
  481,21:         uint reward = 0;
  551,22:         uint tokenId = 0;

Minter.sol
  57,21:         for (uint i = 0; i < claimants.length; i++) {

Pair.sol
  20,29:     uint public totalSupply = 0;
  61,24:     uint public index0 = 0;
  62,24:     uint public index1 = 0;
  257,21:         for (uint i = 0; i < _prices.length; i++) {
  273,24:         uint nextIndex = 0;
  274,20:         uint index = 0;
  389,21:         for (uint i = 0; i < 255; i++) {

RewardsDistributor.sol
  73,24:         uint next_week = 0;
  75,21:         for (uint i = 0; i < 20; i++) {
  103,19:         uint _min = 0;
  105,21:         for (uint i = 0; i < 128; i++) {
  119,19:         uint _min = 0;
  121,21:         for (uint i = 0; i < 128; i++) {
  148,21:         for (uint i = 0; i < 20; i++) {
  154,27:                 int128 dt = 0;
  170,25:         uint user_epoch = 0;
  171,28:         uint to_distribute = 0;
  195,21:         for (uint i = 0; i < 50; i++) {
  227,25:         uint user_epoch = 0;
  228,28:         uint to_distribute = 0;
  252,21:         for (uint i = 0; i < 50; i++) {
  299,20:         uint total = 0;

Router.sol
  90,21:         for (uint i = 0; i < routes.length; i++) {
  112,27:         uint _totalSupply = 0;
  316,21:         for (uint i = 0; i < routes.length; i++) {

Velo.sol
  9,29:     uint public totalSupply = 0;

VelodromeLibrary.sol
  24,21:         for (uint i = 0; i < 255; i++) {

Voter.sol
  76,21:         for (uint i = 0; i < _tokens.length; i++) {
  101,30:         uint256 _totalWeight = 0;
  103,21:         for (uint i = 0; i < _poolVoteCnt; i ++) {
  128,21:         for (uint i = 0; i < _poolCnt; i ++) {
  139,34:         uint256 _totalVoteWeight = 0;
  140,30:         uint256 _totalWeight = 0;
  141,29:         uint256 _usedWeight = 0;
  143,21:         for (uint i = 0; i < _poolCnt; i++) {
  147,21:         for (uint i = 0; i < _poolCnt; i++) {
  266,21:         for (uint i = 0; i < _gauges.length; i++) {
  304,21:         for (uint i = 0; i < _gauges.length; i++) {
  310,21:         for (uint i = 0; i < _gauges.length; i++) {
  346,21:         for (uint x = 0; x < _gauges.length; x++) {

VotingEscrow.sol
  584,27:         int128 old_dslope = 0;
  585,27:         int128 new_dslope = 0;
  622,26:         uint block_slope = 0; // dblock/dt
  632,25:             for (uint i = 0; i < 255; ++i) {
  636,32:                 int128 d_slope = 0;
  884,19:         uint _min = 0;
  886,21:         for (uint i = 0; i < 128; ++i) {
  940,19:         uint _min = 0;
  942,21:         for (uint i = 0; i < 128; ++i) {
  960,22:         uint d_block = 0;
  961,18:         uint d_t = 0;
  996,17:         uint dt = 0;
  1017,21:         for (uint i = 0; i < 255; ++i) {
  1019,28:             int128 d_slope = 0;
  1145,20:         uint votes = 0;
  1146,21:         for (uint i = 0; i < _tokenIds.length; i++) {
  1168,22:         uint32 lower = 0;
  1192,20:         uint votes = 0;
  1193,21:         for (uint i = 0; i < _tokenIds.length; i++) {
  1225,29:                 for (uint i = 0; i < srcRepOld.length; i++) {
  1249,29:                 for (uint i = 0; i < dstRepOld.length; i++) {
  1295,29:                 for (uint i = 0; i < srcRepOld.length; i++) {
  1320,29:                 for (uint i = 0; i < dstRepOld.length; i++) {
  1325,29:                 for (uint i = 0; i < ownerTokenCount; i++) {

governance/L2Governor.sol
  326,24:         for (uint256 i = 0; i < targets.length; ++i) {
  343,28:             for (uint256 i = 0; i < targets.length; ++i) {

[G02] ++i use less gas than i++:

++i costs less gas compared to i++. about 5 gas per iteration.

Gauge.sol
  179,42:         for (uint i = 0; i < numRewards; i++) {
  353,45:         for (uint i = 0; i < tokens.length; i++) {
  405,51:         for (uint i = _startIndex; i < _endIndex; i++) {
  426,34:         for (uint i; i < length; i++) {
  448,55:             for (uint i = _startIndex; i < _endIndex; i++) {
  484,55:             for (uint i = _startIndex; i < _endIndex; i++) {

Minter.sol
  57,48:         for (uint i = 0; i < claimants.length; i++) {

Pair.sol
  257,46:         for (uint i = 0; i < _prices.length; i++) {
  389,35:         for (uint i = 0; i < 255; i++) {

RewardsDistributor.sol
  75,34:         for (uint i = 0; i < 20; i++) {
  105,35:         for (uint i = 0; i < 128; i++) {
  121,35:         for (uint i = 0; i < 128; i++) {
  148,34:         for (uint i = 0; i < 20; i++) {
  195,34:         for (uint i = 0; i < 50; i++) {
  252,34:         for (uint i = 0; i < 50; i++) {
  301,48:         for (uint i = 0; i < _tokenIds.length; i++) {

Router.sol
  90,45:         for (uint i = 0; i < routes.length; i++) {
  316,45:         for (uint i = 0; i < routes.length; i++) {

VelodromeLibrary.sol
  24,35:         for (uint i = 0; i < 255; i++) {

Voter.sol
  76,46:         for (uint i = 0; i < _tokens.length; i++) {
  143,40:         for (uint i = 0; i < _poolCnt; i++) {
  147,40:         for (uint i = 0; i < _poolCnt; i++) {
  266,46:         for (uint i = 0; i < _gauges.length; i++) {
  272,39:         for (uint i = start; i < end; i++) {
  304,46:         for (uint i = 0; i < _gauges.length; i++) {
  310,46:         for (uint i = 0; i < _gauges.length; i++) {

VotingEscrow.sol
  1146,48:         for (uint i = 0; i < _tokenIds.length; i++) {
  1193,48:         for (uint i = 0; i < _tokenIds.length; i++) {
  1225,56:                 for (uint i = 0; i < srcRepOld.length; i++) {
  1249,56:                 for (uint i = 0; i < dstRepOld.length; i++) {
  1295,56:                 for (uint i = 0; i < srcRepOld.length; i++) {
  1320,56:                 for (uint i = 0; i < dstRepOld.length; i++) {
  1325,55:                 for (uint i = 0; i < ownerTokenCount; i++) {

[G03] Use Custom Errors to save Gas:

Custom errors from Solidity 0.8.4 are cheaper than require messages. https://blog.soliditylang.org/2021/04/21/custom-errors/

GalloDaSballo commented 2 years ago

Would save between 100 and 500 gas