[libc-commits] [PATCH] D130310: [libc] Don't call user comparator function for equal pointers

Alex Brachet via Phabricator via libc-commits libc-commits at lists.llvm.org
Thu Jul 21 14:57:44 PDT 2022


abrachet created this revision.
abrachet added a reviewer: sivachandra.
Herald added subscribers: ecnelises, tschuett.
Herald added a project: All.
abrachet requested review of this revision.

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


https://reviews.llvm.org/D130310

Files:
  libc/src/stdlib/qsort.cpp


Index: libc/src/stdlib/qsort.cpp
===================================================================
--- libc/src/stdlib/qsort.cpp
+++ libc/src/stdlib/qsort.cpp
@@ -42,6 +42,10 @@
   }
 
   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);
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D130310.446548.patch
Type: text/x-patch
Size: 447 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libc-commits/attachments/20220721/b2b3ccf9/attachment.bin>


More information about the libc-commits mailing list