From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 0CA123858C5F; Fri, 3 Feb 2023 10:01:36 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0CA123858C5F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1675418496; bh=YFapEW8XEFm8RI1KxTdEdvRd57utstXyqCE+yWjAxnE=; h=From:To:Subject:Date:In-Reply-To:References:From; b=UGKAa9FxCOwRnTS0seBYPeREfrvBhdPYslz0rwAjKTeTyXWj5orPfzAcO2ZeH7HWq lTiUu3NloLbMH2gq4HHSgot1wgLxF3TbpO9yg8swxZMx9hOR3QV6vxuLimShgayIMY nRW+9TURyLqlztxLsKEhCoJAaiBgW42HfSpC+uoY= From: "redi at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug libstdc++/108645] Change in behavior, std::accumulate doesn't always work as expected in C++20 builds Date: Fri, 03 Feb 2023 10:01:33 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: libstdc++ X-Bugzilla-Version: unknown X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: redi at gcc dot gnu.org 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: Message-ID: In-Reply-To: References: 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=3D108645 --- Comment #4 from Jonathan Wakely --- (In reply to Jonathan Wakely from comment #3) > const auto __size =3D __lhs.size() + __rhs.size(); > if (__size > __lhs.capacity() && __size <=3D __rhs.capacity()) > return std::move(__rhs.insert(0, __lhs)); It would be possible to change that logic to: const auto __size =3D __lhs.size() + __rhs.size(); if (__size <=3D __rhs.capacity() && __rhs.capacity() > __lhs.capacity()) return std::move(__rhs.insert(0, __lhs)); That way if both strings have sufficient capacity, we would return the larg= er of the two capacities. In general, the returned string is more likely to be= the one that gets reused, and this way it would be more likely to have addition= al spare capacity. In some usage patterns this would be a pessimization, because it would tend= to coalesce all the larger capacities into one string, leaving smaller, less useful strings behind. Also, inserting into the beginning of the RHS takes = more work than simply appending to the end of the LHS. So I don't think it's cle= ar that this would be a definite improvement. It wouldn't change anything in your example anyway, because there is no dynamically allocated memory anywhere except the accumulator value, which is always the LHS.=