Skip to content

Commit

Permalink
avoid useless array allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
OliverNChalk committed Jun 16, 2024
1 parent 2dd543e commit 6cedd2f
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions src/ReservoirPriceOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -148,20 +148,19 @@ contract ReservoirPriceOracle is IPriceOracle, IReservoirPriceOracle, Owned(msg.

OracleAverageQuery[] memory lQueries = new OracleAverageQuery[](lRoute.length - 1);

uint256[] memory lNewPrices = new uint256[](lRoute.length - 1);
for (uint256 i = 0; i < lRoute.length - 1; ++i) {
(lToken0, lToken1) = lRoute[i].sortTokens(lRoute[i + 1]);

lQueries[i] = OracleAverageQuery(
lNewPrices[i] = _getTimeWeightedAverageSingle(OracleAverageQuery(
PRICE_TYPE,
lToken0,
lToken1,
twapPeriod,
0 // now
);
));
}

uint256[] memory lNewPrices = getTimeWeightedAverage(lQueries);

for (uint256 i = 0; i < lNewPrices.length; ++i) {
address lBase = lQueries[i].base;
address lQuote = lQueries[i].quote;
Expand Down Expand Up @@ -192,19 +191,23 @@ contract ReservoirPriceOracle is IPriceOracle, IReservoirPriceOracle, Owned(msg.
rResults = new uint256[](aQueries.length);

OracleAverageQuery memory lQuery;

Check warning on line 193 in src/ReservoirPriceOracle.sol

View workflow job for this annotation

GitHub Actions / lint

Variable "lQuery" is unused
// TODO: We create the `aQueries` array just to iterate through it. Can
// we not just do each call inline without allocating an array?
for (uint256 i = 0; i < aQueries.length; ++i) {
lQuery = aQueries[i];
ReservoirPair lPair = pairs[lQuery.base][lQuery.quote];
_validatePair(lPair);

(,,, uint16 lIndex) = lPair.getReserves();
uint256 lResult = lPair.getTimeWeightedAverage(lQuery.priceType, lQuery.secs, lQuery.ago, lIndex);
rResults[i] = lResult;
rResults[i] = _getTimeWeightedAverageSingle(aQueries[i]);
}
}

function _getTimeWeightedAverageSingle(OracleAverageQuery memory aQuery)
private
view
returns (uint256 rResult)
{
ReservoirPair lPair = pairs[aQuery.base][aQuery.quote];
_validatePair(lPair);

(,,, uint16 lIndex) = lPair.getReserves();
rResult = lPair.getTimeWeightedAverage(aQuery.priceType, aQuery.secs, aQuery.ago, lIndex);
}

/// @inheritdoc IReservoirPriceOracle
function getLatest(OracleLatestQuery calldata aQuery) external view returns (uint256) {
ReservoirPair lPair = pairs[aQuery.base][aQuery.quote];
Expand Down

0 comments on commit 6cedd2f

Please sign in to comment.