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.

    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

    Member4w

    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

    Member2w

    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

    Member1w

    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
Home Channels Search Login Register