Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::deque memory usage - Visual C++, and comparison to others

Follow up to What the heque is going on with the memory overhead of std::deque?

Visual C++ manages deque blocks according to the container element type using this:

#define _DEQUESIZ   (sizeof (value_type) <= 1 ? 16 \
    : sizeof (value_type) <= 2 ? 8 \
    : sizeof (value_type) <= 4 ? 4 \
    : sizeof (value_type) <= 8 ? 2 \
    : 1)    /* elements per block (a power of 2) */

This results in very large memory footprint for small elements. By changing the 16 in the first line to 128 I was able to drastically reduce the footprint required for a large deque<char>. Process Explorer Private Bytes dropped from 181MB -> 113MB after 100m push_back(const char& mychar) calls).

  • Can anybody justify the values in that #define?
  • How do other compilers handle deque block sizing?
  • What would be their footprint (32-bit operation) for the simple test of 100m push_back calls to deque<char>?
  • Does STL allow for overriding of this block size at compile-time, without modifying the <deque> code?
like image 632
Steve Townsend Avatar asked Nov 04 '10 14:11

Steve Townsend


People also ask

Does deque take more memory than vector?

Deque can have additional memory overhead over vector because it's made of a few blocks instead of contiguous one.

Is std :: deque contiguous?

As the quote from cppref states, std::deque uses multiple arrays to store the elements. Within each array the elements are contiguous.

Is deque sorted?

Unlike lists, deques don't include a . sort() method to sort the sequence in place. This is because sorting a linked list would be an inefficient operation.

What is deque c++?

In C++, the STL deque is a sequential container that provides the functionality of a double-ended queue data structure. In a regular queue, elements are added from the rear and removed from the front. However, in a deque, we can insert and remove elements from both the front and rear. Deque Data Structure.


1 Answers

gcc has

return __size < 512 ? size_t(512 / __size) : size_t(1);

with a comment

/*  The '512' is
 *  tunable (and no other code needs to change), but no investigation has
 *  been done since inheriting the SGI code.
 */
like image 144
AProgrammer Avatar answered Sep 21 '22 06:09

AProgrammer