Skip to content

Conversation

philnik777
Copy link
Contributor

@philnik777 philnik777 commented Aug 18, 2025

Apple M4:
-----------------------------------------------------------------------------------------------------
Benchmark                                                                         old             new
-----------------------------------------------------------------------------------------------------
BM_MakeHeap_uint32_Random_1                                                  0.285 ns        0.271 ns
BM_MakeHeap_uint32_Random_4                                                   2.09 ns         1.80 ns
BM_MakeHeap_uint32_Random_16                                                  1.85 ns         1.83 ns
BM_MakeHeap_uint32_Random_64                                                  1.92 ns         1.50 ns
BM_MakeHeap_uint32_Random_256                                                 2.10 ns         1.87 ns
BM_MakeHeap_uint32_Random_1024                                                1.73 ns         1.86 ns
BM_MakeHeap_uint32_Random_16384                                               2.17 ns         2.05 ns
BM_MakeHeap_uint32_Random_262144                                              1.77 ns         1.77 ns
BM_MakeHeap_uint32_Ascending_1                                               0.288 ns        0.277 ns
BM_MakeHeap_uint32_Ascending_4                                               0.658 ns        0.481 ns
BM_MakeHeap_uint32_Ascending_16                                              0.636 ns        0.637 ns
BM_MakeHeap_uint32_Ascending_64                                              0.643 ns        0.601 ns
BM_MakeHeap_uint32_Ascending_256                                             0.710 ns        0.636 ns
BM_MakeHeap_uint32_Ascending_1024                                            0.747 ns        0.660 ns
BM_MakeHeap_uint32_Ascending_16384                                           0.713 ns        0.633 ns
BM_MakeHeap_uint32_Ascending_262144                                          0.769 ns        0.731 ns
BM_MakeHeap_uint32_Descending_1                                              0.294 ns        0.280 ns
BM_MakeHeap_uint32_Descending_4                                              0.379 ns        0.305 ns
BM_MakeHeap_uint32_Descending_16                                             0.376 ns        0.268 ns
BM_MakeHeap_uint32_Descending_64                                             0.358 ns        0.271 ns
BM_MakeHeap_uint32_Descending_256                                            0.377 ns        0.284 ns
BM_MakeHeap_uint32_Descending_1024                                           0.355 ns        0.267 ns
BM_MakeHeap_uint32_Descending_16384                                          0.348 ns        0.248 ns
BM_MakeHeap_uint32_Descending_262144                                         0.349 ns        0.247 ns
BM_MakeHeap_uint32_SingleElement_1                                           0.292 ns        0.280 ns
BM_MakeHeap_uint32_SingleElement_4                                           0.570 ns        0.332 ns
BM_MakeHeap_uint32_SingleElement_16                                          0.635 ns        0.604 ns
BM_MakeHeap_uint32_SingleElement_64                                          0.653 ns        0.567 ns
BM_MakeHeap_uint32_SingleElement_256                                         0.703 ns        0.609 ns
BM_MakeHeap_uint32_SingleElement_1024                                        0.737 ns        0.604 ns
BM_MakeHeap_uint32_SingleElement_16384                                       0.699 ns        0.574 ns
BM_MakeHeap_uint32_SingleElement_262144                                      0.803 ns        0.684 ns
BM_MakeHeap_uint32_PipeOrgan_1                                               0.291 ns        0.284 ns
BM_MakeHeap_uint32_PipeOrgan_4                                               0.588 ns        0.399 ns
BM_MakeHeap_uint32_PipeOrgan_16                                              0.648 ns         1.12 ns
BM_MakeHeap_uint32_PipeOrgan_64                                              0.662 ns        0.771 ns
BM_MakeHeap_uint32_PipeOrgan_256                                             0.723 ns        0.672 ns
BM_MakeHeap_uint32_PipeOrgan_1024                                            0.749 ns        0.674 ns
BM_MakeHeap_uint32_PipeOrgan_16384                                           0.708 ns        0.638 ns
BM_MakeHeap_uint32_PipeOrgan_262144                                          0.786 ns        0.743 ns
BM_MakeHeap_uint32_Heap_1                                                    0.298 ns        0.282 ns
BM_MakeHeap_uint32_Heap_4                                                    0.396 ns        0.308 ns
BM_MakeHeap_uint32_Heap_16                                                   0.377 ns        0.268 ns
BM_MakeHeap_uint32_Heap_64                                                   0.356 ns        0.271 ns
BM_MakeHeap_uint32_Heap_256                                                  0.378 ns        0.290 ns
BM_MakeHeap_uint32_Heap_1024                                                 0.356 ns        0.275 ns
BM_MakeHeap_uint32_Heap_16384                                                0.348 ns        0.252 ns
BM_MakeHeap_uint32_Heap_262144                                               0.347 ns        0.250 ns
BM_MakeHeap_uint32_QuickSortAdversary_1                                      0.290 ns        0.284 ns
BM_MakeHeap_uint32_QuickSortAdversary_4                                      0.627 ns        0.409 ns
BM_MakeHeap_uint32_QuickSortAdversary_16                                     0.640 ns        0.653 ns
BM_MakeHeap_uint32_QuickSortAdversary_64                                     0.577 ns        0.484 ns
BM_MakeHeap_uint32_QuickSortAdversary_256                                    0.613 ns        0.521 ns
BM_MakeHeap_uint32_QuickSortAdversary_1024                                   0.652 ns        0.514 ns
BM_MakeHeap_uint32_QuickSortAdversary_16384                                  0.428 ns        0.308 ns
BM_MakeHeap_uint32_QuickSortAdversary_262144                                 0.373 ns        0.261 ns
BM_MakeHeap_uint64_Random_1                                                  0.291 ns        0.281 ns
BM_MakeHeap_uint64_Random_4                                                   2.20 ns         1.97 ns
BM_MakeHeap_uint64_Random_16                                                  1.93 ns         1.70 ns
BM_MakeHeap_uint64_Random_64                                                  1.89 ns         1.48 ns
BM_MakeHeap_uint64_Random_256                                                 2.10 ns         1.99 ns
BM_MakeHeap_uint64_Random_1024                                                1.41 ns         2.04 ns
BM_MakeHeap_uint64_Random_16384                                               2.12 ns         1.83 ns
BM_MakeHeap_uint64_Random_262144                                              1.83 ns         1.63 ns
BM_MakeHeap_uint64_Ascending_1                                               0.288 ns        0.283 ns
BM_MakeHeap_uint64_Ascending_4                                               0.624 ns        0.493 ns
BM_MakeHeap_uint64_Ascending_16                                              0.648 ns        0.688 ns
BM_MakeHeap_uint64_Ascending_64                                              0.671 ns        0.634 ns
BM_MakeHeap_uint64_Ascending_256                                             0.739 ns        0.680 ns
BM_MakeHeap_uint64_Ascending_1024                                            0.761 ns        0.698 ns
BM_MakeHeap_uint64_Ascending_16384                                           0.740 ns        0.685 ns
BM_MakeHeap_uint64_Ascending_262144                                          0.849 ns        0.837 ns
BM_MakeHeap_uint64_Descending_1                                              0.304 ns        0.287 ns
BM_MakeHeap_uint64_Descending_4                                              0.395 ns        0.312 ns
BM_MakeHeap_uint64_Descending_16                                             0.374 ns        0.276 ns
BM_MakeHeap_uint64_Descending_64                                             0.358 ns        0.272 ns
BM_MakeHeap_uint64_Descending_256                                            0.389 ns        0.303 ns
BM_MakeHeap_uint64_Descending_1024                                           0.361 ns        0.278 ns
BM_MakeHeap_uint64_Descending_16384                                          0.352 ns        0.253 ns
BM_MakeHeap_uint64_Descending_262144                                         0.350 ns        0.251 ns
BM_MakeHeap_uint64_SingleElement_1                                           0.295 ns        0.285 ns
BM_MakeHeap_uint64_SingleElement_4                                           0.569 ns        0.358 ns
BM_MakeHeap_uint64_SingleElement_16                                          0.649 ns        0.652 ns
BM_MakeHeap_uint64_SingleElement_64                                          0.673 ns        0.565 ns
BM_MakeHeap_uint64_SingleElement_256                                         0.732 ns        0.651 ns
BM_MakeHeap_uint64_SingleElement_1024                                        0.759 ns        0.632 ns
BM_MakeHeap_uint64_SingleElement_16384                                       0.748 ns        0.614 ns
BM_MakeHeap_uint64_SingleElement_262144                                      0.947 ns        0.797 ns
BM_MakeHeap_uint64_PipeOrgan_1                                               0.295 ns        0.284 ns
BM_MakeHeap_uint64_PipeOrgan_4                                               0.601 ns        0.496 ns
BM_MakeHeap_uint64_PipeOrgan_16                                              0.655 ns         1.18 ns
BM_MakeHeap_uint64_PipeOrgan_64                                              0.682 ns        0.803 ns
BM_MakeHeap_uint64_PipeOrgan_256                                             0.759 ns        0.710 ns
BM_MakeHeap_uint64_PipeOrgan_1024                                            0.759 ns        0.713 ns
BM_MakeHeap_uint64_PipeOrgan_16384                                           0.739 ns        0.696 ns
BM_MakeHeap_uint64_PipeOrgan_262144                                          0.870 ns        0.849 ns
BM_MakeHeap_uint64_Heap_1                                                    0.290 ns        0.284 ns
BM_MakeHeap_uint64_Heap_4                                                    0.413 ns        0.314 ns
BM_MakeHeap_uint64_Heap_16                                                   0.378 ns        0.277 ns
BM_MakeHeap_uint64_Heap_64                                                   0.362 ns        0.272 ns
BM_MakeHeap_uint64_Heap_256                                                  0.389 ns        0.303 ns
BM_MakeHeap_uint64_Heap_1024                                                 0.362 ns        0.283 ns
BM_MakeHeap_uint64_Heap_16384                                                0.352 ns        0.253 ns
BM_MakeHeap_uint64_Heap_262144                                               0.350 ns        0.251 ns
BM_MakeHeap_uint64_QuickSortAdversary_1                                      0.293 ns        0.284 ns
BM_MakeHeap_uint64_QuickSortAdversary_4                                      0.606 ns        0.494 ns
BM_MakeHeap_uint64_QuickSortAdversary_16                                     0.645 ns        0.691 ns
BM_MakeHeap_uint64_QuickSortAdversary_64                                     0.607 ns        0.511 ns
BM_MakeHeap_uint64_QuickSortAdversary_256                                    0.641 ns        0.537 ns
BM_MakeHeap_uint64_QuickSortAdversary_1024                                   0.657 ns        0.529 ns
BM_MakeHeap_uint64_QuickSortAdversary_16384                                  0.435 ns        0.316 ns
BM_MakeHeap_uint64_QuickSortAdversary_262144                                 0.382 ns        0.266 ns
BM_MakeHeap_pair<uint32, uint32>_Random_1                                    0.297 ns        0.378 ns
BM_MakeHeap_pair<uint32, uint32>_Random_4                                     2.80 ns        0.765 ns
BM_MakeHeap_pair<uint32, uint32>_Random_16                                    2.92 ns         2.20 ns
BM_MakeHeap_pair<uint32, uint32>_Random_64                                    3.17 ns         3.64 ns
BM_MakeHeap_pair<uint32, uint32>_Random_256                                   3.44 ns         3.20 ns
BM_MakeHeap_pair<uint32, uint32>_Random_1024                                  3.35 ns         3.64 ns
BM_MakeHeap_pair<uint32, uint32>_Random_16384                                 3.22 ns         3.50 ns
BM_MakeHeap_pair<uint32, uint32>_Random_262144                                3.61 ns         3.46 ns
BM_MakeHeap_pair<uint32, uint32>_Ascending_1                                 0.291 ns        0.379 ns
BM_MakeHeap_pair<uint32, uint32>_Ascending_4                                 0.779 ns        0.436 ns
BM_MakeHeap_pair<uint32, uint32>_Ascending_16                                0.943 ns         1.01 ns
BM_MakeHeap_pair<uint32, uint32>_Ascending_64                                 1.17 ns         1.26 ns
BM_MakeHeap_pair<uint32, uint32>_Ascending_256                                1.38 ns         1.44 ns
BM_MakeHeap_pair<uint32, uint32>_Ascending_1024                               1.37 ns         1.43 ns
BM_MakeHeap_pair<uint32, uint32>_Ascending_16384                              1.29 ns         1.31 ns
BM_MakeHeap_pair<uint32, uint32>_Ascending_262144                             1.37 ns         1.40 ns
BM_MakeHeap_pair<uint32, uint32>_Descending_1                                0.292 ns        0.382 ns
BM_MakeHeap_pair<uint32, uint32>_Descending_4                                0.440 ns        0.347 ns
BM_MakeHeap_pair<uint32, uint32>_Descending_16                               0.529 ns        0.520 ns
BM_MakeHeap_pair<uint32, uint32>_Descending_64                               0.540 ns        0.527 ns
BM_MakeHeap_pair<uint32, uint32>_Descending_256                              0.637 ns        0.583 ns
BM_MakeHeap_pair<uint32, uint32>_Descending_1024                             0.552 ns        0.513 ns
BM_MakeHeap_pair<uint32, uint32>_Descending_16384                            0.522 ns        0.488 ns
BM_MakeHeap_pair<uint32, uint32>_Descending_262144                           0.515 ns        0.492 ns
BM_MakeHeap_pair<uint32, uint32>_SingleElement_1                             0.299 ns        0.377 ns
BM_MakeHeap_pair<uint32, uint32>_SingleElement_4                             0.787 ns        0.474 ns
BM_MakeHeap_pair<uint32, uint32>_SingleElement_16                             1.07 ns        0.921 ns
BM_MakeHeap_pair<uint32, uint32>_SingleElement_64                             1.20 ns         1.15 ns
BM_MakeHeap_pair<uint32, uint32>_SingleElement_256                            1.30 ns         1.27 ns
BM_MakeHeap_pair<uint32, uint32>_SingleElement_1024                           1.30 ns         1.31 ns
BM_MakeHeap_pair<uint32, uint32>_SingleElement_16384                          1.29 ns         1.28 ns
BM_MakeHeap_pair<uint32, uint32>_SingleElement_262144                         1.37 ns         1.38 ns
BM_MakeHeap_pair<uint32, uint32>_PipeOrgan_1                                 0.293 ns        0.385 ns
BM_MakeHeap_pair<uint32, uint32>_PipeOrgan_4                                 0.677 ns        0.438 ns
BM_MakeHeap_pair<uint32, uint32>_PipeOrgan_16                                 1.04 ns         1.00 ns
BM_MakeHeap_pair<uint32, uint32>_PipeOrgan_64                                 1.20 ns         1.27 ns
BM_MakeHeap_pair<uint32, uint32>_PipeOrgan_256                                1.40 ns         1.43 ns
BM_MakeHeap_pair<uint32, uint32>_PipeOrgan_1024                               1.36 ns         1.43 ns
BM_MakeHeap_pair<uint32, uint32>_PipeOrgan_16384                              1.27 ns         1.31 ns
BM_MakeHeap_pair<uint32, uint32>_PipeOrgan_262144                             1.37 ns         1.41 ns
BM_MakeHeap_pair<uint32, uint32>_Heap_1                                      0.292 ns        0.378 ns
BM_MakeHeap_pair<uint32, uint32>_Heap_4                                      0.440 ns        0.380 ns
BM_MakeHeap_pair<uint32, uint32>_Heap_16                                     0.560 ns        0.606 ns
BM_MakeHeap_pair<uint32, uint32>_Heap_64                                     0.588 ns        0.573 ns
BM_MakeHeap_pair<uint32, uint32>_Heap_256                                    0.632 ns        0.607 ns
BM_MakeHeap_pair<uint32, uint32>_Heap_1024                                   0.598 ns        0.580 ns
BM_MakeHeap_pair<uint32, uint32>_Heap_16384                                  0.576 ns        0.563 ns
BM_MakeHeap_pair<uint32, uint32>_Heap_262144                                 0.572 ns        0.561 ns
BM_MakeHeap_pair<uint32, uint32>_QuickSortAdversary_1                        0.304 ns        0.379 ns
BM_MakeHeap_pair<uint32, uint32>_QuickSortAdversary_4                        0.823 ns        0.430 ns
BM_MakeHeap_pair<uint32, uint32>_QuickSortAdversary_16                        1.08 ns         1.03 ns
BM_MakeHeap_pair<uint32, uint32>_QuickSortAdversary_64                        1.18 ns         1.23 ns
BM_MakeHeap_pair<uint32, uint32>_QuickSortAdversary_256                       1.39 ns         1.43 ns
BM_MakeHeap_pair<uint32, uint32>_QuickSortAdversary_1024                      1.36 ns         1.42 ns
BM_MakeHeap_pair<uint32, uint32>_QuickSortAdversary_16384                     1.28 ns         1.32 ns
BM_MakeHeap_pair<uint32, uint32>_QuickSortAdversary_262144                    1.34 ns         1.37 ns
BM_MakeHeap_tuple<uint32, uint64, uint32>_Random_1                           0.276 ns        0.511 ns
BM_MakeHeap_tuple<uint32, uint64, uint32>_Random_4                            4.25 ns         1.96 ns
BM_MakeHeap_tuple<uint32, uint64, uint32>_Random_16                           4.84 ns         3.77 ns
BM_MakeHeap_tuple<uint32, uint64, uint32>_Random_64                           5.53 ns         4.93 ns
BM_MakeHeap_tuple<uint32, uint64, uint32>_Random_256                          5.30 ns         5.06 ns
BM_MakeHeap_tuple<uint32, uint64, uint32>_Random_1024                         5.29 ns         5.02 ns
BM_MakeHeap_tuple<uint32, uint64, uint32>_Random_16384                        5.53 ns         5.31 ns
BM_MakeHeap_tuple<uint32, uint64, uint32>_Random_262144                       5.49 ns         5.29 ns
BM_MakeHeap_tuple<uint32, uint64, uint32>_Ascending_1                        0.275 ns        0.443 ns
BM_MakeHeap_tuple<uint32, uint64, uint32>_Ascending_4                         1.22 ns        0.764 ns
BM_MakeHeap_tuple<uint32, uint64, uint32>_Ascending_16                        1.39 ns         1.49 ns
BM_MakeHeap_tuple<uint32, uint64, uint32>_Ascending_64                        1.66 ns         1.76 ns
BM_MakeHeap_tuple<uint32, uint64, uint32>_Ascending_256                       1.85 ns         1.99 ns
BM_MakeHeap_tuple<uint32, uint64, uint32>_Ascending_1024                      1.90 ns         2.02 ns
BM_MakeHeap_tuple<uint32, uint64, uint32>_Ascending_16384                     2.15 ns         2.27 ns
BM_MakeHeap_tuple<uint32, uint64, uint32>_Ascending_262144                    2.25 ns         2.37 ns
BM_MakeHeap_tuple<uint32, uint64, uint32>_Descending_1                       0.274 ns        0.445 ns
BM_MakeHeap_tuple<uint32, uint64, uint32>_Descending_4                       0.939 ns        0.520 ns
BM_MakeHeap_tuple<uint32, uint64, uint32>_Descending_16                       1.02 ns        0.811 ns
BM_MakeHeap_tuple<uint32, uint64, uint32>_Descending_64                       1.01 ns        0.941 ns
BM_MakeHeap_tuple<uint32, uint64, uint32>_Descending_256                      1.03 ns         1.01 ns
BM_MakeHeap_tuple<uint32, uint64, uint32>_Descending_1024                    0.969 ns        0.947 ns
BM_MakeHeap_tuple<uint32, uint64, uint32>_Descending_16384                   0.882 ns        0.876 ns
BM_MakeHeap_tuple<uint32, uint64, uint32>_Descending_262144                  0.893 ns        0.871 ns
BM_MakeHeap_tuple<uint32, uint64, uint32>_SingleElement_1                    0.276 ns        0.443 ns
BM_MakeHeap_tuple<uint32, uint64, uint32>_SingleElement_4                     1.34 ns        0.870 ns
BM_MakeHeap_tuple<uint32, uint64, uint32>_SingleElement_16                    1.60 ns         1.59 ns
BM_MakeHeap_tuple<uint32, uint64, uint32>_SingleElement_64                    1.91 ns         2.00 ns
BM_MakeHeap_tuple<uint32, uint64, uint32>_SingleElement_256                   1.91 ns         2.08 ns
BM_MakeHeap_tuple<uint32, uint64, uint32>_SingleElement_1024                  1.91 ns         2.10 ns
BM_MakeHeap_tuple<uint32, uint64, uint32>_SingleElement_16384                 2.13 ns         2.35 ns
BM_MakeHeap_tuple<uint32, uint64, uint32>_SingleElement_262144                2.25 ns         2.48 ns
BM_MakeHeap_tuple<uint32, uint64, uint32>_PipeOrgan_1                        0.275 ns        0.446 ns
BM_MakeHeap_tuple<uint32, uint64, uint32>_PipeOrgan_4                         1.07 ns        0.671 ns
BM_MakeHeap_tuple<uint32, uint64, uint32>_PipeOrgan_16                        1.36 ns         1.44 ns
BM_MakeHeap_tuple<uint32, uint64, uint32>_PipeOrgan_64                        1.70 ns         1.80 ns
BM_MakeHeap_tuple<uint32, uint64, uint32>_PipeOrgan_256                       1.88 ns         2.05 ns
BM_MakeHeap_tuple<uint32, uint64, uint32>_PipeOrgan_1024                      1.92 ns         2.06 ns
BM_MakeHeap_tuple<uint32, uint64, uint32>_PipeOrgan_16384                     2.11 ns         2.26 ns
BM_MakeHeap_tuple<uint32, uint64, uint32>_PipeOrgan_262144                    2.18 ns         2.34 ns
BM_MakeHeap_tuple<uint32, uint64, uint32>_Heap_1                             0.274 ns        0.441 ns
BM_MakeHeap_tuple<uint32, uint64, uint32>_Heap_4                             0.938 ns        0.587 ns
BM_MakeHeap_tuple<uint32, uint64, uint32>_Heap_16                             1.01 ns        0.873 ns
BM_MakeHeap_tuple<uint32, uint64, uint32>_Heap_64                             1.08 ns         1.00 ns
BM_MakeHeap_tuple<uint32, uint64, uint32>_Heap_256                            1.21 ns         1.18 ns
BM_MakeHeap_tuple<uint32, uint64, uint32>_Heap_1024                           1.37 ns         1.29 ns
BM_MakeHeap_tuple<uint32, uint64, uint32>_Heap_16384                          1.31 ns         1.26 ns
BM_MakeHeap_tuple<uint32, uint64, uint32>_Heap_262144                         1.29 ns         1.25 ns
BM_MakeHeap_tuple<uint32, uint64, uint32>_QuickSortAdversary_1               0.275 ns        0.447 ns
BM_MakeHeap_tuple<uint32, uint64, uint32>_QuickSortAdversary_4                1.22 ns        0.764 ns
BM_MakeHeap_tuple<uint32, uint64, uint32>_QuickSortAdversary_16               1.35 ns         1.46 ns
BM_MakeHeap_tuple<uint32, uint64, uint32>_QuickSortAdversary_64               1.63 ns         1.73 ns
BM_MakeHeap_tuple<uint32, uint64, uint32>_QuickSortAdversary_256              1.83 ns         1.87 ns
BM_MakeHeap_tuple<uint32, uint64, uint32>_QuickSortAdversary_1024             1.81 ns         1.94 ns
BM_MakeHeap_tuple<uint32, uint64, uint32>_QuickSortAdversary_16384            2.06 ns         2.19 ns
BM_MakeHeap_tuple<uint32, uint64, uint32>_QuickSortAdversary_262144           2.14 ns         2.31 ns
BM_MakeHeap_string_Random_1                                                  0.289 ns        0.446 ns
BM_MakeHeap_string_Random_4                                                   7.43 ns         4.84 ns
BM_MakeHeap_string_Random_16                                                  9.73 ns         8.92 ns
BM_MakeHeap_string_Random_64                                                  11.5 ns         11.5 ns
BM_MakeHeap_string_Random_256                                                 12.6 ns         12.4 ns
BM_MakeHeap_string_Random_1024                                                13.2 ns         13.2 ns
BM_MakeHeap_string_Random_16384                                               15.4 ns         15.4 ns
BM_MakeHeap_string_Random_262144                                              21.4 ns         21.5 ns
BM_MakeHeap_string_Ascending_1                                               0.287 ns        0.447 ns
BM_MakeHeap_string_Ascending_4                                                3.22 ns         2.44 ns
BM_MakeHeap_string_Ascending_16                                               3.77 ns         3.62 ns
BM_MakeHeap_string_Ascending_64                                               4.84 ns         5.17 ns
BM_MakeHeap_string_Ascending_256                                              5.79 ns         6.04 ns
BM_MakeHeap_string_Ascending_1024                                             5.93 ns         6.60 ns
BM_MakeHeap_string_Ascending_16384                                            6.84 ns         7.25 ns
BM_MakeHeap_string_Ascending_262144                                           13.5 ns         14.3 ns
BM_MakeHeap_string_Descending_1                                              0.293 ns        0.447 ns
BM_MakeHeap_string_Descending_4                                               2.61 ns         1.83 ns
BM_MakeHeap_string_Descending_16                                              2.64 ns         2.60 ns
BM_MakeHeap_string_Descending_64                                              2.75 ns         2.74 ns
BM_MakeHeap_string_Descending_256                                             3.78 ns         3.57 ns
BM_MakeHeap_string_Descending_1024                                            3.20 ns         3.51 ns
BM_MakeHeap_string_Descending_16384                                           3.57 ns         3.85 ns
BM_MakeHeap_string_Descending_262144                                          6.27 ns         6.39 ns
BM_MakeHeap_string_SingleElement_1                                           0.291 ns        0.448 ns
BM_MakeHeap_string_SingleElement_4                                            3.88 ns         2.71 ns
BM_MakeHeap_string_SingleElement_16                                           5.08 ns         4.96 ns
BM_MakeHeap_string_SingleElement_64                                           6.14 ns         6.29 ns
BM_MakeHeap_string_SingleElement_256                                          6.13 ns         6.46 ns
BM_MakeHeap_string_SingleElement_1024                                         5.98 ns         6.60 ns
BM_MakeHeap_string_SingleElement_16384                                        5.88 ns         6.39 ns
BM_MakeHeap_string_SingleElement_262144                                       9.95 ns         10.2 ns
BM_MakeHeap_string_PipeOrgan_1                                               0.292 ns        0.447 ns
BM_MakeHeap_string_PipeOrgan_4                                                2.97 ns         2.51 ns
BM_MakeHeap_string_PipeOrgan_16                                               3.76 ns         3.91 ns
BM_MakeHeap_string_PipeOrgan_64                                               4.89 ns         5.20 ns
BM_MakeHeap_string_PipeOrgan_256                                              5.77 ns         6.09 ns
BM_MakeHeap_string_PipeOrgan_1024                                             6.14 ns         6.40 ns
BM_MakeHeap_string_PipeOrgan_16384                                            6.83 ns         7.32 ns
BM_MakeHeap_string_PipeOrgan_262144                                           13.8 ns         14.6 ns
BM_MakeHeap_string_Heap_1                                                    0.288 ns        0.515 ns
BM_MakeHeap_string_Heap_4                                                     3.62 ns         4.20 ns
BM_MakeHeap_string_Heap_16                                                    5.36 ns         5.23 ns
BM_MakeHeap_string_Heap_64                                                    5.79 ns         5.38 ns
BM_MakeHeap_string_Heap_256                                                   5.70 ns         5.40 ns
BM_MakeHeap_string_Heap_1024                                                  5.78 ns         5.37 ns
BM_MakeHeap_string_Heap_16384                                                 6.09 ns         5.67 ns
BM_MakeHeap_string_Heap_262144                                                6.37 ns         5.96 ns
BM_MakeHeap_string_QuickSortAdversary_1                                      0.282 ns        0.448 ns
BM_MakeHeap_string_QuickSortAdversary_4                                       7.45 ns         5.60 ns
BM_MakeHeap_string_QuickSortAdversary_16                                      9.76 ns         8.85 ns
BM_MakeHeap_string_QuickSortAdversary_64                                      11.5 ns         11.2 ns
BM_MakeHeap_string_QuickSortAdversary_256                                     12.0 ns         11.8 ns
BM_MakeHeap_string_QuickSortAdversary_1024                                    12.2 ns         12.0 ns
BM_MakeHeap_string_QuickSortAdversary_16384                                   13.7 ns         13.6 ns
BM_MakeHeap_string_QuickSortAdversary_262144                                  14.1 ns         14.8 ns
BM_MakeHeap_float_Random_1                                                   0.287 ns        0.287 ns
BM_MakeHeap_float_Random_4                                                    2.29 ns         2.60 ns
BM_MakeHeap_float_Random_16                                                   4.00 ns         2.48 ns
BM_MakeHeap_float_Random_64                                                   4.41 ns         1.92 ns
BM_MakeHeap_float_Random_256                                                  4.73 ns         2.05 ns
BM_MakeHeap_float_Random_1024                                                 4.90 ns         2.27 ns
BM_MakeHeap_float_Random_16384                                                4.42 ns         2.27 ns
BM_MakeHeap_float_Random_262144                                               4.72 ns         1.39 ns
BM_MakeHeap_float_Ascending_1                                                0.291 ns        0.293 ns
BM_MakeHeap_float_Ascending_4                                                0.633 ns        0.428 ns
BM_MakeHeap_float_Ascending_16                                               0.638 ns        0.874 ns
BM_MakeHeap_float_Ascending_64                                               0.614 ns        0.698 ns
BM_MakeHeap_float_Ascending_256                                              0.663 ns        0.713 ns
BM_MakeHeap_float_Ascending_1024                                             0.660 ns        0.761 ns
BM_MakeHeap_float_Ascending_16384                                            0.628 ns        0.725 ns
BM_MakeHeap_float_Ascending_262144                                           0.629 ns        0.814 ns
BM_MakeHeap_float_Descending_1                                               0.290 ns        0.290 ns
BM_MakeHeap_float_Descending_4                                               0.421 ns        0.316 ns
BM_MakeHeap_float_Descending_16                                              0.302 ns        0.225 ns
BM_MakeHeap_float_Descending_64                                              0.293 ns        0.212 ns
BM_MakeHeap_float_Descending_256                                             0.314 ns        0.246 ns
BM_MakeHeap_float_Descending_1024                                            0.300 ns        0.231 ns
BM_MakeHeap_float_Descending_16384                                           0.308 ns        0.205 ns
BM_MakeHeap_float_Descending_262144                                          0.309 ns        0.203 ns
BM_MakeHeap_float_SingleElement_1                                            0.289 ns        0.292 ns
BM_MakeHeap_float_SingleElement_4                                            0.569 ns        0.347 ns
BM_MakeHeap_float_SingleElement_16                                           0.538 ns        0.825 ns
BM_MakeHeap_float_SingleElement_64                                           0.585 ns        0.727 ns
BM_MakeHeap_float_SingleElement_256                                          0.603 ns        0.708 ns
BM_MakeHeap_float_SingleElement_1024                                         0.618 ns        0.760 ns
BM_MakeHeap_float_SingleElement_16384                                        0.599 ns        0.726 ns
BM_MakeHeap_float_SingleElement_262144                                       0.723 ns        0.820 ns
BM_MakeHeap_float_PipeOrgan_1                                                0.289 ns        0.291 ns
BM_MakeHeap_float_PipeOrgan_4                                                0.457 ns        0.420 ns
BM_MakeHeap_float_PipeOrgan_16                                               0.670 ns         1.32 ns
BM_MakeHeap_float_PipeOrgan_64                                               0.764 ns        0.889 ns
BM_MakeHeap_float_PipeOrgan_256                                              0.793 ns        0.757 ns
BM_MakeHeap_float_PipeOrgan_1024                                             0.755 ns        0.764 ns
BM_MakeHeap_float_PipeOrgan_16384                                            0.723 ns        0.723 ns
BM_MakeHeap_float_PipeOrgan_262144                                           0.654 ns        0.817 ns
BM_MakeHeap_float_Heap_1                                                     0.291 ns        0.289 ns
BM_MakeHeap_float_Heap_4                                                     0.388 ns        0.316 ns
BM_MakeHeap_float_Heap_16                                                    0.317 ns        0.225 ns
BM_MakeHeap_float_Heap_64                                                    0.353 ns        0.213 ns
BM_MakeHeap_float_Heap_256                                                   0.361 ns        0.246 ns
BM_MakeHeap_float_Heap_1024                                                  0.381 ns        0.233 ns
BM_MakeHeap_float_Heap_16384                                                 0.390 ns        0.205 ns
BM_MakeHeap_float_Heap_262144                                                0.379 ns        0.202 ns
BM_MakeHeap_float_QuickSortAdversary_1                                       0.295 ns        0.289 ns
BM_MakeHeap_float_QuickSortAdversary_4                                       0.640 ns        0.422 ns
BM_MakeHeap_float_QuickSortAdversary_16                                      0.658 ns        0.871 ns
BM_MakeHeap_float_QuickSortAdversary_64                                      0.574 ns        0.659 ns
BM_MakeHeap_float_QuickSortAdversary_256                                     0.631 ns        0.550 ns
BM_MakeHeap_float_QuickSortAdversary_1024                                    0.617 ns        0.552 ns
BM_MakeHeap_float_QuickSortAdversary_16384                                   0.424 ns        0.283 ns
BM_MakeHeap_float_QuickSortAdversary_262144                                  0.386 ns        0.219 ns

