Omniscia 0xPhase Audit

DiamondLib Code Style Findings

DiamondLib Code Style Findings

DLI-01C: Inefficient mapping Lookups

TypeSeverityLocation
Gas OptimizationDiamondLib.sol:L247, L250, L279, L286, L289, L299, L307, L321

Description:

The linked statements perform key-based lookup operations on mapping declarations from storage multiple times for the same key redundantly.

Example:

diamond/DiamondLib.sol
235/// @notice Adds a function to the diamond
236/// @param ds The diamond storage pointer
237/// @param _selector The function selector to add
238/// @param _selectorPosition The function selector position
239/// @param _facetAddress The facet address
240function addFunction(
241 DiamondStorage storage ds,
242 bytes4 _selector,
243 uint96 _selectorPosition,
244 address _facetAddress
245) internal {
246 ds
247 .selectorToFacetAndPosition[_selector]
248 .functionSelectorPosition = _selectorPosition;
249 ds.facetFunctionSelectors[_facetAddress].functionSelectors.push(_selector);
250 ds.selectorToFacetAndPosition[_selector].facetAddress = _facetAddress;
251}

Recommendation:

As the lookups internally perform an expensive keccak256 operation, we advise the lookups to be cached wherever possible to a single local declaration that either holds the value of the mapping in case of primitive types or holds a storage pointer to the struct contained.

Alleviation (3dd3d7bf0c2693b2f9c23bacedfa420393f7ea84):

All referenced mapping lookups have been optimized as advised, utilizing interim variables for iterative mapping declarations and caching values to local variables where applicable.