Table of Contents
You just deployed a smart contract, and it’s costing 98 gas to run a function that does nothing. Literally.
See for yourself, Why?
Because even doing “nothing” on Ethereum still means something to the Ethereum Virtual Machine (EVM).
Welcome to Part 3 of our gas optimization series, where we will look at how gas really works behind the scenes and how you can make your Solidity smart contracts use less gas by removing anything that’s not needed.
The Building Blocks: OPCODEs Are the DNA of Ethereum
In Solidity, saving on gas is like saving fuel in your car. To do that, you need to know about OPCODEs – the basic commands that tell Ethereum what to do. They’re the ABCs of Ethereum’s language.
When you write Solidity code, it doesn’t run directly on the Ethereum network. Instead, it gets compiled into a set of OPCODEs that the Ethereum Virtual Machine (EVM) can understand and execute. Think of it like translating English into machine language that computers can process
What Are OPCODEs?
OPCODEs are low level instructions that perform specific operations:
Arithmetic Operations: ADD, SUB, MUL, DIV for mathematical calculations
Memory Operations: MLOAD, MSTORE for temporary data storage
Storage Operations: SLOAD, SSTORE for permanent blockchain storage
Program Flow: JUMP, JUMPI for conditional logic.
Cryptographic Functions: SHA3, ECRECOVER for security operations
Contract Interactions: CALL, DELEGATECALL for inter-contract communication
And here’s the catch:
Each OPCODE consumes gas, and some cost way more than others.
Pro Tip:
You can see the full list of OPCODE gas costs here: ethereum.org/en/developers/docs/evm/opcodes
The Secret Code: Function Selectors
Here’s something that might surprise you: when you compile your Solidity code into bytecode, the names of your functions don’t actually show up in the final assembly output. Instead, what ends up on the Ethereum network is something like a secret code called function selectors
How Function Selectors Work
Function selectors are created by taking the first four bytes of the hash of the function’s signature. Let’s look at a real example:
function doNothig() external pure {}
After compilation, this becomes:
46aad107
So when someone sends a transaction to your contract, they don’t send the name doNothig(),they send the selector 0x46aad107.
That’s how the EVM knows which function to run, without storing long names on-chain.
Gas Optimization Tips Every Developer Should Know
1. Keep It Light
The less code you deploy, the less gas you’ll use during deployment. Think of it as packing light for a flight – every extra byte costs money.
Code size optimization strategies:
- Remove unnecessary functions and variables
- Use libraries for common operations instead of duplicating code
- Implement proxy patterns for upgradeable contracts
- Strip out debugging code and comments in production
Real impact: A 1KB reduction in contract size saves approximately 200,000 gas units during deployment
2. Choose Wisely: OPCODE Selection
Certain commands are cheaper than others. Picking the right ones is like choosing a budget airline over a luxury carrier.
Cost-effective choices:
- Some instructions (like ADD) cost less than others (like SSTORE).
- Store fewer variables
- Avoid unnecessary require checks
- Minimize state changes
3. Pack Data Tight: Transaction Data Optimization
The more data you send in a transaction, the more you spend. Keep your transaction data lean.
Data packing techniques:
- Use struct packing to fit multiple variables in single storage slots
- Compress arrays and use fixed-size arrays when possible
- Batch multiple operations to reduce transaction overhead
- Use events instead of storage for data that doesn’t need to be queried on-chain
4. Minimize Memory Use
Memory is temporary, but still costs gas.
Avoid creating new memory variables when not required.
Use calldata instead of memory for function inputs when possible
5. Be Smart About Storage
Storage is the most expensive operation.
- Only write to storage if you must
- Use events to log non-critical data instead of storing it
- Don’t overwrite storage values unless necessary
Every time you write to SSTORE, you’re paying a premium.
The Compound Effect
A little bit of gas saved on every transaction can add up to significant savings over time. If your contract processes 1,000 transactions per day and you save 500 gas per transaction, that’s 500,000 gas units daily – potentially saving hundreds of dollars monthly at current gas prices.
Understanding OPCODEs and function selectors is just the beginning. In Part 4, we’ll explore advanced Solidity techniques that can dramatically reduce gas consumption

Leave a Reply