Fixes #120752

Copy link

github-actions bot commented Aug 18, 2025

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
git-clang-format --diff origin/main HEAD --extensions cpp,h -- libcxx/include/__algorithm/make_heap.h libcxx/include/__algorithm/partial_sort.h libcxx/include/__algorithm/partial_sort_copy.h libcxx/include/__algorithm/sift_down.h libcxx/test/libcxx/algorithms/bad_iterator_traits.verify.cpp

⚠️
The reproduction instructions above might return results for more than one PR
in a stack if you are using a stacked PR workflow. You can limit the results by
changing origin/main to the base branch/commit you want to compare against.
⚠️

View the diff from clang-format here.
diff --git a/libcxx/include/__algorithm/make_heap.h b/libcxx/include/__algorithm/make_heap.h
index 46fab2039..8cfeda2b5 100644
--- a/libcxx/include/__algorithm/make_heap.h
+++ b/libcxx/include/__algorithm/make_heap.h
@@ -33,8 +33,8 @@ inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void
 __make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare&& __comp) {
   __comp_ref_type<_Compare> __comp_ref = __comp;
 
-  using __diff_t = __iter_diff_t<_RandomAccessIterator>;
-  const __diff_t __n   = __last - __first;
+  using __diff_t     = __iter_diff_t<_RandomAccessIterator>;
+  const __diff_t __n = __last - __first;
 
   static const bool __assume_both_children = is_arithmetic<__iter_value_type<_RandomAccessIterator> >::value;
 

@ldionne ldionne marked this pull request as ready for review August 25, 2025 16:48
@ldionne ldionne requested a review from a team as a code owner August 25, 2025 16:48
@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Aug 25, 2025
@llvmbot
Copy link
Member

llvmbot commented Aug 25, 2025

@llvm/pr-subscribers-libcxx

Author: Nikolas Klauser (philnik777)

Changes
----------------------------------------------------------------------------------------------------
Benchmark                                                                        old             new
----------------------------------------------------------------------------------------------------
BM_MakeHeap_uint32_Random_1                                                  1.02 ns         1.07 ns
BM_MakeHeap_uint32_Random_4                                                  3.35 ns         2.87 ns
BM_MakeHeap_uint32_Random_16                                                 3.11 ns         2.68 ns
BM_MakeHeap_uint32_Random_64                                                 3.24 ns         2.85 ns
BM_MakeHeap_uint32_Random_256                                                3.10 ns         2.77 ns
BM_MakeHeap_uint32_Random_1024                                               3.03 ns         2.63 ns
BM_MakeHeap_uint32_Random_16384                                              3.04 ns         2.63 ns
BM_MakeHeap_uint32_Random_262144                                             3.06 ns         2.65 ns
BM_MakeHeap_uint32_Ascending_1                                               1.04 ns         1.07 ns
BM_MakeHeap_uint32_Ascending_4                                               1.30 ns        0.879 ns
BM_MakeHeap_uint32_Ascending_16                                              1.23 ns        0.958 ns
BM_MakeHeap_uint32_Ascending_64                                              1.38 ns        0.879 ns
BM_MakeHeap_uint32_Ascending_256                                             1.59 ns        0.933 ns
BM_MakeHeap_uint32_Ascending_1024                                            1.54 ns        0.968 ns
BM_MakeHeap_uint32_Ascending_16384                                           1.58 ns         1.01 ns
BM_MakeHeap_uint32_Ascending_262144                                          1.69 ns         1.11 ns
BM_MakeHeap_uint32_Descending_1                                              1.07 ns         1.05 ns
BM_MakeHeap_uint32_Descending_4                                             0.809 ns        0.549 ns
BM_MakeHeap_uint32_Descending_16                                            0.705 ns        0.520 ns
BM_MakeHeap_uint32_Descending_64                                            0.692 ns        0.405 ns
BM_MakeHeap_uint32_Descending_256                                           0.729 ns        0.429 ns
BM_MakeHeap_uint32_Descending_1024                                          0.682 ns        0.378 ns
BM_MakeHeap_uint32_Descending_16384                                         0.666 ns        0.362 ns
BM_MakeHeap_uint32_Descending_262144                                        0.667 ns        0.361 ns
BM_MakeHeap_uint32_SingleElement_1                                           1.07 ns         1.10 ns
BM_MakeHeap_uint32_SingleElement_4                                           1.30 ns        0.666 ns
BM_MakeHeap_uint32_SingleElement_16                                          1.35 ns        0.861 ns
BM_MakeHeap_uint32_SingleElement_64                                          1.46 ns        0.827 ns
BM_MakeHeap_uint32_SingleElement_256                                         1.60 ns        0.896 ns
BM_MakeHeap_uint32_SingleElement_1024                                        1.55 ns        0.942 ns
BM_MakeHeap_uint32_SingleElement_16384                                       1.59 ns        0.986 ns
BM_MakeHeap_uint32_SingleElement_262144                                      1.64 ns         1.07 ns
BM_MakeHeap_uint32_PipeOrgan_1                                               1.04 ns         1.09 ns
BM_MakeHeap_uint32_PipeOrgan_4                                              0.989 ns        0.836 ns
BM_MakeHeap_uint32_PipeOrgan_16                                              1.24 ns        0.958 ns
BM_MakeHeap_uint32_PipeOrgan_64                                              1.41 ns        0.885 ns
BM_MakeHeap_uint32_PipeOrgan_256                                             1.57 ns        0.912 ns
BM_MakeHeap_uint32_PipeOrgan_1024                                            1.55 ns        0.951 ns
BM_MakeHeap_uint32_PipeOrgan_16384                                           1.58 ns        0.985 ns
BM_MakeHeap_uint32_PipeOrgan_262144                                          1.66 ns         1.12 ns
BM_MakeHeap_uint32_Heap_1                                                    1.08 ns         1.09 ns
BM_MakeHeap_uint32_Heap_4                                                   0.800 ns        0.572 ns
BM_MakeHeap_uint32_Heap_16                                                  0.704 ns        0.532 ns
BM_MakeHeap_uint32_Heap_64                                                  0.694 ns        0.405 ns
BM_MakeHeap_uint32_Heap_256                                                 0.736 ns        0.427 ns
BM_MakeHeap_uint32_Heap_1024                                                0.682 ns        0.381 ns
BM_MakeHeap_uint32_Heap_16384                                               0.665 ns        0.363 ns
BM_MakeHeap_uint32_Heap_262144                                              0.666 ns        0.372 ns
BM_MakeHeap_uint32_QuickSortAdversary_1                                      1.05 ns         1.09 ns
BM_MakeHeap_uint32_QuickSortAdversary_4                                      1.30 ns        0.843 ns
BM_MakeHeap_uint32_QuickSortAdversary_16                                     1.23 ns        0.946 ns
BM_MakeHeap_uint32_QuickSortAdversary_64                                     1.25 ns        0.725 ns
BM_MakeHeap_uint32_QuickSortAdversary_256                                    1.30 ns        0.757 ns
BM_MakeHeap_uint32_QuickSortAdversary_1024                                   1.26 ns        0.749 ns
BM_MakeHeap_uint32_QuickSortAdversary_16384                                 0.809 ns        0.455 ns
BM_MakeHeap_uint32_QuickSortAdversary_262144                                0.721 ns        0.376 ns
BM_MakeHeap_uint64_Random_1                                                  1.06 ns         1.04 ns
BM_MakeHeap_uint64_Random_4                                                  3.41 ns         2.86 ns
BM_MakeHeap_uint64_Random_16                                                 3.21 ns         2.79 ns
BM_MakeHeap_uint64_Random_64                                                 3.40 ns         3.09 ns
BM_MakeHeap_uint64_Random_256                                                3.22 ns         2.98 ns
BM_MakeHeap_uint64_Random_1024                                               3.10 ns         2.82 ns
BM_MakeHeap_uint64_Random_16384                                              3.10 ns         2.81 ns
BM_MakeHeap_uint64_Random_262144                                             3.18 ns         2.93 ns
BM_MakeHeap_uint64_Ascending_1                                               1.09 ns         1.08 ns
BM_MakeHeap_uint64_Ascending_4                                               1.35 ns        0.893 ns
BM_MakeHeap_uint64_Ascending_16                                              1.32 ns         1.01 ns
BM_MakeHeap_uint64_Ascending_64                                              1.50 ns        0.951 ns
BM_MakeHeap_uint64_Ascending_256                                             1.68 ns         1.01 ns
BM_MakeHeap_uint64_Ascending_1024                                            1.65 ns         1.00 ns
BM_MakeHeap_uint64_Ascending_16384                                           1.76 ns         1.09 ns
BM_MakeHeap_uint64_Ascending_262144                                          2.05 ns         1.32 ns
BM_MakeHeap_uint64_Descending_1                                              1.13 ns         1.07 ns
BM_MakeHeap_uint64_Descending_4                                             0.825 ns        0.589 ns
BM_MakeHeap_uint64_Descending_16                                            0.742 ns        0.531 ns
BM_MakeHeap_uint64_Descending_64                                            0.757 ns        0.502 ns
BM_MakeHeap_uint64_Descending_256                                           0.769 ns        0.463 ns
BM_MakeHeap_uint64_Descending_1024                                          0.711 ns        0.414 ns
BM_MakeHeap_uint64_Descending_16384                                         0.692 ns        0.385 ns
BM_MakeHeap_uint64_Descending_262144                                        0.692 ns        0.397 ns
BM_MakeHeap_uint64_SingleElement_1                                           1.09 ns         1.10 ns
BM_MakeHeap_uint64_SingleElement_4                                           1.33 ns        0.745 ns
BM_MakeHeap_uint64_SingleElement_16                                          1.41 ns        0.952 ns
BM_MakeHeap_uint64_SingleElement_64                                          1.58 ns        0.944 ns
BM_MakeHeap_uint64_SingleElement_256                                         1.71 ns        0.975 ns
BM_MakeHeap_uint64_SingleElement_1024                                        1.59 ns         1.01 ns
BM_MakeHeap_uint64_SingleElement_16384                                       1.72 ns         1.10 ns
BM_MakeHeap_uint64_SingleElement_262144                                      2.01 ns         1.42 ns
BM_MakeHeap_uint64_PipeOrgan_1                                               1.10 ns         1.15 ns
BM_MakeHeap_uint64_PipeOrgan_4                                               1.04 ns        0.889 ns
BM_MakeHeap_uint64_PipeOrgan_16                                              1.32 ns         1.04 ns
BM_MakeHeap_uint64_PipeOrgan_64                                              1.49 ns        0.978 ns
BM_MakeHeap_uint64_PipeOrgan_256                                             1.62 ns        0.995 ns
BM_MakeHeap_uint64_PipeOrgan_1024                                            1.58 ns        0.994 ns
BM_MakeHeap_uint64_PipeOrgan_16384                                           1.66 ns         1.10 ns
BM_MakeHeap_uint64_PipeOrgan_262144                                          1.84 ns         1.36 ns
BM_MakeHeap_uint64_Heap_1                                                    1.06 ns         1.08 ns
BM_MakeHeap_uint64_Heap_4                                                   0.793 ns        0.606 ns
BM_MakeHeap_uint64_Heap_16                                                  0.732 ns        0.525 ns
BM_MakeHeap_uint64_Heap_64                                                  0.747 ns        0.493 ns
BM_MakeHeap_uint64_Heap_256                                                 0.759 ns        0.446 ns
BM_MakeHeap_uint64_Heap_1024                                                0.706 ns        0.402 ns
BM_MakeHeap_uint64_Heap_16384                                               0.662 ns        0.384 ns
BM_MakeHeap_uint64_Heap_262144                                              0.676 ns        0.382 ns
BM_MakeHeap_uint64_QuickSortAdversary_1                                      1.05 ns         1.03 ns
BM_MakeHeap_uint64_QuickSortAdversary_4                                      1.31 ns        0.885 ns
BM_MakeHeap_uint64_QuickSortAdversary_16                                     1.25 ns        0.964 ns
BM_MakeHeap_uint64_QuickSortAdversary_64                                     1.28 ns        0.804 ns
BM_MakeHeap_uint64_QuickSortAdversary_256                                    1.32 ns        0.825 ns
BM_MakeHeap_uint64_QuickSortAdversary_1024                                   1.28 ns        0.768 ns
BM_MakeHeap_uint64_QuickSortAdversary_16384                                 0.831 ns        0.490 ns
BM_MakeHeap_uint64_QuickSortAdversary_262144                                0.709 ns        0.398 ns
BM_MakeHeap_pair&lt;uint32, uint32&gt;_Random_1                                    1.02 ns         1.07 ns
BM_MakeHeap_pair&lt;uint32, uint32&gt;_Random_4                                    3.94 ns         3.72 ns
BM_MakeHeap_pair&lt;uint32, uint32&gt;_Random_16                                   4.58 ns         3.85 ns
BM_MakeHeap_pair&lt;uint32, uint32&gt;_Random_64                                   5.32 ns         4.20 ns
BM_MakeHeap_pair&lt;uint32, uint32&gt;_Random_256                                  5.46 ns         4.27 ns
BM_MakeHeap_pair&lt;uint32, uint32&gt;_Random_1024                                 5.53 ns         4.21 ns
BM_MakeHeap_pair&lt;uint32, uint32&gt;_Random_16384                                5.58 ns         4.29 ns
BM_MakeHeap_pair&lt;uint32, uint32&gt;_Random_262144                               5.56 ns         4.44 ns
BM_MakeHeap_pair&lt;uint32, uint32&gt;_Ascending_1                                 1.07 ns         1.03 ns
BM_MakeHeap_pair&lt;uint32, uint32&gt;_Ascending_4                                 1.96 ns         1.61 ns
BM_MakeHeap_pair&lt;uint32, uint32&gt;_Ascending_16                                2.10 ns         2.06 ns
BM_MakeHeap_pair&lt;uint32, uint32&gt;_Ascending_64                                2.35 ns         2.28 ns
BM_MakeHeap_pair&lt;uint32, uint32&gt;_Ascending_256                               2.58 ns         2.41 ns
BM_MakeHeap_pair&lt;uint32, uint32&gt;_Ascending_1024                              2.57 ns         2.45 ns
BM_MakeHeap_pair&lt;uint32, uint32&gt;_Ascending_16384                             2.61 ns         2.56 ns
BM_MakeHeap_pair&lt;uint32, uint32&gt;_Ascending_262144                            2.76 ns         2.79 ns
BM_MakeHeap_pair&lt;uint32, uint32&gt;_Descending_1                                1.07 ns         1.05 ns
BM_MakeHeap_pair&lt;uint32, uint32&gt;_Descending_4                                1.21 ns         1.05 ns
BM_MakeHeap_pair&lt;uint32, uint32&gt;_Descending_16                               1.14 ns        0.980 ns
BM_MakeHeap_pair&lt;uint32, uint32&gt;_Descending_64                               1.16 ns         1.01 ns
BM_MakeHeap_pair&lt;uint32, uint32&gt;_Descending_256                              1.18 ns        0.973 ns
BM_MakeHeap_pair&lt;uint32, uint32&gt;_Descending_1024                             1.13 ns        0.945 ns
BM_MakeHeap_pair&lt;uint32, uint32&gt;_Descending_16384                            1.11 ns        0.960 ns
BM_MakeHeap_pair&lt;uint32, uint32&gt;_Descending_262144                           1.11 ns        0.929 ns
BM_MakeHeap_pair&lt;uint32, uint32&gt;_SingleElement_1                             1.08 ns         1.09 ns
BM_MakeHeap_pair&lt;uint32, uint32&gt;_SingleElement_4                             1.94 ns         1.36 ns
BM_MakeHeap_pair&lt;uint32, uint32&gt;_SingleElement_16                            2.22 ns         1.90 ns
BM_MakeHeap_pair&lt;uint32, uint32&gt;_SingleElement_64                            2.46 ns         2.19 ns
BM_MakeHeap_pair&lt;uint32, uint32&gt;_SingleElement_256                           2.55 ns         2.38 ns
BM_MakeHeap_pair&lt;uint32, uint32&gt;_SingleElement_1024                          2.51 ns         2.45 ns
BM_MakeHeap_pair&lt;uint32, uint32&gt;_SingleElement_16384                         2.57 ns         2.56 ns
BM_MakeHeap_pair&lt;uint32, uint32&gt;_SingleElement_262144                        2.74 ns         2.83 ns
BM_MakeHeap_pair&lt;uint32, uint32&gt;_PipeOrgan_1                                 1.07 ns         1.11 ns
BM_MakeHeap_pair&lt;uint32, uint32&gt;_PipeOrgan_4                                 1.42 ns         1.54 ns
BM_MakeHeap_pair&lt;uint32, uint32&gt;_PipeOrgan_16                                2.01 ns         1.99 ns
BM_MakeHeap_pair&lt;uint32, uint32&gt;_PipeOrgan_64                                2.33 ns         2.27 ns
BM_MakeHeap_pair&lt;uint32, uint32&gt;_PipeOrgan_256                               2.48 ns         2.40 ns
BM_MakeHeap_pair&lt;uint32, uint32&gt;_PipeOrgan_1024                              2.48 ns         2.46 ns
BM_MakeHeap_pair&lt;uint32, uint32&gt;_PipeOrgan_16384                             2.54 ns         2.57 ns
BM_MakeHeap_pair&lt;uint32, uint32&gt;_PipeOrgan_262144                            2.68 ns         2.76 ns
BM_MakeHeap_pair&lt;uint32, uint32&gt;_Heap_1                                      1.08 ns         1.08 ns
BM_MakeHeap_pair&lt;uint32, uint32&gt;_Heap_4                                      1.24 ns         1.02 ns
BM_MakeHeap_pair&lt;uint32, uint32&gt;_Heap_16                                     1.16 ns         1.02 ns
BM_MakeHeap_pair&lt;uint32, uint32&gt;_Heap_64                                     1.17 ns         1.00 ns
BM_MakeHeap_pair&lt;uint32, uint32&gt;_Heap_256                                    1.20 ns        0.992 ns
BM_MakeHeap_pair&lt;uint32, uint32&gt;_Heap_1024                                   1.13 ns        0.942 ns
BM_MakeHeap_pair&lt;uint32, uint32&gt;_Heap_16384                                  1.12 ns        0.936 ns
BM_MakeHeap_pair&lt;uint32, uint32&gt;_Heap_262144                                 1.13 ns        0.945 ns
BM_MakeHeap_pair&lt;uint32, uint32&gt;_QuickSortAdversary_1                        1.08 ns         1.06 ns
BM_MakeHeap_pair&lt;uint32, uint32&gt;_QuickSortAdversary_4                        1.92 ns         1.93 ns
BM_MakeHeap_pair&lt;uint32, uint32&gt;_QuickSortAdversary_16                       2.13 ns         2.07 ns
BM_MakeHeap_pair&lt;uint32, uint32&gt;_QuickSortAdversary_64                       2.36 ns         2.29 ns
BM_MakeHeap_pair&lt;uint32, uint32&gt;_QuickSortAdversary_256                      2.56 ns         2.41 ns
BM_MakeHeap_pair&lt;uint32, uint32&gt;_QuickSortAdversary_1024                     2.50 ns         2.46 ns
BM_MakeHeap_pair&lt;uint32, uint32&gt;_QuickSortAdversary_16384                    2.57 ns         2.57 ns
BM_MakeHeap_pair&lt;uint32, uint32&gt;_QuickSortAdversary_262144                   2.67 ns         2.89 ns
BM_MakeHeap_tuple&lt;uint32, uint64, uint32&gt;_Random_1                          0.979 ns         1.05 ns
BM_MakeHeap_tuple&lt;uint32, uint64, uint32&gt;_Random_4                           5.14 ns         4.88 ns
BM_MakeHeap_tuple&lt;uint32, uint64, uint32&gt;_Random_16                          7.93 ns         5.95 ns
BM_MakeHeap_tuple&lt;uint32, uint64, uint32&gt;_Random_64                          9.08 ns         6.63 ns
BM_MakeHeap_tuple&lt;uint32, uint64, uint32&gt;_Random_256                         9.10 ns         6.47 ns
BM_MakeHeap_tuple&lt;uint32, uint64, uint32&gt;_Random_1024                        9.03 ns         6.43 ns
BM_MakeHeap_tuple&lt;uint32, uint64, uint32&gt;_Random_16384                       9.09 ns         6.60 ns
BM_MakeHeap_tuple&lt;uint32, uint64, uint32&gt;_Random_262144                      9.19 ns         6.86 ns
BM_MakeHeap_tuple&lt;uint32, uint64, uint32&gt;_Ascending_1                        1.04 ns         1.02 ns
BM_MakeHeap_tuple&lt;uint32, uint64, uint32&gt;_Ascending_4                        2.13 ns         2.55 ns
BM_MakeHeap_tuple&lt;uint32, uint64, uint32&gt;_Ascending_16                       1.87 ns         2.67 ns
BM_MakeHeap_tuple&lt;uint32, uint64, uint32&gt;_Ascending_64                       2.27 ns         3.06 ns
BM_MakeHeap_tuple&lt;uint32, uint64, uint32&gt;_Ascending_256                      2.41 ns         3.18 ns
BM_MakeHeap_tuple&lt;uint32, uint64, uint32&gt;_Ascending_1024                     2.27 ns         3.18 ns
BM_MakeHeap_tuple&lt;uint32, uint64, uint32&gt;_Ascending_16384                    2.19 ns         3.40 ns
BM_MakeHeap_tuple&lt;uint32, uint64, uint32&gt;_Ascending_262144                   2.37 ns         3.78 ns
BM_MakeHeap_tuple&lt;uint32, uint64, uint32&gt;_Descending_1                      0.981 ns         1.04 ns
BM_MakeHeap_tuple&lt;uint32, uint64, uint32&gt;_Descending_4                       1.38 ns         1.61 ns
BM_MakeHeap_tuple&lt;uint32, uint64, uint32&gt;_Descending_16                      1.18 ns         1.35 ns
BM_MakeHeap_tuple&lt;uint32, uint64, uint32&gt;_Descending_64                      1.29 ns         1.35 ns
BM_MakeHeap_tuple&lt;uint32, uint64, uint32&gt;_Descending_256                     1.20 ns         1.28 ns
BM_MakeHeap_tuple&lt;uint32, uint64, uint32&gt;_Descending_1024                    1.06 ns         1.18 ns
BM_MakeHeap_tuple&lt;uint32, uint64, uint32&gt;_Descending_16384                   1.01 ns         1.13 ns
BM_MakeHeap_tuple&lt;uint32, uint64, uint32&gt;_Descending_262144                 0.982 ns         1.13 ns
BM_MakeHeap_tuple&lt;uint32, uint64, uint32&gt;_SingleElement_1                   0.967 ns         1.13 ns
BM_MakeHeap_tuple&lt;uint32, uint64, uint32&gt;_SingleElement_4                    2.34 ns         2.22 ns
BM_MakeHeap_tuple&lt;uint32, uint64, uint32&gt;_SingleElement_16                   2.46 ns         2.86 ns
BM_MakeHeap_tuple&lt;uint32, uint64, uint32&gt;_SingleElement_64                   2.74 ns         3.61 ns
BM_MakeHeap_tuple&lt;uint32, uint64, uint32&gt;_SingleElement_256                  2.75 ns         3.74 ns
BM_MakeHeap_tuple&lt;uint32, uint64, uint32&gt;_SingleElement_1024                 2.71 ns         3.69 ns
BM_MakeHeap_tuple&lt;uint32, uint64, uint32&gt;_SingleElement_16384                2.71 ns         4.03 ns
BM_MakeHeap_tuple&lt;uint32, uint64, uint32&gt;_SingleElement_262144               2.92 ns         4.58 ns
BM_MakeHeap_tuple&lt;uint32, uint64, uint32&gt;_PipeOrgan_1                       0.968 ns         1.01 ns
BM_MakeHeap_tuple&lt;uint32, uint64, uint32&gt;_PipeOrgan_4                        1.95 ns         2.15 ns
BM_MakeHeap_tuple&lt;uint32, uint64, uint32&gt;_PipeOrgan_16                       1.89 ns         2.41 ns
BM_MakeHeap_tuple&lt;uint32, uint64, uint32&gt;_PipeOrgan_64                       2.45 ns         3.00 ns
BM_MakeHeap_tuple&lt;uint32, uint64, uint32&gt;_PipeOrgan_256                      2.61 ns         3.09 ns
BM_MakeHeap_tuple&lt;uint32, uint64, uint32&gt;_PipeOrgan_1024                     2.49 ns         3.14 ns
BM_MakeHeap_tuple&lt;uint32, uint64, uint32&gt;_PipeOrgan_16384                    2.38 ns         3.38 ns
BM_MakeHeap_tuple&lt;uint32, uint64, uint32&gt;_PipeOrgan_262144                   2.48 ns         3.90 ns
BM_MakeHeap_tuple&lt;uint32, uint64, uint32&gt;_Heap_1                             1.04 ns        0.964 ns
BM_MakeHeap_tuple&lt;uint32, uint64, uint32&gt;_Heap_4                             1.43 ns         1.43 ns
BM_MakeHeap_tuple&lt;uint32, uint64, uint32&gt;_Heap_16                            1.16 ns         1.29 ns
BM_MakeHeap_tuple&lt;uint32, uint64, uint32&gt;_Heap_64                            1.26 ns         1.28 ns
BM_MakeHeap_tuple&lt;uint32, uint64, uint32&gt;_Heap_256                           1.27 ns         1.18 ns
BM_MakeHeap_tuple&lt;uint32, uint64, uint32&gt;_Heap_1024                          1.28 ns         1.13 ns
BM_MakeHeap_tuple&lt;uint32, uint64, uint32&gt;_Heap_16384                         1.19 ns         1.17 ns
BM_MakeHeap_tuple&lt;uint32, uint64, uint32&gt;_Heap_262144                        1.19 ns         1.14 ns
BM_MakeHeap_tuple&lt;uint32, uint64, uint32&gt;_QuickSortAdversary_1               1.05 ns        0.988 ns
BM_MakeHeap_tuple&lt;uint32, uint64, uint32&gt;_QuickSortAdversary_4               2.22 ns         2.77 ns
BM_MakeHeap_tuple&lt;uint32, uint64, uint32&gt;_QuickSortAdversary_16              1.92 ns         2.54 ns
BM_MakeHeap_tuple&lt;uint32, uint64, uint32&gt;_QuickSortAdversary_64              2.27 ns         2.83 ns
BM_MakeHeap_tuple&lt;uint32, uint64, uint32&gt;_QuickSortAdversary_256             2.43 ns         2.94 ns
BM_MakeHeap_tuple&lt;uint32, uint64, uint32&gt;_QuickSortAdversary_1024            2.25 ns         2.89 ns
BM_MakeHeap_tuple&lt;uint32, uint64, uint32&gt;_QuickSortAdversary_16384           2.18 ns         3.06 ns
BM_MakeHeap_tuple&lt;uint32, uint64, uint32&gt;_QuickSortAdversary_262144          2.35 ns         3.39 ns
BM_MakeHeap_string_Random_1                                                  1.35 ns         1.25 ns
BM_MakeHeap_string_Random_4                                                  10.9 ns         10.2 ns
BM_MakeHeap_string_Random_16                                                 23.3 ns         20.8 ns
BM_MakeHeap_string_Random_64                                                 27.4 ns         24.6 ns
BM_MakeHeap_string_Random_256                                                28.8 ns         25.6 ns
BM_MakeHeap_string_Random_1024                                               30.1 ns         27.9 ns
BM_MakeHeap_string_Random_16384                                              35.1 ns         35.5 ns
BM_MakeHeap_string_Random_262144                                             68.9 ns         82.1 ns
BM_MakeHeap_string_Ascending_1                                               1.32 ns         1.25 ns
BM_MakeHeap_string_Ascending_4                                               8.25 ns         8.83 ns
BM_MakeHeap_string_Ascending_16                                              18.7 ns         18.6 ns
BM_MakeHeap_string_Ascending_64                                              20.9 ns         23.3 ns
BM_MakeHeap_string_Ascending_256                                             21.8 ns         23.7 ns
BM_MakeHeap_string_Ascending_1024                                            24.4 ns         26.8 ns
BM_MakeHeap_string_Ascending_16384                                           35.3 ns         35.6 ns
BM_MakeHeap_string_Ascending_262144                                          55.2 ns         70.2 ns
BM_MakeHeap_string_Descending_1                                              1.25 ns         1.25 ns
BM_MakeHeap_string_Descending_4                                              7.69 ns         8.02 ns
BM_MakeHeap_string_Descending_16                                             19.5 ns         18.0 ns
BM_MakeHeap_string_Descending_64                                             18.2 ns         18.1 ns
BM_MakeHeap_string_Descending_256                                            18.0 ns         18.0 ns
BM_MakeHeap_string_Descending_1024                                           20.9 ns         20.1 ns
BM_MakeHeap_string_Descending_16384                                          27.3 ns         25.4 ns
BM_MakeHeap_string_Descending_262144                                         30.7 ns         33.2 ns
BM_MakeHeap_string_SingleElement_1                                           1.32 ns         1.29 ns
BM_MakeHeap_string_SingleElement_4                                           8.21 ns         7.52 ns
BM_MakeHeap_string_SingleElement_16                                          15.6 ns         14.1 ns
BM_MakeHeap_string_SingleElement_64                                          19.4 ns         18.9 ns
BM_MakeHeap_string_SingleElement_256                                         14.4 ns         14.3 ns
BM_MakeHeap_string_SingleElement_1024                                        11.3 ns         11.4 ns
BM_MakeHeap_string_SingleElement_16384                                       12.3 ns         11.4 ns
BM_MakeHeap_string_SingleElement_262144                                      29.3 ns         29.5 ns
BM_MakeHeap_string_PipeOrgan_1                                               1.30 ns         1.34 ns
BM_MakeHeap_string_PipeOrgan_4                                               8.01 ns         8.54 ns
BM_MakeHeap_string_PipeOrgan_16                                              18.7 ns         18.4 ns
BM_MakeHeap_string_PipeOrgan_64                                              21.7 ns         22.9 ns
BM_MakeHeap_string_PipeOrgan_256                                             21.9 ns         23.6 ns
BM_MakeHeap_string_PipeOrgan_1024                                            24.5 ns         26.2 ns
BM_MakeHeap_string_PipeOrgan_16384                                           35.6 ns         35.1 ns
BM_MakeHeap_string_PipeOrgan_262144                                          55.0 ns         69.0 ns
BM_MakeHeap_string_Heap_1                                                    1.26 ns         1.24 ns
BM_MakeHeap_string_Heap_4                                                    8.07 ns         7.95 ns
BM_MakeHeap_string_Heap_16                                                   22.0 ns         18.8 ns
BM_MakeHeap_string_Heap_64                                                   19.4 ns         16.9 ns
BM_MakeHeap_string_Heap_256                                                  14.7 ns         12.3 ns
BM_MakeHeap_string_Heap_1024                                                 10.5 ns         8.55 ns
BM_MakeHeap_string_Heap_16384                                                10.2 ns         8.72 ns
BM_MakeHeap_string_Heap_262144                                               18.2 ns         17.2 ns
BM_MakeHeap_string_QuickSortAdversary_1                                      1.25 ns         1.35 ns
BM_MakeHeap_string_QuickSortAdversary_4                                      10.6 ns         10.3 ns
BM_MakeHeap_string_QuickSortAdversary_16                                     25.7 ns         23.2 ns
BM_MakeHeap_string_QuickSortAdversary_64                                     25.9 ns         24.0 ns
BM_MakeHeap_string_QuickSortAdversary_256                                    23.5 ns         21.4 ns
BM_MakeHeap_string_QuickSortAdversary_1024                                   22.3 ns         19.9 ns
BM_MakeHeap_string_QuickSortAdversary_16384                                  23.4 ns         21.7 ns
BM_MakeHeap_string_QuickSortAdversary_262144                                 49.0 ns         53.2 ns
BM_MakeHeap_float_Random_1                                                   1.09 ns         1.05 ns
BM_MakeHeap_float_Random_4                                                   4.07 ns         4.40 ns
BM_MakeHeap_float_Random_16                                                  5.49 ns         4.59 ns
BM_MakeHeap_float_Random_64                                                  6.45 ns         4.84 ns
BM_MakeHeap_float_Random_256                                                 6.67 ns         4.81 ns
BM_MakeHeap_float_Random_1024                                                6.69 ns         4.74 ns
BM_MakeHeap_float_Random_16384                                               6.67 ns         4.73 ns
BM_MakeHeap_float_Random_262144                                              6.58 ns         4.65 ns
BM_MakeHeap_float_Ascending_1                                                1.12 ns         1.04 ns
BM_MakeHeap_float_Ascending_4                                                1.10 ns        0.937 ns
BM_MakeHeap_float_Ascending_16                                               1.26 ns         1.37 ns
BM_MakeHeap_float_Ascending_64                                               1.38 ns         1.28 ns
BM_MakeHeap_float_Ascending_256                                              1.39 ns         1.30 ns
BM_MakeHeap_float_Ascending_1024                                             1.32 ns         1.39 ns
BM_MakeHeap_float_Ascending_16384                                            1.24 ns         1.39 ns
BM_MakeHeap_float_Ascending_262144                                           1.31 ns         1.71 ns
BM_MakeHeap_float_Descending_1                                               1.05 ns         1.05 ns
BM_MakeHeap_float_Descending_4                                              0.835 ns        0.655 ns
BM_MakeHeap_float_Descending_16                                             0.728 ns        0.605 ns
BM_MakeHeap_float_Descending_64                                             0.628 ns        0.516 ns
BM_MakeHeap_float_Descending_256                                            0.656 ns        0.554 ns
BM_MakeHeap_float_Descending_1024                                           0.611 ns        0.515 ns
BM_MakeHeap_float_Descending_16384                                          0.597 ns        0.500 ns
BM_MakeHeap_float_Descending_262144                                         0.592 ns        0.502 ns
BM_MakeHeap_float_SingleElement_1                                            1.06 ns         1.03 ns
BM_MakeHeap_float_SingleElement_4                                            1.08 ns        0.968 ns
BM_MakeHeap_float_SingleElement_16                                           1.08 ns         1.24 ns
BM_MakeHeap_float_SingleElement_64                                           1.13 ns         1.26 ns
BM_MakeHeap_float_SingleElement_256                                          1.20 ns         1.29 ns
BM_MakeHeap_float_SingleElement_1024                                         1.16 ns         1.37 ns
BM_MakeHeap_float_SingleElement_16384                                        1.11 ns         1.39 ns
BM_MakeHeap_float_SingleElement_262144                                       1.15 ns         1.62 ns
BM_MakeHeap_float_PipeOrgan_1                                                1.08 ns         1.07 ns
BM_MakeHeap_float_PipeOrgan_4                                               0.966 ns         1.02 ns
BM_MakeHeap_float_PipeOrgan_16                                               1.12 ns         1.31 ns
BM_MakeHeap_float_PipeOrgan_64                                               1.06 ns         1.27 ns
BM_MakeHeap_float_PipeOrgan_256                                              1.18 ns         1.28 ns
BM_MakeHeap_float_PipeOrgan_1024                                             1.14 ns         1.39 ns
BM_MakeHeap_float_PipeOrgan_16384                                            1.09 ns         1.39 ns
BM_MakeHeap_float_PipeOrgan_262144                                           1.15 ns         1.68 ns
BM_MakeHeap_float_Heap_1                                                     1.04 ns         1.03 ns
BM_MakeHeap_float_Heap_4                                                    0.821 ns        0.656 ns
BM_MakeHeap_float_Heap_16                                                   0.771 ns        0.618 ns
BM_MakeHeap_float_Heap_64                                                   0.683 ns        0.519 ns
BM_MakeHeap_float_Heap_256                                                  0.658 ns        0.529 ns
BM_MakeHeap_float_Heap_1024                                                 0.668 ns        0.478 ns
BM_MakeHeap_float_Heap_16384                                                0.717 ns        0.451 ns
BM_MakeHeap_float_Heap_262144                                               0.683 ns        0.428 ns
BM_MakeHeap_float_QuickSortAdversary_1                                       1.03 ns         1.08 ns
BM_MakeHeap_float_QuickSortAdversary_4                                       1.07 ns         1.13 ns
BM_MakeHeap_float_QuickSortAdversary_16                                      1.04 ns         1.36 ns
BM_MakeHeap_float_QuickSortAdversary_64                                     0.987 ns         1.10 ns
BM_MakeHeap_float_QuickSortAdversary_256                                     1.13 ns         1.11 ns
BM_MakeHeap_float_QuickSortAdversary_1024                                    1.01 ns         1.23 ns
BM_MakeHeap_float_QuickSortAdversary_16384                                  0.643 ns        0.633 ns
BM_MakeHeap_float_QuickSortAdversary_262144                                 0.579 ns        0.695 ns

Fixes #120752


Full diff: https://github.com/llvm/llvm-project/pull/154092.diff

4 Files Affected:

  • (modified) libcxx/include/__algorithm/make_heap.h (+10-4)
  • (modified) libcxx/include/__algorithm/partial_sort.h (+1-1)
  • (modified) libcxx/include/__algorithm/partial_sort_copy.h (+1-1)
  • (modified) libcxx/include/__algorithm/sift_down.h (+19-18)
diff --git a/libcxx/include/__algorithm/make_heap.h b/libcxx/include/__algorithm/make_heap.h
index e8f0cdb27333a..7eb5d6b89492d 100644
--- a/libcxx/include/__algorithm/make_heap.h
+++ b/libcxx/include/__algorithm/make_heap.h
@@ -12,6 +12,7 @@
 #include <__algorithm/comp.h>
 #include <__algorithm/comp_ref_type.h>
 #include <__algorithm/iterator_operations.h>
+#include <__algorithm/push_heap.h>
 #include <__algorithm/sift_down.h>
 #include <__config>
 #include <__iterator/iterator_traits.h>
@@ -31,13 +32,18 @@ inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void
 __make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare&& __comp) {
   __comp_ref_type<_Compare> __comp_ref = __comp;
 
-  using difference_type = typename iterator_traits<_RandomAccessIterator>::difference_type;
-  difference_type __n   = __last - __first;
+  using __diff_t = __iter_diff_t<_RandomAccessIterator>;
+  const __diff_t __n   = __last - __first;
+
+  const __diff_t __odd_n = (__n & 1) ? __n : __n - 1;
+
   if (__n > 1) {
     // start from the first parent, there is no need to consider children
-    for (difference_type __start = (__n - 2) / 2; __start >= 0; --__start) {
-      std::__sift_down<_AlgPolicy>(__first, __comp_ref, __n, __first + __start);
+
+    for (__diff_t __start = (__odd_n - 2) / 2; __start >= 0; --__start) {
+      std::__sift_down<_AlgPolicy, true>(__first, __comp_ref, __odd_n, __start);
     }
+    std::__sift_up<_AlgPolicy>(__first, __last, __comp, __n);
   }
 }
 
diff --git a/libcxx/include/__algorithm/partial_sort.h b/libcxx/include/__algorithm/partial_sort.h
index 7f8d0c49147e3..4b39ae0cf2dfc 100644
--- a/libcxx/include/__algorithm/partial_sort.h
+++ b/libcxx/include/__algorithm/partial_sort.h
@@ -45,7 +45,7 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _RandomAccessIterator __part
   for (; __i != __last; ++__i) {
     if (__comp(*__i, *__first)) {
       _IterOps<_AlgPolicy>::iter_swap(__i, __first);
-      std::__sift_down<_AlgPolicy>(__first, __comp, __len, __first);
+      std::__sift_down<_AlgPolicy, false>(__first, __comp, __len, 0);
     }
   }
   std::__sort_heap<_AlgPolicy>(std::move(__first), std::move(__middle), __comp);
diff --git a/libcxx/include/__algorithm/partial_sort_copy.h b/libcxx/include/__algorithm/partial_sort_copy.h
index 172f53b290d54..2230dfc9cc4ad 100644
--- a/libcxx/include/__algorithm/partial_sort_copy.h
+++ b/libcxx/include/__algorithm/partial_sort_copy.h
@@ -60,7 +60,7 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<_InputIterator, _Random
     for (; __first != __last; ++__first)
       if (std::__invoke(__comp, std::__invoke(__proj1, *__first), std::__invoke(__proj2, *__result_first))) {
         *__result_first = *__first;
-        std::__sift_down<_AlgPolicy>(__result_first, __projected_comp, __len, __result_first);
+        std::__sift_down<_AlgPolicy, false>(__result_first, __projected_comp, __len, 0);
       }
     std::__sort_heap<_AlgPolicy>(__result_first, __r, __projected_comp);
   }
