Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip binary search if observation exists #88

Merged
merged 2 commits into from
Feb 13, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 28 additions & 44 deletions src/libraries/DrawAccumulatorLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -161,56 +161,40 @@ 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];
trmid marked this conversation as resolved.
Show resolved Hide resolved
} else {
(, , , uint24 afterOrAtDrawId) = binarySearch(
_accumulator.drawRingBuffer,
oldestIndex,
newestIndex,
ringBufferInfo.cardinality,
_startDrawId
);
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];
trmid marked this conversation as resolved.
Show resolved Hide resolved
if (atOrBeforeEnd.available == 0 && atOrBeforeEnd.disbursed == 0) {
if (_endDrawId >= newestDrawId || ringBufferInfo.cardinality == 1) {
atOrBeforeEnd = _accumulator.observations[newestDrawId];
} else {
(, uint24 beforeOrAtDrawId, , ) = binarySearch(
_accumulator.drawRingBuffer,
oldestIndex,
newestIndex,
ringBufferInfo.cardinality,
_endDrawId
);
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
Loading