Gas Optimizations #46
Labels
bug
Something isn't working
edited-by-warden
G (Gas Optimization)
sponsor confirmed
Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity")
Low effect on readability
[G-01] Use
!= 0
instead of> 0
for unsigned integers.A
uint
can't be below zero, so!= 0
is sufficient and is gas more efficient.1 instance:
Consider replacing
>
by!=
.save 3 gas
[G-02] Unnecessary initialization of variable
Some data type have a default value which is already the desired one. The default value of
uint
is0
, it is so unnecessary to initialize these again.3 instances:
Consider removing
= 0
save 3 gas each
[G-03] Transformation of post-increment to pre-increment
A pre-increment is cheaper than a post one. When it is possible, it is a good practice to apply pre-increment.
5 instances:
Consider transforming those.
With those changes, these evolutions in gas average report can be observe:
[G-04] Expression like
x = x + y
are cheaper thanx += y
for states variables.4 instances:
Consider replacing
+=
and-=
With those changes, these evolutions in gas average report can be observe:
[G-05] Some operations can be marked unchecked
If an operation can't overflow, it is cheaper to mark it as unchecked to avoid the automatic check of overflow.
In this case:
The operation can't overflow or undeflow
1 instances
Consider marking it unchecked
With this changes, these evolutions in gas average report can be observe:
This part can already be subjected to two improvements, however this one is still largely ineffective, especially for large numbers up to 2^256. It would be very useful to import a log10 function from an external mathematical library. The gain can be very important.
[G-06] Unnecessary public constant
Declaring a private constant is cheaper than a public one. In some case, a constant can be declared as private to save gas. It is the case if the constant don't need to be called outside the contract. A user could still read the value directly in the code instead of calling it, if needed.
8 instances:
Consider changing those constants to private. (The code still pass all the test with these changes.)
With those changes, these evolutions in gas average report can be observe:
[G-07] Using
storage
instead ofmemory
can be cheaper.A
storage
structure is pre allocated by the contract, by contrast, amemory
one is newly created. Depending on the case both can be used to optimize the gas cost because simply, astorage
is cheaper to create but more expensive to read from and to return and amemory
on the other hand is more expensive to create but cheaper to read from and to return. We can optimize with trials and errors instead of complex calculations (which will probably work a bit better, but it's not done here).Following this, we can deduce 7 cases that can be swapped to optimize runtime cost and deployment cost:
Consider changing
memory
tostorage
in these linesWith these changes, these evolutions in gas average report can be observed:
[G-08] Using
calldata
instead ofmemory
for read only argument in external functionIf a function parameter is read only, it is cheaper in gas to use
calldata
instead ofmemory
.4 instances:
Consider changing
memory
tocalldata
in these lines/With these changes, these evolutions in gas average report can be observed:
Medium effect in use
[G-09]
external
function for the admin can be marked aspayable
If a function is guaranteed to revert when called by a normal user, this function can be marked as
payable
to avoid the check to know if a payment is provided.2 instances:
Consider adding
payable
keyword.Save 21 gas cost
High effect on readability
[G-10] Optimise function name
Every function have a keccak256 hash, this hash defines the order of the function in the contract. The best the ranking, the minimum the gas usage to access the function. Each time a function is called, the EVM need to pass through all the functions better ranked (going through a function cost 22 gas), and this operation cost gas. This can be optimized, the ranking is defined by the first four bytes of the kekkack256 hash of the function name. The name can be changed to improve the ranking, which greatly impacts the readability. That's why it's not practical to change all the names, but it's possible to change only the ones of the functions called a lot of times. This change can be done on the following functions according to their number of uses in the tests and their current ranking.
Kernel.sol
: f166d9eb -modulePermissions(bytes5,address,bytes4)
Must outrank: 000dd95d -
moduleDependents(bytes5,uint256)
New name: 00097fbb -
modulePermissions_1055(bytes5,address,bytes4)
Rank: 14 -> 1
Save 286 gas each call
Kernel.sol
: c4d1f8f1 -executeAction(uint8,address)
Must outrank: 000dd95d -
moduleDependents(bytes5,uint256)
New name: 000a8da2 -
executeAction_11563(uint8,address)
Rank: 11 -> 2
Save 198 gas each call
MINTR.sol
: 1ae7ec2e -KEYCODE()
Must outrank: 02b1d239 -
ohm()
New name: 00906b26 -
KEYCODE_342()
Rank: 2-> 1
Save 22 gas each call
RANGE.sol
: bf30142b -capacity(bool)
Must outrank: 00d16739 -
regenerate(bool,uint256)
New name: 00e60c55 -
capacity_81(bool)
Rank: 14 -> 1
Save 286 gas each call
TRSRY.sol
: 1ae7ec2e -KEYCODE()
Must outrank: 15226b54 -
getReserveBalance(address)
New name: 00906b26 -
KEYCODE_342()
Rank: 2-> 1
Save 22 gas each call
VOTE.sol
: 1ae7ec2e -KEYCODE()
Must outrank: 06fdde03 -
name()
New name: 00906b26 -
KEYCODE_342()
Rank: 3 -> 1
Save 44 gas each call
Governance.sol
: d1755067 -endorseProposal(uint256)
Must outrank: 01153876 -
proposalHasBeenActivated(uint256)
New name: 007fedae -
endorseProposal_861(uint256)
Rank: 22 -> 1
Save 462 gas each call
Governance.sol
: 9459b875 -configureDependencies()
Must outrank: 01153876 -
proposalHasBeenActivated(uint256)
New name: 00aced39 -
configureDependencies_1382()
Rank: 18 -> 2
Save gas each call
Operator.sol
: 7159a618 -operate()
Must outrank: 01de9ba8 -
setReserveFactor(uint32)
New name: 000b8875 -
operate_53()
Rank: 18 -> 1
Save 352 gas each call
Operator.sol
: ec7404b1 -setActiveStatus(bool)
Must outrank: 01de9ba8 -
setReserveFactor(uint32)
New name: 00d3138f -
setActiveStatus_78(bool)
Rank: 31 -> 2
Save 638 gas each call
Consider optimizing these function names.
The text was updated successfully, but these errors were encountered: