Share Dialog
Share Dialog

Subscribe to KevinWang

Subscribe to KevinWang
<100 subscribers
<100 subscribers
In Ethereum, the execution of contract will cost gas fee. An excellent contract should cost gas fee as low as possible.
In this article, I will summary some gas optimization tips.
In Ethereum, storage data is written in contract account, which will cost the most gas fee.
We can use memory to storage middle data, instead of always writing storage data.
uint256 a = storageVar; // read storage
a += 10; // memory operation
storageVar = a; // write storage
Solidity aligns storage slots on 32-byte boundaries, multiple uint8 or bool values can be packed into the same slot.
struct Packed {
uint128 a;
uint128 b;
}
Use memory instead of Storage except the storage variable is necessary.
Use mapping instead of for-each searching array.
Mapping does not occupy continuous storage space, and read/write operations are relatively inexpensive.
If there are other judgment conditions before function call, we can prioritize judgment of prerequisite conditions.
if (a > 10 && expensiveCheck()) { ... }
External is cheaper than Public.
Pure and View not cost gas fee.
Use minimum sufficient data type. Like uint8, uint16 are more storage space-efficient than uint256.
Use as little as possible indexed parameter.
Index will cost more gas fee.
Avoid frequent emit large events
Multi-level inheritance will increase deployment Gas.
Use swap-and-pop instead of dynamic deletion.
But this way will disorder the sequence.
function remove(uint index) public {
arr[index] = arr[arr.length - 1];
arr.pop();
}
Using unchecked to skip overflow checking can save gas fee.
unchecked { i++; }
In Ethereum, the execution of contract will cost gas fee. An excellent contract should cost gas fee as low as possible.
In this article, I will summary some gas optimization tips.
In Ethereum, storage data is written in contract account, which will cost the most gas fee.
We can use memory to storage middle data, instead of always writing storage data.
uint256 a = storageVar; // read storage
a += 10; // memory operation
storageVar = a; // write storage
Solidity aligns storage slots on 32-byte boundaries, multiple uint8 or bool values can be packed into the same slot.
struct Packed {
uint128 a;
uint128 b;
}
Use memory instead of Storage except the storage variable is necessary.
Use mapping instead of for-each searching array.
Mapping does not occupy continuous storage space, and read/write operations are relatively inexpensive.
If there are other judgment conditions before function call, we can prioritize judgment of prerequisite conditions.
if (a > 10 && expensiveCheck()) { ... }
External is cheaper than Public.
Pure and View not cost gas fee.
Use minimum sufficient data type. Like uint8, uint16 are more storage space-efficient than uint256.
Use as little as possible indexed parameter.
Index will cost more gas fee.
Avoid frequent emit large events
Multi-level inheritance will increase deployment Gas.
Use swap-and-pop instead of dynamic deletion.
But this way will disorder the sequence.
function remove(uint index) public {
arr[index] = arr[arr.length - 1];
arr.pop();
}
Using unchecked to skip overflow checking can save gas fee.
unchecked { i++; }
No activity yet