You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Update value order can be adjusted to simplify the code and save gas
For example, to update the num variable with newVal, the current way is as following:
uint oldVal = num;
num = newVal;
emitupdate(oldVal, newVal);
If the execution order is adjusted, some operations can be saved (memory space allocation, variable assignment), reducing both the deployment and run time gas cost.
emitupdate(num, newVal);
num = newVal;
The operation saved: 1 sload, 1 memory allocation, 1 mstore.
There are multiple places can use this trick for optimization, since the updates of parameters are widely and frequently used, the optimization can be beneficial.
X = X + Y / X = X - Y
IS CHEAPER THANX += Y / X -= Y
The demo of the gas comparison can be seen here.
Consider use
X = X + Y / X = X - Y
to save gas.Instances number of this issue:
Update value order can be adjusted to simplify the code and save gas
For example, to update the
num
variable withnewVal
, the current way is as following:If the execution order is adjusted, some operations can be saved (memory space allocation, variable assignment), reducing both the deployment and run time gas cost.
The operation saved: 1 sload, 1 memory allocation, 1 mstore.
The demo of the gas comparison can be seen here.
There are multiple places can use this trick for optimization, since the updates of parameters are widely and frequently used, the optimization can be beneficial.
Instances number of this issue: 3
Code reuse
Some function codes overlap.
Consider reuse the common part and save deployment gas.
retrievePledgeRewards()
andclosePledge()
codes overlap a lot, only a few lines of difference.the following are used in
extendPledge()
,increasePledgeRewardPerVote()
and_pledge()
:pledgeParams.closed
could be deletedThis variable does not seem useful, since the check for
if(pledgeParams.endTimestamp <= block.timestamp)
can serve the purpose ofpledgeParams.closed
.Consider delete
pledgeParams.closed
.The text was updated successfully, but these errors were encountered: