20,000 USDC
View results
Submission Details
Severity: gas
Valid

Multiple `address`/ID mappings can be combined into a single `mapping` of an `address`/ID to a struct, where appropriate

Summary

Multiple address/ID mappings can be combined into a single mapping of an address/ID to a struct

Vulnerability Details

Saves a storage slot for the mapping. Depending on the circumstances and sizes of types, can avoid a Gsset (20000 gas) per mapping combined. Reads and subsequent writes can also be cheaper when a function requires both values and they both fit in the same storage slot. Finally, if both fields are accessed in the same function, can save ~42 gas per access due to not having to recalculate the key's keccak256 hash (Gkeccak256 - 30 gas) and that calculation's associated stack operations.

19 mapping(address => uint256) public supplyIndex;
22 mapping(address => uint256) public balances;
23 mapping(address => uint256) public claimable;

https://github.com/Cyfrin/2023-07-beedle/blob/main/src/Staking.sol#L19-L24

Impact

Gas savings

Tools Used

Manual review

Recommendations

Consider combining multiple mappings of the same type into one mapping to a struct

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.