Skip to content

In this article, I tried to give a brief explanation of gas optimization.

Notifications You must be signed in to change notification settings

0xdanial/gas-optimization

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 

Repository files navigation

memory

Slots of storage

In Ethereum, you pay the gas fee for anything storage slot used. because you should be optimizing your code to little storage slot use for costs of little gas. When you put two uint128 variables together, the solidity compiler detects them in a 256-bit memory slot and when put not together, each of uint128 variables occupied 256-bit memory. (128-bit memory wasting)

for example, this code is not optimized and will have 3 slot storage used:

uint8 a;
uint256 b;
uint8 c;

And now look at the below code, variables are packed:

uint8 a;
uint8 c;
uint256 b;

Do not shrink Variables without packing

if use the unit8 variables solidity compiler goes to convert unit256 to uint8 and this conversion costs a gas fee. because you should shrink variables when you can pack them.

Try not to initialize variables with the default value

When you initialize variables with default values costs unnecessary gas. Like this:

bool a = true;
uint b = 0;

Caching array length for loops

Caching the array length outside a loop:

👎 Without optimization:

for (uint256 i = 0; i < array.length; i++) {
}

👍 With optimization:

uint256 length = array.length;
for (uint256 i=0; i < length;) {
	doStuff(array[i]);
	unchecked { ++i; }
}

More resources:

https://github.com/byterocket/c4-common-issues/blob/main/0-Gas-Optimizations.md/#g001---dont-initialize-variables-with-default-value

https://medium.com/coinmonks/gas-optimization-in-solidity-part-i-variables-9d5775e43dde

https://dev.to/javier123454321/solidity-gas-optimizations-pt-3-packing-structs-23f4

https://yamenmerhi.medium.com/gas-optimization-in-solidity-75945e12322f

About

In this article, I tried to give a brief explanation of gas optimization.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published