Zokyo Gas Savings
  • โ›ฝZokyo Gas Savings
  • ๐Ÿ“šTutorials
    • โœ”๏ธGas Saving Technique 1: Unchecked Arithmetic
    • โ›“๏ธGas Saving Technique 2: Immutable Variable
    • โœจGas Saving Technique 3: Double star ** inefficiency
    • ๐Ÿ’ฐGas Saving Technique 4: Cache Array Length
    • โฌ…๏ธGas Saving Technique 5: ++i costs less gas compared to i++
    • โš–๏ธGas Saving Technique 6: NOT operator ! cheaper than boolean FALSE
    • ๐ŸชกGas Saving Technique 7: Using Short Reason Strings
    • ๐ŸชตGas Saving Technique 8: Use Custom Errors instead of Revert Strings to save Gas
    • โœ’๏ธGas Saving Technique 9: Use Custom Errors instead of Revert Strings to save Gas
    • ๐Ÿ‘พGas Saving Technique 10: Calldata cheaper than memory
    • โ›”Gas Saving Technique 11: > 0 is less efficient than != 0 for unsigned integers
    • โž—Gas Saving Technique 12: SafeMath no longer needed
    • ๐Ÿ˜ฎGas Saving Technique 13: variables default to 0
    • ๐ŸงฑGas Saving Technique 14: struct layout/ variable packing
    • ๐Ÿ“žGas Saving Technique 15: Cache External Call
    • โœ๏ธGas Saving Technique 16: Early Validation before external call
    • ๐Ÿ˜ŽGas Saving Technique 17: Donโ€™t cache value that is used once
    • ๐Ÿ˜งGas Saving Technique 18: Redundant code
    • โœ…Gas Saving Technique 19: Early Validation before external call
    • โ›๏ธGas Saving Technique 20: Storage vs Memory read optimizations
    • โœ’๏ธGas Saving Technique 21: Unneeded If statements
    • ๐ŸŒ—Gas Saving Technique 22: >= is cheaper than >
    • ๐ŸŽ’Gas Saving Technique 23: Public to private constants
    • โน๏ธGas Saving Technique 24: Make unchanged variables constant/immutable
    • โฑ๏ธGas Saving Techniques 25: Redundant Access Control Checks
    • โžก๏ธGas Saving Technique 26: Shift Right instead of Dividing by 2
    • ๐ŸชƒGas Saving Tutorial 27: Efficient Boolean Comparison
    • ๐ŸคGas Saving Technique 28: && operator uses more gas
    • ๐Ÿ‘“Gas Saving Technique 29: x = x + y is cheaper than x += y
    • ๐Ÿ‘‚Gas Saving Technique 30: Using 1 and 2 rather than 0 and 1 saves gas
    • โšฝGas Saving Technique 31: Optimize Storage by Avoiding Booleans
    • ๐Ÿ”™Gas Saving Technique 32: Optimal Use of Named Return Variables in Solidity
    • ๐Ÿ›ข๏ธGas Saving Technique 33: Making Functions Payable for Optimized Gas Costs
    • โœ๏ธGas Saving Technique 34: Optimizing Storage References in Smart Contracts
    • โ›ฐ๏ธGas Saving Technique 35: Usage of uints/ints smaller than 32 bytes (256 bits) incurs overhead
    • ๐ŸŒช๏ธGas Saving Technique 36: Inlining Single Use Internal Functions for Savings
    • โ˜„๏ธGas Saving Technique 37: Switching from Public to External Functions for Savings
    • ๐ŸŽ†Gas Saving Technique 38: Upgrading Solidity Compiler to Improve Gas Efficiency and Security
    • ๐Ÿ•ถ๏ธGas Saving Technique 39: Avoiding Duplicated Code for Gas Savings
    • ๐Ÿ˜„Gas Saving Technique 40: Removal of Unused Internal Functions for Gas Savings
    • ๐Ÿ–‹๏ธGas Saving Tutorial 41: In-lining Single Use Modifiers For Gas Saving
    • โ›๏ธGas Saving Technique 42: `require` vs`assert`
Powered by GitBook
On this page
  1. Tutorials

Gas Saving Technique 14: struct layout/ variable packing

PreviousGas Saving Technique 13: variables default to 0NextGas Saving Technique 15: Cache External Call

Last updated 1 year ago

Introduction

Gas optimization in smart contracts is essential for creating efficient and cost-effective applications on the Ethereum blockchain. One subtle yet impactful optimization technique is tight packing of variables within structs to minimize storage slots usage. Proper arrangement and ordering of variables in your structs can save storage slots, reducing the contract's deployment and state-changing transaction costs.

Impact & Details

Understanding Storage Slots

  • Storage Slot Consumption: In Ethereum, storage is organized into 32-byte slots. Each slot can hold a single variable of up to 32 bytes, and larger variables can span multiple slots. The ordering of variables in structs can affect how these slots are filled, influencing the gas cost for deploying and interacting with contracts.

  • Tight Variable Packing: By strategically ordering variables in structs, developers can ensure that storage slots are utilized efficiently, minimizing the total slots needed.

Implementation of Tight Variable Packing for Gas Savings

Practical Example: Optimizing Struct Variable Layout

Here is an example struct before and after optimization:

Before Optimization:

solidityCopy codestruct Claim {
    uint256 created;
    uint256 updated;
    address initiator;
    bytes32 protocol;
    uint256 amount;
    address receiver;
    uint32 timestamp;
    State state;
    bytes ancillaryData;
}

After Optimization:

solidityCopy codestruct Claim {
    uint256 created;
    uint256 updated;
    uint256 amount;
    address initiator;
    bytes32 protocol;
    address receiver;
    uint32 timestamp;
    State state;
    bytes ancillaryData;
}

In the optimized version, uint256 variables are grouped together at the beginning of the struct, followed by address and smaller-sized variables. This rearrangement allows for more efficient packing of data into storage slots, saving gas during deployment and operation.

Recommended Mitigation Steps

  1. Identify Structs for Optimization: Review your smart contracts to identify structs that can benefit from variable reordering.

  2. Rearrange Variables for Tight Packing: Reorder variables within each struct to minimize storage slot usage. Group larger variables together and place smaller variables adjacent to each other to make efficient use of the available space within each slot.

  3. Test: After making changes, rigorously test the smart contract to verify that its functionality remains intact while it utilizes fewer storage slots and consumes less gas.

Conclusion

Tight variable packing in structs is a simple but effective optimization for gas savings in smart contract development. By minimizing storage slot usage through efficient variable ordering, developers can reduce the gas costs associated with deploying and running smart contracts on the Ethereum network. As always, thorough testing is crucial to ensure that these optimizations do not inadvertently affect the contract's intended functionality.

๐Ÿ“š
๐Ÿงฑ
Book an audit with Zokyo