F-2023-0011·gas-optimization

Save big amount of gas by using uint256 instead of bool in mapping

TL;DR

bool mappings cost an extra SLOAD per write since the EVM must read the slot, replace the bool bits, and rewrite it; uint256 sentinels avoid the read-modify-write.

Severity
INFO
Impact
LOW
Likelihood
LOW
Method
MManual review
CAT.
Complexity
LOW
Exploitability
LOW
02Section · Description

Description

Booleans are more expensive than uint256 or any type that takes up a full word because each write operation emits an extra SLOAD to first read the slot's contents, replace the bits taken up by the boolean, and then write back.

solidity
mapping(address => mapping(address => bool)) private _operatorApprovals;
mapping(address => bool) public sharedProxyAddresses;
03Section · Impact

Impact

By just using mapping(address => uint256), and having:

  • uint256 NOT_APPROVED = 1 instead of false
  • uint256 APPROVED = 2 instead of true

there can be a considerable difference on gas saved.

Recommended read: Save over a hundred thousand gas with this Solidity gas optimization tip.

04Section · Recommendation

Recommendation

Replace boolean mappings with uint256 sentinels using 1 and 2 as the off and on states respectively to avoid the SLOAD on every write.

F-2023-0011

oog
zealynx

Smart Contract Security Digest

Monthly exploit breakdowns, audit checklists, and DeFi security research — straight to your inbox

© 2026 Zealynx