morpho-org/morpho-data-structures

HOG-08C: Upward Traversal Optimization

MerlinEgalite opened this issue · 0 comments

HOG-08C: Upward Traversal Optimization

Type Severity Location
Gas Optimization HeapOrdering.sol:L128, L129

Description:

The shiftUp upward traversal mechanism of the heap structure is sub-optimal as it computes the result of getAccount(_heap, _rank >> 1) twice redundantly.

Example:

while (_rank > 1 && initialValue > getAccount(_heap, _rank >> 1).value) {
    setAccount(_heap, _rank, getAccount(_heap, _rank >> 1));
    _rank >>= 1;
}

Recommendation:

We advise the result to be cached to a local variable that is consequently utilized for the while loop condition and setAccount execution thus optimizing the gas cost of the function significantly.