[libc-commits] [libc] 5e2d507 - [libc] Don't call user comparator function for equal pointers

Alex Brachet via libc-commits libc-commits at lists.llvm.org
Fri Jul 22 10:03:24 PDT 2022


Author: Alex Brachet
Date: 2022-07-22T17:03:16Z
New Revision: 5e2d5071ffd770ad8f15a57bfbf21e691a475629

URL: https://github.com/llvm/llvm-project/commit/5e2d5071ffd770ad8f15a57bfbf21e691a475629
DIFF: https://github.com/llvm/llvm-project/commit/5e2d5071ffd770ad8f15a57bfbf21e691a475629.diff

LOG: [libc] Don't call user comparator function for equal pointers

The standard says two equal pointers must compare equal
so there is no need to call the user comparator function
in this case.

Differential Revision: https://reviews.llvm.org/D130310

Added: 
    

Modified: 
    libc/src/stdlib/qsort.cpp

Removed: 
    


################################################################################
diff  --git a/libc/src/stdlib/qsort.cpp b/libc/src/stdlib/qsort.cpp
index 4eaff618d0a7a..f6e2fc10e4d70 100644
--- a/libc/src/stdlib/qsort.cpp
+++ b/libc/src/stdlib/qsort.cpp
@@ -42,6 +42,10 @@ class Array {
   }
 
   int elem_compare(size_t i, const uint8_t *other) const {
+    // An element must compare equal to itself so we don't need to consult the
+    // user provided comparator.
+    if (get(i) == other)
+      return 0;
     return compare(get(i), other);
   }
 


        


More information about the libc-commits mailing list