ArtOfBlockChain
  • Hey everyone, I recently gave a blockchain developer job interview, and I struggled with two questions about the Ethereum Virtual Machine (EVM). I'd appreciate some help understanding the answers better.

    1. How is gas mapped to major opcodes like SSTORE, SLOAD (and warm SLOAD), MLOAD, etc.? I understand that different opcodes have different gas costs, but I couldn’t explain how gas mapping works in detail.

    2. What is slot packing in the context of the EVM? I’m a bit confused about this concept and how it relates to storage optimization.

    If anyone could explain these concepts or point me to some resources, that would be awesome! Thanks in advance.

    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
  • Synthill Brown

    Member2mos

    Gas in the Ethereum Virtual Machine (EVM) is tied to opcodes based on how much work they require. For instance, the SSTORE opcode, which saves data to a contract's storage, has different gas costs depending on what you’re doing. If you’re storing a new value, it’ll set you back 20,000 gas, but if you're just updating an existing one, it's only 5,000 gas. This setup really encourages developers to manage their state efficiently.

    On the other hand, the SLOAD opcode, which pulls data from storage, costs 2,100 gas. But if you hit a "warm SLOAD" (basically, if you just accessed that data recently), it only costs you 1 gas! So, there's a big incentive to keep your storage access efficient.

    Now, about slot packing—this is all about maximizing how you use those storage slots in your contracts. Each slot can hold 32 bytes, so if you group smaller data types together, you can fit more into one slot. For example, instead of using separate slots for two uint256 variables, you could pack them into a single slot. This can save you a ton on gas costs and keep your contract lean, which is super important in a gas-sensitive environment like Ethereum!

    Are you sure? This action cannot be undone.
    Cancel
  • Sayali Bhandari

    Member2mos

    Hey there! No worries, these questions can be tricky.

    First up, gas mapping to opcodes like SSTORE and SLOAD essentially refers to the cost of executing these operations on the EVM. Each opcode has a predefined gas cost, which is intended to reflect the computational complexity and resource usage. For instance, SLOAD costs 200 gas, while SSTORE costs 20,000 gas when storing a new value, but only 5,000 gas if you’re updating an existing one (this is where warm SLOAD comes into play).

    As for slot packing, it’s a technique used to optimize storage in the EVM by packing multiple smaller data types into a single storage slot (which is 32 bytes). This minimizes storage costs and improves efficiency. For example, if you have several uint8 variables, packing them together can save gas compared to storing each in a separate slot.

    Hope that clears things up!

    Are you sure? This action cannot be undone.
    Cancel
  • Andria Shines

    Member2mos

    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.

    Are you sure? This action cannot be undone.
    Cancel
  • Anita Patel

    Member23h

    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.

    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register