public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/97912] New: Get rid of location-invariant requirement in std::function small object optimisation
@ 2020-11-19 17:29 redi at gcc dot gnu.org
  2020-11-19 17:31 ` [Bug libstdc++/97912] " redi at gcc dot gnu.org
  2021-08-31 16:13 ` redi at gcc dot gnu.org
  0 siblings, 2 replies; 3+ messages in thread
From: redi at gcc dot gnu.org @ 2020-11-19 17:29 UTC (permalink / raw)
  To: gcc-bugs

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

            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 (rather
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 <functional>
#include <cassert>

struct selfobsessed
{
  selfobsessed() : me(this) { }

  selfobsessed(const selfobsessed&) : me(this) { }
  selfobsessed& operator=(const selfobsessed&) { return *this; }

  void operator()() const { assert(me == this); }

  selfobsessed* me;
};

int main()
{
  std::function<void()> f1( selfobsessed{} );
  std::function<void()> f2;
  swap(f1, f2);
  f2();
}

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

* [Bug libstdc++/97912] Get rid of location-invariant requirement in std::function small object optimisation
  2020-11-19 17:29 [Bug libstdc++/97912] New: Get rid of location-invariant requirement in std::function small object optimisation redi at gcc dot gnu.org
@ 2020-11-19 17:31 ` redi at gcc dot gnu.org
  2021-08-31 16:13 ` redi at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: redi at gcc dot gnu.org @ 2020-11-19 17:31 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Jonathan Wakely from comment #0)
> It would be good to remove the trivially copyable requirement, and store any
> type of suitable size and alignment to fit in the buffer.

Well, we'd still require nothrow move constructible. So not *any* type.

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

* [Bug libstdc++/97912] Get rid of location-invariant requirement in std::function small object optimisation
  2020-11-19 17:29 [Bug libstdc++/97912] New: Get rid of location-invariant requirement in std::function small object optimisation redi at gcc dot gnu.org
  2020-11-19 17:31 ` [Bug libstdc++/97912] " redi at gcc dot gnu.org
@ 2021-08-31 16:13 ` redi at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: redi at gcc dot gnu.org @ 2021-08-31 16:13 UTC (permalink / raw)
  To: gcc-bugs

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

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |ABI
             Status|UNCONFIRMED                 |NEW
     Ever confirmed|0                           |1
   Last reconfirmed|                            |2021-08-31

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

end of thread, other threads:[~2021-08-31 16:13 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-19 17:29 [Bug libstdc++/97912] New: Get rid of location-invariant requirement in std::function small object optimisation redi at gcc dot gnu.org
2020-11-19 17:31 ` [Bug libstdc++/97912] " redi at gcc dot gnu.org
2021-08-31 16:13 ` redi 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).