From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 85A2738708BE; Thu, 19 Nov 2020 17:29:17 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 85A2738708BE From: "redi at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug libstdc++/97912] New: Get rid of location-invariant requirement in std::function small object optimisation Date: Thu, 19 Nov 2020 17:29:17 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: libstdc++ X-Bugzilla-Version: 11.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: enhancement 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: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter cc 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 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 19 Nov 2020 17:29:17 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D97912 Bug ID: 97912 Summary: Get rid of location-invariant requirement in std::function small object optimisation Product: gcc Version: 11.0 Status: UNCONFIRMED Severity: enhancement Priority: P3 Component: libstdc++ Assignee: unassigned at gcc dot gnu.org Reporter: redi at gcc dot gnu.org CC: barry.revzin at gmail dot com Target Milestone: --- std::function will only store the target object in an internal buffer (rath= er than on the heap) if it is "location invariant", which means trivially copyable. It would be good to remove the trivially copyable requirement, and store any type of suitable size and alignment to fit in the buffer. The difficulty is that std::function::swap does a byte-wise copy of the _Any_data buffer, which is why we require trivially copyable. Changing that might be an ABI break (existing code will have inlined that function::swap definition, and will still do a byte-wise copy rather than whatever the new code does to support non-trivially copyable types). This testcase fails if we remove the trivially copyable requirement: #include #include struct selfobsessed { selfobsessed() : me(this) { } selfobsessed(const selfobsessed&) : me(this) { } selfobsessed& operator=3D(const selfobsessed&) { return *this; } void operator()() const { assert(me =3D=3D this); } selfobsessed* me; }; int main() { std::function f1( selfobsessed{} ); std::function f2; swap(f1, f2); f2(); }=