• Struggling With EVM Gas Costs in Interviews — How Do SSTORE/SLOAD and Slot Packing Actually Work in Real Projects?

    Miben Rogers

    Miben Rogers

    @YGHQ65t
    Updated: Dec 9, 2025
    Views: 1.2K

    Yesterday’s smart contract interview was quite difficult for me.

    I was asked to explain SSTORE vs SLOAD gas costs, warm vs cold access, and when slot packing actually saves gas. I’ve studied these topics, but under pressure the “why behind the numbers” completely vanished.

    I understand the basics:

    • storage writes cost more than reads

    • EIP-2929 introduced warm/cold access

    • smaller variables can fit into a single slot

    But interviewers usually ask follow-up questions like “When does this matter in real contracts?” or “How would you optimize storage patterns without breaking readability?” — and that’s where I freeze.

    Can someone explain this in simple, memorable terms?

    • Why are storage writes so expensive in the first place?

    • When does warm/cold access actually appear in real transactions?

    • Does slot packing always work, or can Solidity reorder things?

    • How do engineers think about these optimizations at architecture level?

    If someone has a clean mental model or cheat-sheet explanation, I’d appreciate it. I want to avoid blanking out again in the next round.

    4
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on ArtOfBlockChain. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • SmartContractGuru

    @SmartContractGuru1yr

    The easiest way to survive these questions is to stop memorizing numbers and start framing the purpose of gas costs.

    1️⃣ Why SSTORE is expensive

    Storage writes modify Ethereum’s persistent state (Merkle Patricia Trie).
    This requires:

    • hashing

    • disk access

    • node propagation

    So EVM prices it to discourage unnecessary state growth.

    Mental model:

    “Writes touch global state. Reads touch local memory.”

    That’s enough to explain the why in interviews.

    2️⃣ Warm vs Cold SLOAD (EIP-2929)

    • Cold access: first time you touch a slot in a tx → more expensive.

    • Warm access: every subsequent read → much cheaper.

    Practical impact:

    • reading mappings inside loops becomes expensive unless access is warm

    • struct-heavy logic can be reorganized to reduce “first touches”

    Interviewers want to see if you understand the behavior, not the numbers.

    3️⃣ Slot packing (when it works)

    Packing works when:

    • variables are declared consecutively

    • types fit cleanly into 32 bytes

    • no dynamic types split the slot

    Example: uint128 + uint128 fits.
    uint128 + bool + uint256 will not pack properly because the uint256 forces alignment.

    A solid interview line:

    “Slot packing helps, but readability and upgradeability matter more than saving a few thousand gas.”

    That’s the real-world trade-off.

  • Sayali Bhandari

    @SayaliB1yr

    Think of gas as EVM’s “cost map.”
    Every opcode has a price because it consumes different resources.

    Why SLOAD is cheaper than SSTORE

    • SLOAD = read only → no state change

    • SSTORE = modifies state → has to update global trie → expensive

    Warm access example

    Imagine this function:

    for (uint i; i < users.length; i++) { total += users[i].balance; // cold first, warm next }

    Only the first SLOAD of that slot is cold.
    All loop iterations after that become warm → cheaper.

    Interview-ready explanation:

    “Warm/cold access matters because a poorly structured loop can multiply cold reads and inflate gas.”

    Slot packing real rule

    Solidity never reorders your variables.
    If your struct layout has a type that doesn’t fit nicely, Solidity will push the next variable to the next slot.

    Ideal packing order in interviews:

    • put fixed-size types together

    • group smaller types (uint64, uint32, bool)

    • avoid mixing dynamic types in packed structs

    This is exactly what interviewers want to hear:
    that you know the rules, the constraints, and the trade-offs.

  • Andria Shines

    @ChainSage1yr

    Can anyone explain how gas is mapped to opcodes like SSTORE and SLOAD in the Ethereum Virtual Machine (EVM)? Specifically, what factors impact gas usage for storage operations in a sample smart contract?

    For example, SSTORE tends to consume more gas when writing to storage, but the exact breakdown of this cost is unclear. On the other hand, SLOAD seems cheaper, but it’s still not clear why there’s a difference in gas usage between the two.

    Any insights into how these operations are linked to gas costs during contract execution, with an example of a smart contract or breakdown, would be helpful.

  • AnitaSmartContractSensei

    @SmartContractSensei10mos

    Here’s the cheat-sheet I give juniors:

    Gas Behavior (3-line summary)

    • Storage writes = expensive

    • Storage reads = moderate

    • Warm reads/writes = discounted after first use

    That’s it.

    When it matters in interviews

    Interviewers are checking whether you know that:

    • Contract state design affects long-term protocol costs

    • Mapping-heavy logic requires careful warm/cold thinking

    • Slot packing helps, but only when the struct layout allows it

    • Over-optimizing storage can make code harder to audit

    My go-to interview explanation

    “I optimize storage when it impacts repeated user-facing flows. But I avoid micro-optimizations if they hurt readability or upgrade safety.”

    This shows engineering maturity, not memorization.

  • Emma T

    @5INFFa43d

    From a QA/security lens, the reason gas-cost questions feel confusing in interviews is because most people study them as isolated numbers instead of looking at how they behave during state transitions. When we audit or test contracts, we don’t ask “How much does SSTORE cost?” — we ask “What part of this code path expands state, or forces repeated cold reads, or triggers unnecessary writes?”

    Here’s the framework I use:

    1️⃣ Storage writes = risk + cost

    Any SSTORE isn’t just expensive, it’s also a surface for bugs:

    wrong slot indexing

    stale state assumptions

    unexpected refunds

    overwritten values inside loops

    Interviewers love candidates who connect gas cost with correctness.

    2️⃣ Warm/cold access shows up in buggy patterns

    A lot of junior contracts unknowingly trigger cold reads inside nested loops or mapping lookups. In QA, we catch this by simulating worst-case paths. In interviews, you can simply say:

    “Cold accesses usually come from unstructured state reads. I try to reorganize logic so repeated accesses stay warm.”

    It shows maturity.

    3️⃣ Slot packing matters for audits, not just efficiency

    When structs are tightly packed, storage layout becomes predictable and harder to accidentally corrupt. When they are not, you see misalignment issues and upgrade conflicts.

    Interview-ready framing:

    “Packing is not just about saving gas — it reduces ambiguity in storage layout, which makes audits and upgrades safer.”

    If you explain gas topics with this testing mindset, you stand out immediately. Engineers who think about state behaviour instead of opcode prices perform better in real-world teams — and interviewers can detect that quickly.

  • Shubhada Pande

    @ShubhadaJP3d

    Very interesting discussion going on. Helping so many candidates who get stuck on gas-cost questions during interviews. What I’ve noticed from speaking with hiring managers is that people don’t get rejected for forgetting exact numbers; they struggle when they can’t explain the reasoning behind storage costs, warm/cold behavior, and real-world trade-offs.

    If you’re preparing for similar interview rounds, these AOB resources might help:

    Gas Pitfalls Juniors Mention — What Interviewers Actually Assess https://artofblockchain.club/discussion/gas-pitfalls-juniors-mention-what-interviewers-actually-assess

    AOB’s goal is to help candidates understand concepts in a way that sticks — not by memorizing gas tables, but by being able to explain why these behaviours exist and how they influence real contracts.

Home Channels Search Login Register