diff --git a/libcxx/include/__algorithm/sift_down.h b/libcxx/include/__algorithm/sift_down.h
index 42803e30631fb..e01c9b2b00f86 100644
--- a/libcxx/include/__algorithm/sift_down.h
+++ b/libcxx/include/__algorithm/sift_down.h
@@ -24,59 +24,60 @@ _LIBCPP_PUSH_MACROS
 
 _LIBCPP_BEGIN_NAMESPACE_STD
 
-template <class _AlgPolicy, class _Compare, class _RandomAccessIterator>
+template <class _AlgPolicy, bool __assume_both_children, class _Compare, class _RandomAccessIterator>
 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void
 __sift_down(_RandomAccessIterator __first,
             _Compare&& __comp,
-            typename iterator_traits<_RandomAccessIterator>::difference_type __len,
-            _RandomAccessIterator __start) {
+            __iter_diff_t<_RandomAccessIterator> __len,
+            __iter_diff_t<_RandomAccessIterator> __start) {
   using _Ops = _IterOps<_AlgPolicy>;
 
   typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
   typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
   // left-child of __start is at 2 * __start + 1
   // right-child of __start is at 2 * __start + 2
-  difference_type __child = __start - __first;
+  difference_type __child = __start;
 
   if (__len < 2 || (__len - 2) / 2 < __child)
     return;
 
-  __child                         = 2 * __child + 1;
-  _RandomAccessIterator __child_i = __first + __child;
+  __child = 2 * __child + 1;
 
-  if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + difference_type(1)))) {
+  if _LIBCPP_CONSTEXPR (__assume_both_children) {
+    // right-child exists and is greater than left-child
+    __child += __comp(__first[__child], __first[__child + 1]);
+  } else if ((__child + 1) < __len && __comp(__first[__child], __first[__child + 1])) {
     // right-child exists and is greater than left-child
-    ++__child_i;
     ++__child;
   }
 
   // check if we are in heap-order
-  if (__comp(*__child_i, *__start))
+  if (__comp(__first[__child], __first[__start]))
     // we are, __start is larger than its largest child
     return;
 
-  value_type __top(_Ops::__iter_move(__start));
+  value_type __top(_Ops::__iter_move(__first + __start));
   do {
     // we are not in heap-order, swap the parent with its largest child
-    *__start = _Ops::__iter_move(__child_i);
-    __start  = __child_i;
+    __first[__start] = _Ops::__iter_move(__first + __child);
+    __start          = __child;
 
     if ((__len - 2) / 2 < __child)
       break;
 
     // recompute the child based off of the updated parent
-    __child   = 2 * __child + 1;
-    __child_i = __first + __child;
+    __child = 2 * __child + 1;
 
-    if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + difference_type(1)))) {
+    if _LIBCPP_CONSTEXPR (__assume_both_children) {
+      __child += __comp(__first[__child], __first[__child + 1]);
+    } else if ((__child + 1) < __len && __comp(__first[__child], __first[__child + 1])) {
       // right-child exists and is greater than left-child
-      ++__child_i;
       ++__child;
     }
 
     // check if we are in heap-order
-  } while (!__comp(*__child_i, __top));
-  *__start = std::move(__top);
+  } while (!__comp(__first[__child], __top));
+  __first[__start] = std::move(__top);
 }
 
 template <class _AlgPolicy, class _Compare, class _RandomAccessIterator>

