5

I want to keep track of a mapping of addresses with only 2 possible states. Does uint8 take more storage and consume more gas in my case?

contract MyContract {
    mapping(address => bool) boolBackedStatus;
    mapping(address => uint8) uint8BackedStatus;

    function setTrue(address user) {
        boolBackedStatus[user] = true;
        uint8BackedStatus[user] = 1;
    }

    function setFalse(address user) {
        boolBackedStatus[user] = false;
        uint8BackedStatus[user] = 0;
    }
}

1 Answer 1

1

Under the hood as of pragma solidity 0.4.19 both are stored as uint256.

The following two contracts use the same amount of gas when calling add.

As uint8:

pragma solidity 0.4.19;

contract AsUint8 {
    mapping(address => uint8) uint8s;

    function add()
    public
    {
        uint8s[msg.sender] = 1;
    }
}

As bool:

pragma solidity 0.4.19;

contract AsBool {
    mapping(address => bool) bools;

    function add()
    public
    {
        bools[msg.sender] = true;
    }
}

With an execution cost of 20484. According to quick tests on https://remix.ethereum.org

2
  • Can we conclude that storing uint8, bool and uint256 consumes same amount of memory? @Victory
    – alper
    Apr 21, 2018 at 15:44
  • 2
    @Alper thats my understanding. But the compiler's output could change in the future.
    – Victory
    Apr 22, 2018 at 23:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.