public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug middle-end/112312] New: GCC fails to optimize a C++ algorithm with a function passed in as well as with a lambda
@ 2023-10-31 11:52 ville.voutilainen at gmail dot com
  2023-10-31 19:12 ` [Bug middle-end/112312] " pinskia at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: ville.voutilainen at gmail dot com @ 2023-10-31 11:52 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112312

            Bug ID: 112312
           Summary: GCC fails to optimize a C++ algorithm with a function
                    passed in as well as with a lambda
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ville.voutilainen at gmail dot com
  Target Milestone: ---

See 
https://godbolt.org/z/s8dWGzb9r

When passing a (pointer to) function as a predicate argument for a C++ stdlib
algorithm, various people hope that it optimizes as well as passing a lambda
that does exactly the same thing. And with clang, it does. With gcc, the
generated
code is much worse for the function case, even though the function definition
is nearby and visible.

The test code used on godbolt pasted for convenience:

#include <string>
#include <vector>
#include <algorithm>

static bool pred1(const std::string& a, const std::string& b)
{
    return false;
}

auto pred2 = [](const std::string& a, const std::string& b)
{
    return false;
};

bool func1(const std::vector<std::string>& vec, const std::string& needle)
{
    return std::ranges::lower_bound(vec, needle, pred1) != vec.end();
}

bool func2(const std::vector<std::string>& vec, const std::string& needle)
{
    return std::ranges::lower_bound(vec, needle, pred2) != vec.end();
}

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2024-02-20 14:02 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-10-31 11:52 [Bug middle-end/112312] New: GCC fails to optimize a C++ algorithm with a function passed in as well as with a lambda ville.voutilainen at gmail dot com
2023-10-31 19:12 ` [Bug middle-end/112312] " pinskia at gcc dot gnu.org
2023-10-31 19:16 ` [Bug tree-optimization/112312] " pinskia at gcc dot gnu.org
2023-10-31 19:23 ` [Bug tree-optimization/112312] -O3 produces worse code than -O2 for std::ranges::lower_bound in some cases, not marking a loop as finite pinskia at gcc dot gnu.org
2024-02-20 14:02 ` jamborm at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).