Omniscia Moby Audit

EnumerableSet Code Style Findings

EnumerableSet Code Style Findings

EST-01C: Potentially Inefficient Shift Operation

Description:

The EnumerableSet::_remove function will unconditionally switch the last element of the array with the element being removed even if the element being removed is the last one in the array.

Example:

contracts/tokens/erc1155/libraries/EnumerableSet.sol
216function _remove(
217 Set storage set,
218 bytes32 value
219) private returns (bool status) {
220 uint256 valueIndex = set._indexes[value];
221
222 if (valueIndex != 0) {
223 unchecked {
224 bytes32 last = set._values[set._values.length - 1];
225
226 // move last value to now-vacant index
227
228 set._values[valueIndex - 1] = last;
229 set._indexes[last] = valueIndex;
230 }
231 // clear last index
232
233 set._values.pop();
234 delete set._indexes[value];
235
236 status = true;
237 }
238}

Recommendation:

We advise the code to perform the set._values and set._indexes assignments solely when the valueIndex - 1 value is different from the set._values.length - 1 value, optimizing the code's gas cost.

Alleviation (b02fae335f62cc1f5f4236fb4d982ad16a32bd26):

The code has been optimized per our recommendation, updating the value and index data points solely when the item being removed is not the last element in the set.