Skip to content

Commit abca606

Browse files
committed
Reserve priority queue size, linear search upperBound on small levelBounds
1 parent 1bb0521 commit abca606

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

flatbush.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1561,7 +1561,15 @@ void Flatbush<ArrayType>::swap(std::vector<uint32_t>& iValues,
15611561

15621562
template <typename ArrayType>
15631563
size_t Flatbush<ArrayType>::upperBound(size_t iNodeIndex) const noexcept {
1564-
const auto& wIt = std::upper_bound(mLevelBounds.cbegin(), mLevelBounds.cend(), iNodeIndex);
1564+
static constexpr auto kSmallInput = 64UL;
1565+
decltype(mLevelBounds.cbegin()) wIt;
1566+
1567+
if (mLevelBounds.size() < kSmallInput) {
1568+
for (wIt = mLevelBounds.cbegin(); wIt != mLevelBounds.cend() && *wIt <= iNodeIndex; ++wIt);
1569+
} else {
1570+
wIt = std::upper_bound(mLevelBounds.cbegin(), mLevelBounds.cend(), iNodeIndex);
1571+
}
1572+
15651573
return (mLevelBounds.cend() == wIt) ? mLevelBounds.back() : *wIt;
15661574
}
15671575

@@ -1618,7 +1626,9 @@ std::vector<size_t> Flatbush<ArrayType>::neighbors(const Point<ArrayType>& iPoin
16181626
const auto wNumItems = numItems();
16191627
const auto wNodeSize = nodeSize();
16201628
auto wNodeIndex = mBoxes.size() - 1UL;
1621-
std::priority_queue<IndexDistance> wQueue;
1629+
std::vector<IndexDistance> wQueueStorage;
1630+
wQueueStorage.reserve(wNodeSize);
1631+
std::priority_queue<IndexDistance> wQueue(std::less<IndexDistance>(), std::move(wQueueStorage));
16221632
std::vector<size_t> wResults;
16231633
wResults.reserve(std::min(wNumItems, iMaxResults));
16241634

0 commit comments

Comments
 (0)