Keyword 'Unchecked' for Gas Optimizaiton
Contents
Introduction
Before Solidity 0.8.0, the SafeMath library needed to be imported manually to prevent the data overflow attack.
In following Solidity version, solidity will perform a check every time data changes to determine if there is an overflow and thus decide whether to throw an exception.
This mechanism also brings additional gas consumption.
Therefore, solidity provides unchecked block modifier to effectively remove the intermediate overflow detection, achieving the purpose of gas saving.
In this unit testing, I run unchecked block and without unchecked block 1000 times to evaluate the gas consumption.
We list code of contract as follow.
|
|
Evaluation
Here is the result:
Method | Gas Fee | Net Gas Fee | Save(Compare to writeByCalldata) |
---|---|---|---|
withoutUnckecked | 415751 | 415751 | 0 |
withUnckecked | 113773 | 113751 | 302000 (≈73%) |
Conclusion
If the security is under control, it is recommended to utilize unchecked for gas optimization.