• EVM Interview Struggles: Gas Costs & Slot Packing

    Miben Rogers

    Miben Rogers

    @YGHQ65t
    Updated: Jul 17, 2025
    Views: 928

    EVM gas costs have me stumped—can someone break this down?

    Hey folks, quick one after a tough interview round:

    • How are gas costs mapped to opcodes like SSTORE, SLOAD (warm vs cold), or even MLOAD? I know every opcode has a price tag, but the why behind those numbers escaped me under pressure.

    • What exactly is “slot packing” in Solidity/EVM storage? I get that it saves gas by squeezing multiple variables into one 32-byte slot, yet the real-world mechanics—and when it doesn’t work—are still foggy.

    If you’ve got a clear explanation, a quick cheat-sheet, or any solid resource on EVM gas costs, storage refunds, or tight variable packing, please drop it here. Links to your favorite opcode tables or recent EIPs totally welcome.

    Thanks a ton—this community always comes through! 🙏


    3
    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

    @SmartContractGuru9mos

    What Interviewers Really Want to Hear

    First off, they're not just testing your memorization - they want to see if you understand the why behind gas costs. Here's the framework I always use:

    "Gas costs reflect computational complexity and state impact" - lead with this concept, then dive into specifics.

    The SSTORE Question (Almost Always Comes Up)

    When they ask about storage operations, here's your money answer:

    SSTORE costs vary based on state changes:

    • 20,000 gas for new storage slots (you're expanding global state)

    • 5,000 gas for updating existing slots (slot already allocated)

    • 200 gas for deleting values (you get a refund too!)

    Pro tip: I usually mention that this pricing incentivizes developers to minimize state bloat - shows you understand the bigger picture.

    The SLOAD Warm/Cold Access Pattern

    This is where you can really shine. Most candidates miss the nuance here:

    SLOAD normally costs 2,100 gas, but warm access is only 1 gas!

    What I tell interviewers: "In production, I always batch storage reads and structure functions to take advantage of warm access patterns. It's the difference between a 50-cent transaction and a $5 one."

    Slot Packing - The Practical Example

    Here's where you show real-world expertise. Don't just explain it - give them code:

    // ❌ Inefficient - uses 2 storage slots

    uint128 balance;

    uint128 rewards;


    // ✅ Optimized - uses 1 storage slot  

    struct UserData {

        uint128 balance;

        uint128 rewards;

    }

    Key interview point: "This optimization can literally halve your storage costs because each slot is 32 bytes, and two uint128s fit perfectly in one slot."

    Interview Red Flags to Avoid

    Don't just memorize gas costs - explain the reasoning

    Don't ignore practical implications (cost in real dollars)

    Don't forget to mention refunds and EIP-2929 changes

    Common Follow-Up Questions

    Be ready for these:

    "How would you optimize a contract with heavy storage usage?"

    "What's the trade-off between memory and storage?"

    "How do you handle dynamic arrays efficiently?"

    My Secret Weapon Answer

    When they ask about gas optimization, I always mention: "The best gas optimization is often architectural - sometimes it's better to use events for non-critical data or implement off-chain indexing rather than fighting with storage costs."

    Shows you think beyond just code-level optimizations.

    Hope this helps with your prep! The key is showing you understand both the technical details AND the business implications. Most candidates nail the first part but miss the second.

    Good luck! 🚀

    Drop a comment if you want me to cover any specific areas - always happy to help a fellow dev level up their interview game.

  • Sayali Bhandari

    @8n5KqIg8mos

    Great question! Let me break down these important Ethereum concepts for you.

    Gas Mapping and Opcode Costs refer to the predetermined computational expenses for executing operations on the Ethereum Virtual Machine (EVM). Each opcode carries specific gas requirements that correspond to its processing complexity and resource consumption. The SLOAD operation consumes 200 gas units, while SSTORE operations vary significantly: 20,000 gas for new value storage versus 5,000 gas for existing value updates. This differential pricing mechanism implements the "warm SLOAD" optimization strategy.

    Slot Packing represents a crucial storage optimization technique that maximizes EVM efficiency by consolidating multiple smaller data types within a single 32-byte storage slot. This approach substantially reduces gas expenses and enhances smart contract performance. When you group several uint8 variables together, the gas savings compared to individual slot allocation can be significant.

    These optimization strategies are essential for cost-effective smart contract development and improved blockchain performance.

  • Andria Shines

    @ChainSage8mos

    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

    @SmartContractSensei6mos

    In the Ethereum Virtual Machine (EVM), gas is mapped to opcodes like SSTORE and SLOAD based on the computational and storage resources required to execute these operations. Specifically, SSTORE costs vary depending on the storage change—setting a zero value to a non-zero value incurs a higher cost, while overwriting an existing value or resetting to zero is cheaper. Conversely, SLOAD is a read operation and thus consumes less gas as it does not modify storage.

    Breakdown of Gas Costs: SSTORE (Storage Writes):

    Writing a new value (from 0 to non-zero): Costs 20,000 gas. Overwriting an existing value (non-zero to non-zero): Costs 5,000 gas. Clearing a value (non-zero to 0): Refunds 15,000 gas due to reduced storage state. SLOAD (Storage Reads):

    Costs a flat 2,100 gas, as it simply reads from the trie structure in storage without modifying it. Factors Impacting Gas: State Change Complexity: Modifying storage involves writing to disk-backed storage, which is expensive. Gas Refunds: Clearing storage results in partial gas refunds to incentivize storage optimization. Cold vs. Warm Access: EIP-2929 introduced additional gas costs for "cold" storage slots, which are accessed for the first time in a transaction. Example: solidity Copy Edit contract GasExample { uint public value;

    function writeStorage(uint _value) public {
        value = _value; // SSTORE operation
    }
    
    function readStorage() public view returns (uint) {
        return value; // SLOAD operation
    }
    

    } Writing to value incurs SSTORE costs based on the value's previous state, while reading value incurs the lower SLOAD cost. Understanding these gas mappings is key to optimizing smart contract design.

Home Channels Search Login Register