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

            Bug ID: 116651
           Summary: Memory allocation elision for std::vector like cases
           Product: gcc
           Version: 15.0
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: antoshkka at gmail dot com
  Target Milestone: ---

Code close to the following was noted in many user applications:

bool test1(const std::vector<std::string_view>& in) {
    return in == std::vector<std::string_view>{"*"};
}

Here people wish to make sure that the vector contains only a single "*"
element. In other words they assume that the above code snippet would be
optimized to something like:

bool test2(const std::vector<std::string_view>& in) {
    return in.size() == 1 && in[0] == "*";
}


Unfortunately that does not happen: https://godbolt.org/z/r59a4nobP

Note that all the functions are inlined however the new+delete are not elided.


Minimized reproducer: https://godbolt.org/z/jvcEd8zo6

Reply via email to