improve: Optimize BookSide::is_sorted

This commit is contained in:
John Colvin committed 2023-01-04 13:35:38 -05:00
1 parent e8fa4f23c6
commit a5459a3aff
1 file changed
+11 -3
+11 -3
View File
@@ -594,16 +594,21 @@ void BookSideT<SZ>::cover_top(std::size_t level) {
template <std::size_t SZ>
bool BookSideT<SZ>::is_sorted(Side side, bool stop_on_first_empty_level) const {
PriceComp cmp(side);
for (std::size_t i = 0; i < max_depth(); i++) {
// CAUTION: the outer loop doesn't automatically increment 'i'. This is
// an optimization due to the fact that empty levels may be skipped. Make
// sure 'i' is ALWAYS incremented in the loop body.
for (std::size_t i = 0; i < max_depth() - 1;) {
const auto &better = _book.at(i);
if (better.is_empty()) {
if (stop_on_first_empty_level) {
return true;
}
// Remember to increment 'i'
i++;
continue;
}
for (std::size_t j = i + 1; j < max_depth(); j++) {
std::size_t j = i + 1;
for (; j < max_depth(); j++) {
const auto &worse = _book.at(j);
if (worse.is_empty()) {
if (stop_on_first_empty_level) {
@@ -615,6 +620,9 @@ bool BookSideT<SZ>::is_sorted(Side side, bool stop_on_first_empty_level) const {
return false;
}
}
// Remember to increment 'i'
bts_assert(j > i);
i = j;
}
return true;
}