Skip to content

Commit

Permalink
assembly isn't more efficient for EnumerableSet.add
Browse files Browse the repository at this point in the history
  • Loading branch information
Amxx committed Feb 9, 2021
1 parent 7402f58 commit 27a0a89
Showing 1 changed file with 5 additions and 16 deletions.
21 changes: 5 additions & 16 deletions contracts/utils/EnumerableSet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,10 @@ library EnumerableSet {
*/
function _add(Set storage set, bytes32 value) private returns (bool) {
if (!_contains(set, value)) {

uint256 last;
assembly {
let p := sload(0x40)
mstore(p, set.slot)
let blk := keccak256(p, 0x20)
// push new value
last := sload(set.slot)
sstore(add(blk, last), value)
// update array length
last := add(last, 1)
sstore(set.slot, last)
}
set._indexes[value] = last;

set._values.push(value);
// The value is stored at length-1, but we add 1 to all indexes
// and use 0 as a sentinel value
set._indexes[value] = set._values.length;
return true;
} else {
return false;
Expand Down Expand Up @@ -132,7 +121,7 @@ library EnumerableSet {
*
* - `index` must be strictly less than {length}.
*/
function _at(Set storage set, uint256 index) private view returns (bytes32 value) {
function _at(Set storage set, uint256 index) private view returns (bytes32) {
require(set._values.length > index, "EnumerableSet: index out of bounds");
return set._values[index];
}
Expand Down

0 comments on commit 27a0a89

Please sign in to comment.