From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 972853858C41; Thu, 2 Nov 2023 13:31:27 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 972853858C41 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1698931887; bh=k/Q45QnlzUIMC4nSu7pJGlVhlGPyreZa90Sr5yhqMw8=; h=From:To:Subject:Date:From; b=r85ImQWkI+1Tp8PjFTXoaP0M85OkIPi4aHKv/rBRrtl0nGW04Mgc4+U5zHRW/i9QR c5ezvRWev5nXwxUyeaxHt+j5o0oWVZBDGkeXr8m8DRpiir6hY596upGjDjsh7KglCv s0S9CmRZ2YMnS19p9TdclG6q2KD+TmewHG423EHg= From: "ted at lyncon dot se" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/112349] New: ranges::max makes unecessary copies Date: Thu, 02 Nov 2023 13:31:26 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: 13.2.1 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: ted at lyncon dot se X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D112349 Bug ID: 112349 Summary: ranges::max makes unecessary copies Product: gcc Version: 13.2.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: ted at lyncon dot se Target Milestone: --- From: https://stackoverflow.com/questions/77409266/why-does-gcc-copy-object-for-e= ach-comparison-in-stdrangesmax OP's sample program shows a copy being made for each comparison: ``` #include #include #include #include struct A { A() =3D default; A(const A&) { std::cout << "Copy\n"; } A(A&&) noexcept { std::cout << "Move\n"; } A& operator=3D(const A&) { std::cout << "Copy assigned\n"; return *this; } A& operator=3D(A&&) noexcept { std::cout << "Move assigned\n"; return *this; } int x =3D 10; }; int main() { std::vector vec(10); std::cout << "Init\n"; std::cout << std::ranges::max(vec, [](const auto& a, const auto& b) { std::cout << "cmp" << std::endl; return a.x < b.x; }).x; } ``` I _think_ the proper solution is to not copy `__first` into `__tmp` in `operator()`: ``` template, _Proj= >> _Comp =3D ranges::less> requires indirectly_copyable_storable, range_value_t<_Range>*> constexpr range_value_t<_Range> operator()(_Range&& __r, _Comp __comp =3D {}, _Proj __proj =3D {}) co= nst { auto __first =3D ranges::begin(__r); auto __last =3D ranges::end(__r); __glibcxx_assert(__first !=3D __last); auto __result =3D *__first; while (++__first !=3D __last) { auto __tmp =3D *__first; // <- CO= PY if (std::__invoke(__comp, std::__invoke(__proj, __result), std::__invoke(__proj, __tmp))) __result =3D std::move(__tmp); } return __result; } ``` By changing it to ```c++ auto& __tmp =3D *__first; ``` ...or just supplying `*__first` to `std::__invoke` it doesn't do copies in = OP's example but it may have other consequences. Worth looking in to anyway.=