heap.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. #pragma once
  6. #include <algorithm>
  7. #include <cstdint>
  8. #include <functional>
  9. #include "port/port.h"
  10. #include "util/autovector.h"
  11. namespace ROCKSDB_NAMESPACE {
  12. // Binary heap implementation optimized for use in multi-way merge sort.
  13. // Comparison to std::priority_queue:
  14. // - In libstdc++, std::priority_queue::pop() usually performs just over logN
  15. // comparisons but never fewer.
  16. // - std::priority_queue does not have a replace-top operation, requiring a
  17. // pop+push. If the replacement element is the new top, this requires
  18. // around 2logN comparisons.
  19. // - This heap's pop() uses a "schoolbook" downheap which requires up to ~2logN
  20. // comparisons.
  21. // - This heap provides a replace_top() operation which requires [1, 2logN]
  22. // comparisons. When the replacement element is also the new top, this
  23. // takes just 1 or 2 comparisons.
  24. //
  25. // The last property can yield an order-of-magnitude performance improvement
  26. // when merge-sorting real-world non-random data. If the merge operation is
  27. // likely to take chunks of elements from the same input stream, only 1
  28. // comparison per element is needed. In RocksDB-land, this happens when
  29. // compacting a database where keys are not randomly distributed across L0
  30. // files but nearby keys are likely to be in the same L0 file.
  31. //
  32. // The container uses the same counterintuitive ordering as
  33. // std::priority_queue: the comparison operator is expected to provide the
  34. // less-than relation, but top() will return the maximum.
  35. template <typename T, typename Compare = std::less<T>>
  36. class BinaryHeap {
  37. public:
  38. BinaryHeap() {}
  39. explicit BinaryHeap(Compare cmp) : cmp_(std::move(cmp)) {}
  40. void push(const T& value) {
  41. data_.push_back(value);
  42. upheap(data_.size() - 1);
  43. }
  44. void push(T&& value) {
  45. data_.push_back(std::move(value));
  46. upheap(data_.size() - 1);
  47. }
  48. const T& top() const {
  49. assert(!empty());
  50. return data_.front();
  51. }
  52. void replace_top(const T& value) {
  53. assert(!empty());
  54. data_.front() = value;
  55. downheap(get_root());
  56. }
  57. void replace_top(T&& value) {
  58. assert(!empty());
  59. data_.front() = std::move(value);
  60. downheap(get_root());
  61. }
  62. void pop() {
  63. assert(!empty());
  64. if (data_.size() > 1) {
  65. // Avoid self-move-assign, because it could cause problems with
  66. // classes which are not prepared for this and it trips up the
  67. // STL debugger when activated.
  68. data_.front() = std::move(data_.back());
  69. }
  70. data_.pop_back();
  71. if (!empty()) {
  72. downheap(get_root());
  73. } else {
  74. reset_root_cmp_cache();
  75. }
  76. }
  77. void swap(BinaryHeap& other) {
  78. std::swap(cmp_, other.cmp_);
  79. data_.swap(other.data_);
  80. std::swap(root_cmp_cache_, other.root_cmp_cache_);
  81. }
  82. void clear() {
  83. data_.clear();
  84. reset_root_cmp_cache();
  85. }
  86. bool empty() const { return data_.empty(); }
  87. size_t size() const { return data_.size(); }
  88. void reset_root_cmp_cache() {
  89. root_cmp_cache_ = std::numeric_limits<size_t>::max();
  90. }
  91. private:
  92. static inline size_t get_root() { return 0; }
  93. static inline size_t get_parent(size_t index) { return (index - 1) / 2; }
  94. static inline size_t get_left(size_t index) { return 2 * index + 1; }
  95. static inline size_t get_right(size_t index) { return 2 * index + 2; }
  96. void upheap(size_t index) {
  97. T v = std::move(data_[index]);
  98. while (index > get_root()) {
  99. const size_t parent = get_parent(index);
  100. if (!cmp_(data_[parent], v)) {
  101. break;
  102. }
  103. data_[index] = std::move(data_[parent]);
  104. index = parent;
  105. }
  106. data_[index] = std::move(v);
  107. reset_root_cmp_cache();
  108. }
  109. void downheap(size_t index) {
  110. T v = std::move(data_[index]);
  111. size_t picked_child = std::numeric_limits<size_t>::max();
  112. while (1) {
  113. const size_t left_child = get_left(index);
  114. if (get_left(index) >= data_.size()) {
  115. break;
  116. }
  117. const size_t right_child = left_child + 1;
  118. assert(right_child == get_right(index));
  119. picked_child = left_child;
  120. if (index == 0 && root_cmp_cache_ < data_.size()) {
  121. picked_child = root_cmp_cache_;
  122. } else if (right_child < data_.size() &&
  123. cmp_(data_[left_child], data_[right_child])) {
  124. picked_child = right_child;
  125. }
  126. if (!cmp_(v, data_[picked_child])) {
  127. break;
  128. }
  129. data_[index] = std::move(data_[picked_child]);
  130. index = picked_child;
  131. }
  132. if (index == 0) {
  133. // We did not change anything in the tree except for the value
  134. // of the root node, left and right child did not change, we can
  135. // cache that `picked_child` is the smallest child
  136. // so next time we compare againist it directly
  137. root_cmp_cache_ = picked_child;
  138. } else {
  139. // the tree changed, reset cache
  140. reset_root_cmp_cache();
  141. }
  142. data_[index] = std::move(v);
  143. }
  144. Compare cmp_;
  145. autovector<T> data_;
  146. // Used to reduce number of cmp_ calls in downheap()
  147. size_t root_cmp_cache_ = std::numeric_limits<size_t>::max();
  148. };
  149. } // namespace ROCKSDB_NAMESPACE