Skip to content

Commit

Permalink
avoid unnecessary temporary array
Browse files Browse the repository at this point in the history
  • Loading branch information
OliverNChalk committed Jul 7, 2024
1 parent 5a84c54 commit 56657be
Showing 1 changed file with 13 additions and 18 deletions.
31 changes: 13 additions & 18 deletions src/ReservoirPriceOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -254,30 +254,25 @@ contract ReservoirPriceOracle is IPriceOracle, IReservoirPriceOracle, Owned(msg.
view
returns (address[] memory rRoute, int256 rDecimalDiff, uint256 rPrice)
{
address[] memory lResults = new address[](Constants.MAX_ROUTE_LENGTH);
bytes32 lSlot = Utils.calculateSlot(aToken0, aToken1);

bytes32 lFirstWord;
uint256 lRouteLength;
assembly {
lFirstWord := sload(lSlot)
}

// simple route
if (lFirstWord.isSimplePrice()) {
lResults[0] = aToken0;
lResults[1] = aToken1;
lRouteLength = 2;
rRoute = new address[](2);
rRoute[0] = aToken0;
rRoute[1] = aToken1;
rDecimalDiff = lFirstWord.getDecimalDifference();
rPrice = lFirstWord.getPrice();
}
// composite route
else if (lFirstWord.isCompositeRoute()) {
address lSecondToken = lFirstWord.getTokenFirstWord();

lResults[0] = aToken0;
lResults[1] = lSecondToken;

// REVIEW: Is it more logical to handle `is2HopRoute` then fallback to `assert(is3HopRoute)`?
if (lFirstWord.is3HopRoute()) {
bytes32 lSecondWord;
Expand All @@ -286,21 +281,21 @@ contract ReservoirPriceOracle is IPriceOracle, IReservoirPriceOracle, Owned(msg.
}
address lThirdToken = lSecondWord.getThirdToken();

lResults[2] = lThirdToken;
lResults[3] = aToken1;
lRouteLength = 4;
rRoute = new address[](4);
rRoute[2] = lThirdToken;
rRoute[3] = aToken1;
} else {
lResults[2] = aToken1;
lRouteLength = 3;
rRoute = new address[](3);
rRoute[2] = aToken1;
}

rRoute[0] = aToken0;
rRoute[1] = lSecondToken;
}
// no route
// solhint-disable-next-line no-empty-blocks
else if (lFirstWord.isUninitialized()) { }

rRoute = new address[](lRouteLength);
for (uint256 i = 0; i < lRouteLength; ++i) {
rRoute[i] = lResults[i];
else if (lFirstWord.isUninitialized()) {
rRoute = new address[](0);
}
}

Expand Down

0 comments on commit 56657be

Please sign in to comment.