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

            Bug ID: 77524
           Summary: Empty std::deque allocates unnecessary heap memory
           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: ---

Created attachment 39582
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=39582&action=edit
deque_alloc_test.cc

I'm observing a performance issue under g++/libstdc++ with std::deque. G++ is
creating two allocations for an empty (and ultimately unused) std::deque,
totalling 576 bytes. Consider the example code:

===cut===
#include <cstdio>
#include <cstdlib>
#include <queue>
void* operator new(std::size_t sz) {
    std::printf("global op new called, size = %zu\n",sz);
    return std::malloc(sz);
}
void operator delete(void* ptr) throw()
{
    std::puts("global op delete called");
    std::free(ptr);
}

int main() {
    std::puts("Alloc std::queue<int>");
    std::deque<int> q;
}
===cut===

(Tested via http://melpon.org/wandbox/permlink/4AXnmcwWhFs9ueRj)

Under GCC 6.1.0 I see 2 allocations being made:

    $ g++ prog.cc -O2 -std=c++11 && ./a.out
    Alloc std::deque<int>
    global op new called, size = 64
    global op new called, size = 512
    global op delete called
    global op delete called

For comparison, under Clang/libc++ I see zero allocations:

    $ clang++ prog.cc -stdlib=libc++ -O2 -std=c++11 && ./a.out
    Alloc std::deque<int>

(I encountered this in a use-case where I create a std::queue<> local and
conditionally add items to it, and then pass the std::queue<> onwards to other
function to consume. A non-trivial number of times there are no items added to
the queue, and hence I was surprised to see cost (i.e. the allocations) in
creating something I don't use).

Reply via email to