[libcxx-commits] [libcxx] Libcxx nth element small ranges (PR #215698)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Wed Aug 12 06:04:28 PDT 2026


================
@@ -234,7 +259,16 @@ inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __nth_element_im
 
   std::__debug_randomize_range<_AlgPolicy>(__first, __last);
 
-  std::__nth_element<_AlgPolicy, __comp_ref_type<_Compare> >(__first, __nth, __last, __comp);
+  // When the size of the range is known at compile time (e.g. in a median-of-5 filter), this
+  // dispatch constant-folds to the appropriate sorting network. Otherwise, the check disappears
+  // entirely and we fall through to the general algorithm, which handles small ranges itself.
+  // Note that __builtin_constant_p is only resolved after inlining, so this catches callers that
+  // pass a constant size even though it is not a constant expression here.
----------------
ldionne wrote:

```suggestion
```

This part is about how `__builtin_constant_p` works generally speaking, I don't think it belongs here.

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


More information about the libcxx-commits mailing list