Copy link
Member

@ldionne ldionne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given the results that show a benefit only for simple types (e.g. arithmetic types), it would probably make sense to only enable this optimization in these cases. That would at least allow us to keep the optimization in the code base and potentially widen its applicability in the future if we know how to do that.

@philnik777 philnik777 force-pushed the optimize_make_heap branch 2 times, most recently from 18f79a5 to db5b085 Compare September 4, 2025 07:28
@philnik777 philnik777 force-pushed the optimize_make_heap branch 2 times, most recently from 593b553 to cc46759 Compare September 5, 2025 13:40
@philnik777 philnik777 merged commit 04518e7 into llvm:main Sep 8, 2025
11 of 14 checks passed
@philnik777 philnik777 deleted the optimize_make_heap branch September 8, 2025 10:00
@mstorsjo
Copy link
Member

mstorsjo commented Sep 9, 2025

This breaks compilation of QtDeclarative:

In file included from /home/martin/code/vlc/contrib/build-i686/qtdeclarative/src/qml/jsruntime/qv4sequenceobject.cpp:4:
In file included from /home/martin/code/vlc/contrib/i686-w64-mingw32/include/QtCore/qsequentialiterable.h:7:
In file included from /home/martin/code/vlc/contrib/i686-w64-mingw32/include/QtCore/qiterable.h:9:
In file included from /home/martin/code/vlc/contrib/i686-w64-mingw32/include/QtCore/qmetacontainer.h:8:
In file included from /home/martin/code/vlc/contrib/i686-w64-mingw32/include/QtCore/qcompare.h:632:
In file included from /home/martin/code/vlc/contrib/i686-w64-mingw32/include/QtCore/qcomparehelpers.h:27:
In file included from /home/martin/clang-nightly/i686-w64-mingw32/include/c++/v1/functional:557:
In file included from /home/martin/clang-nightly/i686-w64-mingw32/include/c++/v1/__functional/boyer_moore_searcher.h:24:
In file included from /home/martin/clang-nightly/i686-w64-mingw32/include/c++/v1/array:566:
In file included from /home/martin/clang-nightly/i686-w64-mingw32/include/c++/v1/algorithm:1875:
In file included from /home/martin/clang-nightly/i686-w64-mingw32/include/c++/v1/__algorithm/make_heap.h:16:
/home/martin/clang-nightly/i686-w64-mingw32/include/c++/v1/__algorithm/sift_down.h:49:46: error: type 'QTaggedIterator<QTaggedIterator<QSequentialIterator, void>, std::random_access_iterator_tag>' does not provide a subscript operator
   49 |   } else if ((__child + 1) < __len && __comp(__first[__child], __first[__child + 1])) {
      |                                              ^~~~~~~ ~~~~~~~
/home/martin/clang-nightly/i686-w64-mingw32/include/c++/v1/__algorithm/make_heap.h:49:12: note: in instantiation of function template specialization 'std::__sift_down<std::_ClassicAlgPolicy, false, QV4::SequenceCompareFunctor &, QTaggedIterator<QTaggedIterator<QSequentialIterator, void>, std::random_access_iterator_tag>>' requested here
   49 |       std::__sift_down<_AlgPolicy, __assume_both_children>(__first, __comp_ref, __sift_down_n, __start);
      |            ^
/home/martin/clang-nightly/i686-w64-mingw32/include/c++/v1/__algorithm/partial_sort.h:41:8: note: in instantiation of function template specialization 'std::__make_heap<std::_ClassicAlgPolicy, QV4::SequenceCompareFunctor &, QTaggedIterator<QTaggedIterator<QSequentialIterator, void>, std::random_access_iterator_tag>>' requested here
   41 |   std::__make_heap<_AlgPolicy>(__first, __middle, __comp);
      |        ^ 
/home/martin/clang-nightly/i686-w64-mingw32/include/c++/v1/__algorithm/partial_sort.h:65:12: note: in instantiation of function template specialization 'std::__partial_sort_impl<std::_ClassicAlgPolicy, QV4::SequenceCompareFunctor &, QTaggedIterator<QTaggedIterator<QSequentialIterator, void>, std::random_access_iterator_tag>, QTaggedIterator<QTaggedIterator<QSequentialIterator, void>, std::random_access_iterator_tag>>' requested here
   65 |       std::__partial_sort_impl<_AlgPolicy>(__first, __middle, __last, static_cast<__comp_ref_type<_Compare> >(__comp));
      |            ^
/home/martin/clang-nightly/i686-w64-mingw32/include/c++/v1/__algorithm/sort.h:934:10: note: in instantiation of function template specialization 'std::__partial_sort<std::_ClassicAlgPolicy, QV4::SequenceCompareFunctor, QTaggedIterator<QTaggedIterator<QSequentialIterator, void>, std::random_access_iterator_tag>, QTaggedIterator<QTaggedIterator<QSequentialIterator, void>, std::random_access_iterator_tag>>' requested here
  934 |     std::__partial_sort<_AlgPolicy>(
      |          ^   
/home/martin/clang-nightly/i686-w64-mingw32/include/c++/v1/__algorithm/sort.h:945:8: note: in instantiation of function template specialization 'std::__sort_impl<std::_ClassicAlgPolicy, QTaggedIterator<QTaggedIterator<QSequentialIterator, void>, std::random_access_iterator_tag>, QV4::SequenceCompareFunctor>' requested here
  945 |   std::__sort_impl<_ClassicAlgPolicy>(std::move(__first), std::move(__last), __comp);
      |        ^
/home/martin/code/vlc/contrib/build-i686/qtdeclarative/src/qml/jsruntime/qv4sequenceobject.cpp:54:14: note: in instantiation of function template specialization 'std::sort<QTaggedIterator<QTaggedIterator<QSequentialIterator, void>, std::random_access_iterator_tag>, QV4::SequenceCompareFunctor>' requested here
   54 |         std::sort(QSequentialIterable::RandomAccessIterator(iterable.mutableBegin()),
      |              ^
/home/martin/code/vlc/contrib/build-i686/qtdeclarative/src/qml/jsruntime/qv4sequenceobject.cpp:453:9: note: in instantiation of function template specialization 'QV4::sortSequence<QV4::SequenceCompareFunctor>' requested here
  453 |         sortSequence(this, SequenceCompareFunctor(f->engine(), argv[0]));
      |         ^
In file included from /home/martin/code/vlc/contrib/build-i686/qtdeclarative/src/qml/jsruntime/qv4sequenceobject.cpp:4:
In file included from /home/martin/code/vlc/contrib/i686-w64-mingw32/include/QtCore/qsequentialiterable.h:7:
In file included from /home/martin/code/vlc/contrib/i686-w64-mingw32/include/QtCore/qiterable.h:9:
In file included from /home/martin/code/vlc/contrib/i686-w64-mingw32/include/QtCore/qmetacontainer.h:8:
In file included from /home/martin/code/vlc/contrib/i686-w64-mingw32/include/QtCore/qcompare.h:632:
In file included from /home/martin/code/vlc/contrib/i686-w64-mingw32/include/QtCore/qcomparehelpers.h:27:
In file included from /home/martin/clang-nightly/i686-w64-mingw32/include/c++/v1/functional:557:
In file included from /home/martin/clang-nightly/i686-w64-mingw32/include/c++/v1/__functional/boyer_moore_searcher.h:24:
In file included from /home/martin/clang-nightly/i686-w64-mingw32/include/c++/v1/array:566:
In file included from /home/martin/clang-nightly/i686-w64-mingw32/include/c++/v1/algorithm:1875:
In file included from /home/martin/clang-nightly/i686-w64-mingw32/include/c++/v1/__algorithm/make_heap.h:16:
/home/martin/clang-nightly/i686-w64-mingw32/include/c++/v1/__algorithm/sift_down.h:55:14: error: type 'QTaggedIterator<QTaggedIterator<QSequentialIterator, void>, std::random_access_iterator_tag>' does not provide a subscript operator
   55 |   if (__comp(__first[__child], __first[__start]))
      |              ^~~~~~~ ~~~~~~~
/home/martin/clang-nightly/i686-w64-mingw32/include/c++/v1/__algorithm/sift_down.h:62:5: error: type 'QTaggedIterator<QTaggedIterator<QSequentialIterator, void>, std::random_access_iterator_tag>' does not provide a subscript operator
   62 |     __first[__start] = _Ops::__iter_move(__first + __child);
      |     ^~~~~~~ ~~~~~~~
/home/martin/clang-nightly/i686-w64-mingw32/include/c++/v1/__algorithm/sift_down.h:73:48: error: type 'QTaggedIterator<QTaggedIterator<QSequentialIterator, void>, std::random_access_iterator_tag>' does not provide a subscript operator
   73 |     } else if ((__child + 1) < __len && __comp(__first[__child], __first[__child + 1])) {
      |                                                ^~~~~~~ ~~~~~~~
/home/martin/clang-nightly/i686-w64-mingw32/include/c++/v1/__algorithm/sift_down.h:80:3: error: type 'QTaggedIterator<QTaggedIterator<QSequentialIterator, void>, std::random_access_iterator_tag>' does not provide a subscript operator
   80 |   __first[__start] = std::move(__top);
      |   ^~~~~~~ ~~~~~~~

(plus many more errors similar to these)

@philnik777
Copy link
Contributor Author

@mstorsjo That looks to me like an issue with the iterator. If its iterator_category derives from random_access_iterator it has to provide a subscript operator according to https://eel.is/c++draft/iterator.requirements#tab:randomaccessiterator. Given that make_heap requires a random access iterator that part is not even optional.

@mstorsjo
Copy link
Member

Hmm. I'm quite out of my depth here...

FWIW, I tried to see if this had been fixed in newer versions of Qt; the issue doesn't appear with Qt 6.9, but only with 6.8 (and probably earlier versions). The issue went away in qt/qtdeclarative@6537b2b#diff-e2bfe467554d88bbaf4f85e0cd871919b55134a80767ed2812b6262bf0e7b520, where std::sort(QSequentialIterable::RandomAccessIterator(iterable.mutableBegin()), QSequentialIterable::RandomAccessIterator(iterable.mutableEnd()), compare); and std::sort(QSequentialIterable::BidirectionalIterator(iterable.mutableBegin()), QSequentialIterable::BidirectionalIterator(iterable.mutableEnd()), compare); were removed.

Given that make_heap requires a random access iterator that part is not even optional.

Sorry, I don't really see where the user is invoking make_heap here - this is code invoking std::sort, which I presume does that somewhere under the hood.

If its iterator_category derives from random_access_iterator it has to provide a subscript operator according to https://eel.is/c++draft/iterator.requirements#tab:randomaccessiterator.

I'm also hitting similar issues with the sorting with a bidirectional iterator (in the call std::sort(QSequentialIterable::BidirectionalIterator(iterable.mutableBegin()), QSequentialIterable::BidirectionalIterator(iterable.mutableEnd()), compare);):

/home/martin/clang-nightly/i686-w64-mingw32/include/c++/v1/__algorithm/sift_down.h:49:46: error: type 'QTaggedIterator<QTaggedIterator<QSequentialIterator, void>, std::bidirectional_iterator_tag>' does not provide a subscript operator
   49 |   } else if ((__child + 1) < __len && __comp(__first[__child], __first[__child + 1])) {
      |                                              ^~~~~~~ ~~~~~~~

@philnik777
Copy link
Contributor Author

@mstorsjo std::sort also requires random access iterators, so I'm not quite sure why there is a QSequentialIterable::BidirectionalIterator. (I assume that's bidirectional only)

@mstorsjo
Copy link
Member

FYI, I reported this issue to Qt at https://bugreports.qt.io/browse/QTBUG-140181, where it got the initial response:

I guess it would be possible to implement the subscript operator for QSequentialIterable::RandomAccessIterator using QVariantRef. I simply didn't realize it was necessary back when I wrote this. Such an implementation should live in qtbase.

@nukyan
Copy link

nukyan commented Sep 21, 2025

Using subscript operator in __sift_down() slows down iterators for std::deque. It this worth?

-----------------------------------------------------------------------------------------
Benchmark                                                                Before     After
-----------------------------------------------------------------------------------------
BM_MakeHeap_uint32_Random_16777216                                      10.3 ns   12.8 ns
BM_MakeHeap_uint32_Ascending_16777216                                   7.80 ns   10.4 ns
BM_MakeHeap_uint32_Descending_16777216                                  1.64 ns   2.46 ns
BM_MakeHeap_uint32_SingleElement_16777216                               7.69 ns   10.4 ns
BM_MakeHeap_uint32_PipeOrgan_16777216                                   7.90 ns   10.5 ns
BM_MakeHeap_uint32_Heap_16777216                                        1.64 ns   2.46 ns
BM_MakeHeap_uint32_QuickSortAdversary_16777216                          1.66 ns   2.49 ns
BM_MakeHeap_uint64_Random_16777216                                      12.8 ns   15.8 ns
BM_MakeHeap_uint64_Ascending_16777216                                   10.5 ns   13.8 ns
BM_MakeHeap_uint64_Descending_16777216                                  1.94 ns   2.70 ns
BM_MakeHeap_uint64_SingleElement_16777216                               10.5 ns   13.9 ns
BM_MakeHeap_uint64_PipeOrgan_16777216                                   10.5 ns   13.9 ns
BM_MakeHeap_uint64_Heap_16777216                                        1.93 ns   2.70 ns
BM_MakeHeap_uint64_QuickSortAdversary_16777216                          1.94 ns   2.72 ns
BM_MakeHeap_pair<uint32, uint32>_Random_16777216                        15.2 ns   16.4 ns
BM_MakeHeap_pair<uint32, uint32>_Ascending_16777216                     10.5 ns   12.3 ns
BM_MakeHeap_pair<uint32, uint32>_Descending_16777216                    3.60 ns   3.89 ns
BM_MakeHeap_pair<uint32, uint32>_SingleElement_16777216                 10.4 ns   12.4 ns
BM_MakeHeap_pair<uint32, uint32>_PipeOrgan_16777216                     10.5 ns   12.3 ns
BM_MakeHeap_pair<uint32, uint32>_Heap_16777216                          3.65 ns   3.84 ns
BM_MakeHeap_pair<uint32, uint32>_QuickSortAdversary_16777216            10.4 ns   12.1 ns
BM_MakeHeap_tuple<uint32, uint64, uint32>_Random_16777216               27.3 ns   28.6 ns
BM_MakeHeap_tuple<uint32, uint64, uint32>_Ascending_16777216            18.4 ns   22.6 ns
BM_MakeHeap_tuple<uint32, uint64, uint32>_Descending_16777216           5.58 ns   6.08 ns
BM_MakeHeap_tuple<uint32, uint64, uint32>_SingleElement_16777216        18.6 ns   24.0 ns
BM_MakeHeap_tuple<uint32, uint64, uint32>_PipeOrgan_16777216            18.3 ns   22.6 ns
BM_MakeHeap_tuple<uint32, uint64, uint32>_Heap_16777216                 6.20 ns   6.26 ns
BM_MakeHeap_tuple<uint32, uint64, uint32>_QuickSortAdversary_16777216   17.6 ns   21.3 ns
BM_MakeHeap_string_Random_16777216                                       257 ns    216 ns
BM_MakeHeap_string_Ascending_16777216                                    270 ns    226 ns
BM_MakeHeap_string_Descending_16777216                                  77.2 ns   80.2 ns
BM_MakeHeap_string_SingleElement_16777216                               88.3 ns   77.2 ns
BM_MakeHeap_string_PipeOrgan_16777216                                    267 ns    220 ns
BM_MakeHeap_string_Heap_16777216                                        36.1 ns   36.3 ns
BM_MakeHeap_string_QuickSortAdversary_16777216                          95.6 ns   88.1 ns
BM_MakeHeap_float_Random_16777216                                       10.6 ns   13.6 ns
BM_MakeHeap_float_Ascending_16777216                                    6.05 ns   11.1 ns
BM_MakeHeap_float_Descending_16777216                                   1.60 ns   2.68 ns
BM_MakeHeap_float_SingleElement_16777216                                5.81 ns   11.2 ns
BM_MakeHeap_float_PipeOrgan_16777216                                    5.91 ns   11.2 ns
BM_MakeHeap_float_Heap_16777216                                         1.60 ns   2.68 ns
BM_MakeHeap_float_QuickSortAdversary_16777216                           1.60 ns   2.70 ns

using __diff_t = __iter_diff_t<_RandomAccessIterator>;
const __diff_t __n = __last - __first;

static const bool __assume_both_children = is_arithmetic<__iter_value_type<_RandomAccessIterator> >::value;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use of static within constexpr seems to be failing with gcc for C++ versions before 23. Does not seem to fail with clang. I am working on getting a simple reproducer but wanted to leave a message for the code owners to be aware of.

error: '__assume_both_children' defined 'static' in 'constexpr' function only available with '-std=c++23' or '-std=gnu++23' [-Wtemplate-body]
   39 |   static const bool __assume_both_children = is_arithmetic<__iter_value_type<_RandomAccessIterator> >::value;
      |                     ^~~~~~~~~~~~~~~~~~~~~~

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The warning is triggered in Clang when -Wsystem-headers is set. Godbolt link: https://godbolt.org/z/Wh7zsxsYY

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Clang, this seems to be a warning whereas in GCC its a hard error. Regardless, this code should be fixed given this violates the language spec. @philnik777

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which part of the spec is violated when using a conforming extension in system headers?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies for lack of clarity in my previous comments where I was just thinking out loud. From what I understand static variables were not allowed within constexpr functions until C++ 23 (https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2647r0.html). Your change creates a static variable static const bool __assume_both_children inside __make_heap function which is annotated with _LIBCPP_CONSTEXPR_SINCE_CXX14. This code to my understanding breaks the language spec w.r.t. creating static variables inside constexpr functions. I created a PR here to fix this and added you as a reviewer: #160180

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[libc++] Investigate the performance of std::make_heap

6 participants