[libcxx-commits] [libcxx] [libc++] Speed up set_intersection() by fast-forwarding over ranges of non-matching elements with one-sided binary search. (PR #75230)

Alexander Kornienko via libcxx-commits libcxx-commits at lists.llvm.org
Tue Jul 30 17:22:13 PDT 2024


alexfh wrote:

Thanks for the nice optimization!

However, I'd like to point out one real-world complication it brings: it breaks some invalid usages of `std::set_intersection` in a hard-to-debug way. I've just investigated a test failure that boiled down to unsorted sequences passed to `std::set_intersection`. The only clue was that the failure started appearing after this libc++ commit. Without this hint I can imagine many more hours of poking around.

I wonder, if it would be possible to add runtime checks in one of the hardening modes to assist with finding this sort of an issue? For example, a check for the correct ordering of the bounds could be made on each step of the binary search. This should at least retain the complexity. A more comprehensive check (ordering of all elements in both input sequences) could be done in `_LIBCPP_HARDENING_MODE_EXTENSIVE` or `_LIBCPP_HARDENING_MODE_DEBUG` mode. What do you think about this?

An example of a problematic use: https://gcc.godbolt.org/z/Y5dxsxzbe.

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


More information about the libcxx-commits mailing list