Skip to content

Commit

Permalink
skip binary search if observation exists
Browse files Browse the repository at this point in the history
  • Loading branch information
trmid committed Feb 13, 2024
1 parent a9cda8f commit a689140
Showing 1 changed file with 37 additions and 54 deletions.
91 changes: 37 additions & 54 deletions src/libraries/DrawAccumulatorLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,6 @@ library DrawAccumulatorLib {
mapping(uint256 drawId => Observation observation) observations;
}

/// @notice A pair of uint24s.
struct Pair48 {
uint24 first;
uint24 second;
}

/// @notice Adds balance for the given draw id to the accumulator.
/// @param accumulator The accumulator to add to
/// @param _amount The amount of balance to add
Expand Down Expand Up @@ -103,10 +97,7 @@ library DrawAccumulatorLib {

return true;
} else {
accumulatorObservations[newestDrawId_] = Observation({
available: SafeCast.toUint96(newestObservation_.available + _amount),
disbursed: newestObservation_.disbursed
});
accumulatorObservations[newestDrawId_].available = SafeCast.toUint96(newestObservation_.available + _amount);

return false;
}
Expand Down Expand Up @@ -161,56 +152,48 @@ library DrawAccumulatorLib {
return 0;
}

uint24 firstObservationDrawIdOccurringAtOrAfterStart;
if (_startDrawId <= oldestDrawId || ringBufferInfo.cardinality == 1) {
firstObservationDrawIdOccurringAtOrAfterStart = oldestDrawId;
} else {
// The start must be between newest and oldest
uint24 beforeOrAtDrawId;
// binary search
(
,
beforeOrAtDrawId,
,
firstObservationDrawIdOccurringAtOrAfterStart
) = binarySearch(
_accumulator.drawRingBuffer,
uint16(oldestIndex),
uint16(newestIndex),
ringBufferInfo.cardinality,
_startDrawId
);
if (beforeOrAtDrawId == _startDrawId) {
firstObservationDrawIdOccurringAtOrAfterStart = _startDrawId;
// check if the start draw has an observation, otherwise search for the earliest observation after
Observation memory atOrAfterStart = _accumulator.observations[_startDrawId];
if (atOrAfterStart.available == 0 && atOrAfterStart.disbursed == 0) {
if (_startDrawId <= oldestDrawId || ringBufferInfo.cardinality == 1) {
atOrAfterStart = _accumulator.observations[oldestDrawId];
} else {
(, uint24 beforeOrAtDrawId, , uint24 afterOrAtDrawId) = binarySearch(
_accumulator.drawRingBuffer,
oldestIndex,
newestIndex,
ringBufferInfo.cardinality,
_startDrawId
);
if (beforeOrAtDrawId == _startDrawId) {
atOrAfterStart = _accumulator.observations[_startDrawId];
} else {
atOrAfterStart = _accumulator.observations[afterOrAtDrawId];
}
}
}

uint24 lastObservationDrawIdOccurringAtOrBeforeEnd;
if (_endDrawId >= newestDrawId || ringBufferInfo.cardinality == 1) {
// then it must be the end
lastObservationDrawIdOccurringAtOrBeforeEnd = newestDrawId;
} else {
uint24 afterOrAtDrawId;
(, lastObservationDrawIdOccurringAtOrBeforeEnd, ,afterOrAtDrawId) = binarySearch(
_accumulator.drawRingBuffer,
uint16(oldestIndex),
uint16(newestIndex),
ringBufferInfo.cardinality,
_endDrawId
);
if (afterOrAtDrawId == _endDrawId) {
lastObservationDrawIdOccurringAtOrBeforeEnd = _endDrawId;
// check if the end draw has an observation, otherwise search for the latest observation before
Observation memory atOrBeforeEnd = _accumulator.observations[_endDrawId];
if (atOrBeforeEnd.available == 0 && atOrBeforeEnd.disbursed == 0) {
if (_endDrawId >= newestDrawId || ringBufferInfo.cardinality == 1) {
atOrBeforeEnd = _accumulator.observations[newestDrawId];
} else {
(, uint24 beforeOrAtDrawId, , uint24 afterOrAtDrawId) = binarySearch(
_accumulator.drawRingBuffer,
oldestIndex,
newestIndex,
ringBufferInfo.cardinality,
_endDrawId
);
if (afterOrAtDrawId == _endDrawId) {
atOrBeforeEnd = _accumulator.observations[_endDrawId];
} else {
atOrBeforeEnd = _accumulator.observations[beforeOrAtDrawId];
}
}
}

Observation memory atOrAfterStart = _accumulator.observations[
firstObservationDrawIdOccurringAtOrAfterStart
];

Observation memory atOrBeforeEnd = _accumulator.observations[
lastObservationDrawIdOccurringAtOrBeforeEnd
];

return atOrBeforeEnd.available + atOrBeforeEnd.disbursed - atOrAfterStart.disbursed;
}

Expand Down

0 comments on commit a689140

Please sign in to comment.