How to reduce Gas cost in Smart contract?

Well, to be frank its very difficult to do so!
Let me give you an idea, what I am talking about. As we know, in Etheruem, we can write smart contracts (codes that are executed on EVM). So for every line of execution of code on EVM — Ethereum Virtual Machine, one has to pay ‘gas’ — a smaller unit of Ether, to the network.
Basically, what happens, when you compile a solidity code, a .sol file, it gets transformed into byte code to be executed on EVM. You can see this in action by clicking details button under compile tab in remix browser (see the screen shot below).
Notice you also get opcodes (which is operation codes, a assembly level language, basically instructions to be executed on the machine.). Executing these instructions, consume gas.
For instance, an addition operation that sums up the top two items of the stack takes 3 units of gas. It is worth noting that real smart contracts consist of lots of operations and some operations consume much more gas than the addition operation.
- Stack operations (e.g., POP, PUSH), arithmetic operations (e.g., ADD, SUB), bit wise operations (e.g., OR, XOR), and comparison operations (e.g., LT/GT) are cheap because being a stack-based virtual machine, EVM favors such stack-related operations.
- Loading a word (i.e., 256 bits) from the memory (e.g., MLOAD) or saving a word to the memory (e.g., MSTORE) are also cheap. For those who don’t know what memory is in solidity, please refer to the solidity document. And search for data allocation (3 types basically, storage, memory, calldata).
- It is worth noting that the gas consumption will be multiplied if many words in memory are read or written.
- Loading a word from the storage (i.e., SLOAD) or saving a word to the storage (i.e., SSTORE) are expensive.
- Memory is temporary but the storage is persistent memory.
- A SSTORE operation costs 20,000 units of gas if the storage word is set to non-zero from zero; otherwise, it costs 5,000.
- EVM has a number of block chain-specific operations which are very expensive, such as BALANCE, CREATE and CALL.
- A conditional jump (i.e, JUMPI) is more expensive than an unconditional jump (i.e., JUMP).
Check out this link for more reference.
So the biggest question is, how can we write these contracts, so that it won’t consume much gas?
Following are the ways by which we may avoid gas devour :
- Understanding of EVM’s instructions properly : if we know these instructions and understand their usage, we can definitely write our codes in such a way that it will consume less gas.
- Using data location (memory , storage and stack)wisely : Whenever we use data locations, we have to use them wisely otherwise we may end up loosing gas.
- We should avoid loops and recursions.
- We should also focus on amount of data read or written.
Below are few links that may help you to understand more on this topic :
You can also join this whatsapp group, “Blockchain #incodewetrust” to participate in discussion related to Ethereum Blockchain. We currently have 160+ developers and having a great discussion every day. Only request I want to make here that, if you are a crypto lover then this is not the right group for you. Coders are most welcome!