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

0 stars 1 forks source link

Gas Optimizations #20

Closed code423n4 closed 2 years ago

code423n4 commented 2 years ago

Preincrement Costs less gas(++i) as compared to postincrement(i++)

++i costs less gas as compared to i++ for unsigned integer, as per-increment is cheaper(its about 5 gas per iteration cheaper)

i++ increments i and returns initial value of i. Which means

uint i = 1; i++; // ==1 but i ==2

But ++i returns the actual incremented value:

uint i = 1; ++i; // ==2 and i ==2 , no need for temporary variable here

In the first case, the compiler has create a temporary variable (when used) for returning 1 instead of 2.

Instances:

contracts/NestedRecords.sol:78:                tokenIndex++;

Reference:

https://www.reddit.com/r/ethdev/comments/tcwspw/i_vs_i_gas_efficiency/

Remediation:

Use Preincrement(++i) instead of Postincrement(i++) in code.

Yashiru commented 2 years ago

Preincrement Costs less gas(++i) as compared to postincrement(i++) (Duplicated)

Duplicated of #2 at For loop optimizaion

JeeberC4 commented 2 years ago

Warden submitted multiple Gas Optimizations. Will not be judged.