This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug libstdc++/77528] New: std::queue default constructor unnecessarily creates temporary of underlying Container


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

            Bug ID: 77528
           Summary: std::queue default constructor unnecessarily creates
                    temporary of underlying Container
           Product: gcc
           Version: 6.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: d.rigby at me dot com
  Target Milestone: ---

Spun out from bug 77524 - std::queue (defaulting to using std::deque as the
underlying Container) performs 4 allocations for an empty std::queue<int>.
Initial analysis from Jonathan Wakely (bug 77524 comment 4):

<quote>
Ouch. It's caused by the same issue, but the default constructor for std::queue
constructs a temporary of the underlying sequence, then move constructs the
data member from the temporary. With std::deque that means we allocate for the
temporary, then allocate again for the data member, then deallocate the memory
of the temporary. That sucks.

      explicit
      queue(_Sequence&& __c = _Sequence())
      : c(std::move(__c)) { }

 IIRC the "default" constructor is defined that way so that it's possible to
explicitly instantiate std::queue<_Sequence> with a non-DefaultConstructible 
_Sequence. But it means std::queue<std:deque<T>> is horribly inefficient. At
least for an empty std::deque<T> the initial allocation is probably going to
end up being useful, because in most cases the deque doesn't stay empty and the
allocation ends up being useful later. In this std::queue constructor the
temporary is definitely never used (it goes out of scope immediately) so
pre-allocating is entirely useless. std::stack has the same issue.

Please create a new bug for those container adaptors, as that can and should be
fixed for the default configuration. We'll keep this bug for the
non-backwards-compatible std::deque change to be done at some future date.
Thanks.
</quote>

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]