r/ethdev • • 16d ago

Question Why are so many opcodes used for a simple transaction?

Lets say we have this simple smart contract written in solidity:

pragma solidity ^0.8.13;
contract Counter {
    uint256 public number;
    function setNumber(uint256 newNumber) public {
        number = newNumber;
    }
}

Environment:

  • Solidity: 0.8.13
  • Optimizer: disabled
  • Foundry: 1.0.0-stable

After compilation we get this runtime code: 6080...56fe. I was curious about how this bytecode is executed for example when I send a transaction with function signature: setNumber(uint256 newNumber) and argument: 1. And I analysed by hand opcode by opcode what is happening (yes I'm crazy - leave me alone). And I wrote it this way::

6080 PUSH1 stack:80

6040 PUSH1 stack:40,80

etc.

I counted 118 opcode executions until I reached the 00(STOP) opcode. Which I think is a crazy amount of opcodes.

I'm aware that what really matters in this example is the SSTORE opcode which consumes 98% of the gas. I have these questions:

  1. Is it really that necessary to have these 118 opcodes to store a single value? At some point I just lost what happens after multiple SWAPs, POPs and JUMPs. Is it important for security reasons or rather to make bytecode more universal in case of more complex operations?
  2. When it comes to optimizing gas consumption do people really pay attention to that 2% of triggered opcodes or do they just focuse entirely on finding the locations of operational codes that actually consume gas, such as sstore?

PS After writing this post, I compiled the code with the optimizer enabled and got a completely new, more optimized bytecode, so I'm no longer sure if my original post is still relevant, but I'd still love to hear your thoughts.

4 Upvotes

7 comments sorted by

2

u/koalofficial 16d ago

Most of those 118 opcodes are compiler boilerplate, not your logic. With the optimizer off, solc emits the generic version of everything: it sets up the free memory pointer (the 6080 6040 52 at the start), checks CALLVALUE because the function is non-payable, checks calldatasize >= 4, shifts calldata right by 0xe0 to get the selector, then walks a chain of DUP/PUSH4/EQ/JUMPI comparisons to find the matching function. After that comes the ABI decoder for the argument (length check, validation, a bunch of JUMPDESTs and SWAP/POP shuffling), then the actual SSTORE, then the return path.

It matters less than it looks because most of those ops cost 1-3 gas each, so the whole dispatch overhead is a few hundred gas. Compare that to roughly 21k intrinsic gas plus about 22.1k for a cold zero-to-nonzero SSTORE. The optimizer will trim redundant stack juggling, and if you want to see how much dispatch costs you can compare `forge inspect` output or gas snapshots with it on vs off. For contracts with many functions, selectors get sorted and binary-searched rather than checked linearly, so it scales better than you'd expect.

1

u/NaturalCarob5611 16d ago

There's a bunch of security checks and method routing stuff baked into the solidity code.

The call comes in, your bytecode starts executing. Purely functionally, it has to look at the calldata you passed in, look at the beginning for the method identifier, find which method you called, parse the calldata according to the method signature, and only then can it start running the method code. But there are also security checks - the method you specified isn't marked payable, so it's going to check if the call included a payment and revert if it did.

1

u/rayQuGR 16d ago

118 opcodes for a simple storage update does look excessive at first, but I think the important distinction is between the number of instructions and their actual gas cost.

The EVM has to handle dispatching the function, decoding calldata, managing the stack and memory, checking execution conditions, and finally updating storage. Most of that is relatively cheap compared with an SSTORE.

I'd be curious to see the same comparison on an EVM-compatible confidential environment like Oasis Sapphire. The execution model is familiar, but confidentiality adds another dimension to the cost and security analysis.

One thing worth remembering is that optimizing opcode count doesn't automatically mean optimizing the most expensive part of execution. Storage access patterns and state changes are usually where I'd start looking.

1

u/SocraLueey 15d ago

Compare 0→1, 1→2 and 1→1 in separate transactions. SSTORE’s cost depends on the storage transition, so that 98% is specific to the case you measured.

1

u/ledgertech_lottie_br 12d ago

I was looking at this for a project and honestly the dispatcher overhead is insane. The optimizer does wonders but if you're deploying many contracts it adds up. Have you tried using libraries or inheriting from OpenZeppelin's base contracts to cut that initial boilerplate?