Gas Optimization in Solidity: Variable Packaging

By November 23, 2021DeFi
Click here to view original web page at medium.com

Smarts contracts are here to automate the economy, it’s a game-changer in how we purchase, and it's going to continue growing in popularity and one day will be as common as smartphones. I truly believe in this technology and I am really excited about the applications that this will have.

In the last month, I have been learning more and more about how to do code smart contracts in solidity. Now I would like to share learning material on some points that I feel every solidity developer needs to be a great asset for defi projects. In this lecture, you don't need to be so advanced in this language, however, so you can start applying the knowledge that I am exposing here you will need to have a basic knowledge of the language and be able to write basic smarts contracts.

But what is Gas optimization and why is so important?

Every time we deploy, sing or interact with a smart contract we spend gas. A smart contract is an automated process and since the blockchain is a decentralized system that is being maintained by many of their users with different roles, a great feature of this system is that offers rewards to them. This incentive is one of the reasons why this technology is attracting a lot of people.

And a smart contract once is deployed on a blockchain it can not be changed (is an immutable piece of code) its really important to be sure that is going to consume the little amount of gas as possible. Since this software runs on the ethereum virtual machine we have to know how things are handled in the background.

In solidity when we write a smart contract we have different places where the data is saved the one that we will be talking about is the storage. Storage: This is where all the contract state variables reside. and every time we initialize, declare or re-assign a value of a state var we spend gas. So we have to be careful with state vars and how we handle them. But more important even is that we have to pay attention to how vars are being packed.

So… Why is var packaging so important?

We save state vars on the storage slot by slot, each slot has a space of 32 bytes equal to 256 bits. When we declared a uint8 (unsigned integer of 8bits), and then a uint256 (unsigned integer of 256bits) on the storage since the 8 and 256 uint can not be together on a slot that is 256 bits our contract will take 2 slots and one of them it will be just for an 8 bit. In the end, we will consume 64bits of space.

But the worse part is that: when you have a entire slot and you have just a unit8 due to a lack of proper var packaging the Ethereum virtual machine has to first convert it to uint256 to work it and the conversion cost more Gas! We can avoid this by seeing this process as a Tetris game! Let me explain:

This will be like coding:

contract badPackaging {
uint192 a;
uint256 b;
}

This will be like coding:

contract badPackaging {
uint248 c;
uint256 d;
}

This will be like coding:

contract GoodPackaging {
uint8 a;
uint16 b;
uint88 c;
uint64 d;
uint40 e;
uint40 f;
}

I am glad you have reached the end of my article, I hope that my personal abstraction can help you write gas-efficient smart contracts I invite you to follow me if you like this content and to keep learning!

Also Read

All Today's Crypto News In One Place