raineorshine
4/24/2017 - 4:01 PM

pack-storage.sol

contract MyContract {
    
    User user;
    mapping (address => User) public users;
    
    uint40 time; // cast to 5 bytes - packed with user
    address addr1; // 20 bytes - packed with time
    address addr2; // 20 bytes - unpacked, one slot
    
    struct User {
        uint40 time; // cast to 5 bytes - packed with user
        address addr1; // 20 bytes - packed with time
        address addr2; // 20 bytes - unpacked, one slot
    }
    
    // 45510 / 15510
    function setVars() {
        time = uint40(now);
        addr1 = this;
        addr2 = this;
    }

    // 45616 / 15616
    function setUser() {
        user = User({
            time: uint40(now),
            addr1: this,
            addr2: this
        });
    }
    
    // 45727 / 15727
    function setUserMapping() {
        users[this] = User({
            time: uint40(now),
            addr1: this,
            addr2: this
        });
    }

}