Created
February 16, 2022 12:47
-
-
Save andreiashu/57d0c26651b599e776dcca5501cad6c3 to your computer and use it in GitHub Desktop.
gas costs and a small gotcha about address to bool mappings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
pragma solidity 0.8.11; | |
contract MappingBool { | |
// NB: if you need to distinguish from nil vs false this method does not work | |
mapping(address => bool) entries; | |
// gas 44463 | |
function add(address _a, bool _b) public { | |
entries[_a] = _b; | |
} | |
// gas 24355 | |
function isTrue(address _a) public view returns (bool) { | |
return entries[_a] == true; | |
} | |
// gas 24311 | |
// NB: this will return true for nil entries as well | |
function isFalse(address _a) public view returns (bool) { | |
return entries[_a] == false; | |
} | |
} | |
contract MappingInt { | |
// NB: if you need to specificall distinguish from nil vs false this method does not work | |
mapping(address => uint256) entries; | |
// gas 44452 | |
function add(address _a, bool _b) public { | |
entries[_a] = _b == true ? 1 : 2; | |
} | |
// gas 24329 | |
function isTrue(address _a) public view returns (bool) { | |
return entries[_a] == 1; | |
} | |
// gas 24263 | |
function isFalse(address _a) public view returns (bool) { | |
return entries[_a] == 2; | |
} | |
// gas 24307 | |
function isSet(address _a) public view returns (bool) { | |
return entries[_a] > 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment