[libcxx-commits] [libcxx] [libc++] [sort] Improve performance of std::sort (PR #76616)

Amirreza Ashouri via libcxx-commits libcxx-commits at lists.llvm.org
Sat Dec 30 08:31:07 PST 2023


================
@@ -68,8 +68,7 @@ void test_same() {
   auto snapshot_custom_v = v;
   std::sort(v.begin(), v.end());
   std::sort(snapshot_v.begin(), snapshot_v.end());
-  std::sort(snapshot_custom_v.begin(), snapshot_custom_v.end(),
-            [](const EqualType&, const EqualType&) { return false; });
----------------
AMP999 wrote:

The old test case passes `[](const EqualType&, const EqualType&) { return false; }` as a comparator, which is not a "simple comparator", and `__use_branchless_sort` is written in a way that is true only when the `__is_simple_comparator` is true, therefor libc++ chooses to use branchless sort for `std::sort(snapshot_v.begin(), snapshot_v.end());` but not for `std::sort(snapshot_custom_v.begin(), snapshot_custom_v.end(), [](const EqualType&, const EqualType&) { return false; });` but after the change `std::sort(snapshot_custom_v.begin(), snapshot_custom_v.end(), std::less<EqualType>())` uses branchless sort. After this change, both statements will use branchless sort while the main behavior is preserved.

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


More information about the libcxx-commits mailing list