Avoid self-move-assign in pop operation of binary heap. (#7942)

Summary:
The current implementation of a binary heap in `util/heap.h` does a move-assign in the `pop` method. In the case that there is exactly one element stored in the heap, this ends up being a self-move-assign. This can cause trouble with certain classes, which are not prepared for this. Furthermore, it trips up the glibc STL debugger (`-D_GLIBCXX_DEBUG`), which produces an assertion failure in this case.

This PR addresses this problem by not doing the (unnecessary in this case) move-assign if there is only one element in the heap.

Pull Request resolved: https://github.com/facebook/rocksdb/pull/7942

Reviewed By: jay-zhuang

Differential Revision: D26528739

Pulled By: ajkr

fbshipit-source-id: 5ca570e0c4168f086b10308ad766dff84e6e2d03
This commit is contained in:
Max Neunhoeffer 2021-02-19 13:42:05 -08:00 committed by Facebook GitHub Bot
parent ec76f03168
commit cf14cb3e29

View File

@ -72,7 +72,12 @@ class BinaryHeap {
void pop() { void pop() {
assert(!empty()); assert(!empty());
if (data_.size() > 1) {
// Avoid self-move-assign, because it could cause problems with
// classes which are not prepared for this and it trips up the
// STL debugger when activated.
data_.front() = std::move(data_.back()); data_.front() = std::move(data_.back());
}
data_.pop_back(); data_.pop_back();
if (!empty()) { if (!empty()) {
downheap(get_root()); downheap(get_root());