Optimize removal in EnumerableSet (#2605)
Co-authored-by: Francisco Giordano <frangio.1@gmail.com>
This commit is contained in:
@ -1,5 +1,8 @@
|
||||
# Changelog
|
||||
|
||||
## Unreleased
|
||||
* Enumerables: Improve gas cost of removal in `EnumerableSet` and `EnumerableMap`.
|
||||
|
||||
## Unreleased
|
||||
|
||||
* `IERC20Metadata`: add a new extended interface that includes the optional `name()`, `symbol()` and `decimals()` functions. ([#2561](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/2561))
|
||||
|
||||
@ -81,15 +81,14 @@ library EnumerableSet {
|
||||
uint256 toDeleteIndex = valueIndex - 1;
|
||||
uint256 lastIndex = set._values.length - 1;
|
||||
|
||||
// When the value to delete is the last one, the swap operation is unnecessary. However, since this occurs
|
||||
// so rarely, we still do the swap anyway to avoid the gas cost of adding an 'if' statement.
|
||||
if (lastIndex != toDeleteIndex) {
|
||||
bytes32 lastvalue = set._values[lastIndex];
|
||||
|
||||
bytes32 lastvalue = set._values[lastIndex];
|
||||
|
||||
// Move the last value to the index where the value to delete is
|
||||
set._values[toDeleteIndex] = lastvalue;
|
||||
// Update the index for the moved value
|
||||
set._indexes[lastvalue] = valueIndex; // Replace lastvalue's index to valueIndex
|
||||
// Move the last value to the index where the value to delete is
|
||||
set._values[toDeleteIndex] = lastvalue;
|
||||
// Update the index for the moved value
|
||||
set._indexes[lastvalue] = valueIndex; // Replace lastvalue's index to valueIndex
|
||||
}
|
||||
|
||||
// Delete the slot where the moved value was stored
|
||||
set._values.pop();
|
||||
|
||||
Reference in New Issue
Block a user