CircularIndex: optimize further for non-power-of-two sizes

- Improve count() for non-power-of-two sizes by handling wrap-around
- Improve full() to use always use count(), which is cheaper in both
  scenarios now
pull/288/head
Yuri D'Elia 2023-07-22 14:54:47 +02:00 committed by DRracer
parent 1979d02027
commit aaee8cab30
1 changed files with 4 additions and 10 deletions

View File

@ -28,9 +28,7 @@ public:
/// @returns true if full /// @returns true if full
inline bool full() const { inline bool full() const {
// alternative without wrap-around logic: return count() == size;
// return tail != head && mask(tail) == mask(head);
return ((index_t)(head - tail) % (size * 2)) == size;
} }
/// Reset the indexes to empty /// Reset the indexes to empty
@ -67,13 +65,9 @@ public:
if constexpr (size_is_power2) if constexpr (size_is_power2)
return head - tail; return head - tail;
else { else {
index_t i = tail; return head >= tail
index_t c = 0; ? (head - tail)
while (i != head) { : (size * 2 + head) - tail;
i = next(i);
++c;
}
return c;
} }
} }