Solidity gas optimization techniques, using Foundry. 总结写 Solidity 智能合约更省 gas 的技巧。
Testing
forge test --contracts 01_Constant/Constant.t.sol --gas-report
Gas report
Function Name | Gas Cost |
---|---|
varConstant | 183 |
varImmutable | 161 ✅ |
variable | 2305 |
Testing
forge test --contracts 02_CalldataAndMemory/CalldataAndMemory.T.sol --gas-report
Gas report
Function Name | Gas Cost |
---|---|
writeByCalldata | 67905 ✅ |
writeByMemory | 68456 |
Testing
forge test --contracts 03_Bitmap/Bitmap.T.sol --gas-report
Gas report
Function Name | Gas Cost |
---|---|
setDataWithBitmap | 22366 ✅ |
setDataWithBoolArray | 35729 |
Testing
forge test --contracts 04_unchecked/Unchecked.T.sol --gas-report
Gas report
Function Name | Gas Cost |
---|---|
forNormal | 1910309 |
forUnckecked | 570287 ✅ |
Testing
forge test --contracts 05_uint/Uint.T.sol --gas-report
Gas report
Function Name | Gas Cost |
---|---|
read Uint8 | 2379 |
read Uint128 | 2465 |
read Uint256 | 2317 ✅ |
set Uint8 | 5355 |
set Uint128 | 5358 |
set Uint256 | 5322 ✅ |