Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a BitMap struct #2710

Merged
merged 10 commits into from Jun 11, 2021
Merged

Add a BitMap struct #2710

merged 10 commits into from Jun 11, 2021

Conversation

Amxx
Copy link
Collaborator

@Amxx Amxx commented Jun 7, 2021

Native bitmap are really storage inneficient:

mapping(X => bool) will use a new storage slot for each entry
bool[] will use an underlying bytes1[] array, which only truly uses 32 bits per 256 bits storage slot.

This PR include a dense bitmap, that will save on gas when entries are consecutive. This design is used by Uniswap merkle-distributor to reduce the gas cost of writing to new (clean) storage slot by a factor 8 over what bool[] would have required.

PR Checklist

  • Tests
  • Documentation
  • Changelog entry

Copy link
Contributor

@frangio frangio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code and tests look good to me.

contracts/utils/structs/BitMap.sol Outdated Show resolved Hide resolved
contracts/utils/structs/BitMap.sol Outdated Show resolved Hide resolved
contracts/utils/structs/BitMap.sol Outdated Show resolved Hide resolved
contracts/utils/structs/BitMap.sol Outdated Show resolved Hide resolved
frangio
frangio previously approved these changes Jun 10, 2021
Copy link
Contributor

@frangio frangio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, though I wonder if we also want a set(BitMap, uint256, bool) function. Could be useful in some cases.

contracts/utils/structs/BitMaps.sol Show resolved Hide resolved
contracts/utils/structs/BitMaps.sol Show resolved Hide resolved
contracts/utils/structs/BitMaps.sol Show resolved Hide resolved
Co-authored-by: Francisco Giordano <frangio.1@gmail.com>
@frangio frangio merged commit f7da53c into OpenZeppelin:master Jun 11, 2021
@Amxx Amxx deleted the feature/structs/bitmap branch June 12, 2021 19:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants