[all-commits] [llvm/llvm-project] 0eaacc: [ADT] Make llvm::is_contained call member `contain...

Jakub Kuderski via All-commits all-commits at lists.llvm.org
Wed Mar 15 09:08:33 PDT 2023


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: 0eaacc25bb98f50cc98bab5f6ef8d6d67e112317
      https://github.com/llvm/llvm-project/commit/0eaacc25bb98f50cc98bab5f6ef8d6d67e112317
  Author: Jakub Kuderski <kubak at google.com>
  Date:   2023-03-15 (Wed, 15 Mar 2023)

  Changed paths:
    M llvm/include/llvm/ADT/STLExtras.h
    M llvm/include/llvm/Analysis/LoopInfoImpl.h
    M llvm/unittests/ADT/STLExtrasTest.cpp

  Log Message:
  -----------
  [ADT] Make llvm::is_contained call member `contains` or `find` when available

This makes it so that calling `llvm::is_contained` no longer degrades
performance over member contains, even though both have almost identical
names. This would be the case in most set/map classes that can check for
an element being present in O(1) or O(log n) time vs. linear scan with
`std::find`. For C++17 maps/sets without `.contains`, use `.find` when available,
falling back to a linear scan with `std::find`.

I also considered detecting member contains and triggering a
`static_assert` instead, but decided against it because it's just as easy
to do the right thing and call `.contains`. This would also make some code fail
only when compiled in the C++20 mode when more container types come with
`.contains` member functions.

This was actually already the case with `CommandLine.h` calling `is_contained`
on `SmallPtrSet` and in a recent BOLT patch.

Reviewed By: kazu, dblaikie, MaskRay

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




More information about the All-commits mailing list