[libcxx-commits] [libcxx] [libc++] Optimize heap operations (PR #159917)

via libcxx-commits libcxx-commits at lists.llvm.org
Wed Oct 1 04:33:51 PDT 2025


nukyan wrote:

> Looking at the benchmark results, I don't see anything with a clear gain. Did I miss something?

I primarily cleaned up some unnecessary boundary checks in `__pop_heap()` and `__sift_up()` functions. It's not important that may no clear gain in it. Some of the results indicates that they are faster.

```
BM_MakeHeap_uint32_Random_262144                                       4.63 ns     4.43 ns
BM_MakeHeap_uint32_Ascending_262144                                    1.61 ns     1.52 ns
BM_MakeHeap_uint64_Random_262144                                       4.34 ns     4.25 ns
BM_MakeHeap_pair<uint32, uint32>_Ascending_262144                      3.42 ns     3.31 ns
BM_MakeHeap_float_Random_262144                                        4.79 ns     4.72 ns
```

Additionally, the following code snippet in `__floyd_sift_down()` optimizes for std::deque, delivering better performance while give the same performance for std::vector.

```cpp
    if (__child < __len) {
      _RandomAccessIterator __right_i = _Ops::next(__child_i);
      if (__comp(*__child_i, *__right_i)) {
        // right-child exists and is greater than left-child
        __child_i = __right_i;
        ++__child;
      }
    }
```

https://github.com/llvm/llvm-project/pull/159917


More information about the libcxx-commits mailing list