code-423n4 / 2021-10-pooltogether-findings

0 stars 0 forks source link

++i is more gas efficient than i++ for loops. #6

Open code423n4 opened 3 years ago

code423n4 commented 3 years ago

Handle

pants

Vulnerability details

At all loops definitions, you used i++ instead ++i although ++i is more gas efficient. An even better approach is to use unchecked {++i}, since you are using solidity version >=0.8.

PierrickGT commented 3 years ago

I did some gas golfing to figure out if ++i is really less gas consuming than i++. We would only save 5 gas per iteration but also lose in code clarity, so this gas saving trade off isn't really worth it.

About the unchecked part, results are a bit more convincing but still negligible. We would save 77 gas per iteration but as stated in the following issue, it is not possible to write unchecked { ++i } inline so we would have to write a helper function which would make our code less legible and harder to maintain in the future.

https://github.com/ethereum/solidity/issues/10695

I've acknowledged the issue but we won't actually make the changes cause we prefer to keep a simple code base that will be easier to maintain in the future.

GalloDaSballo commented 3 years ago

The sponsor does acknowledge and I agree that the gas savings do not justify for the loss of clarity