Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't std::deque allow specifying the bucket size?

Tags:

std::deque stores elements in "buckets" (arrays) of fixed size. Different compilers use different bucket sizes:

  • MSVC: 16 bytes or element size if it's bigger
  • GCC: 512 bytes or element size if it's bigger
  • Clang: element_size < 256 ? 4096 : element_size * 16

For MSVC (especially) and GCC, if the deque element size is bigger than the hardcoded size, std::deque turns into a convoluted std::list with performance penalties in the majority of cases.

Clang does better in my opinion, no matter what the size of the deque element, the bucket will be at least 16 elements. Though the minimal bucket size of 4096 bytes can be sub-optimal in some cases for small elements.

Why doesn't std::deque have an additional template parameter for bucket size with the default value of what the vendor thinks is reasonable? This wouldn't break backward compatibility but would allow performance optimisation.

like image 660
Andriy Tylychko Avatar asked Jul 14 '19 23:07

Andriy Tylychko


People also ask

How do I set deque size?

deque resize() function in C++ STL The deque::resize() is an inbuilt function in C++ STL which changes the size of the deque. If the given size is greater than the current size, then new elements are inserted at the end of the deque. If the given size is smaller than the current size, then extra elements are destroyed.

How do I know my deque size?

deque::size() size() function is used to return the size of the deque container or the number of elements in the deque container. This is an inbuilt function from C++ Standard Template Library(STL).

Is deque contiguous memory?

Elements in a deque are not contiguous in memory; vector elements are guaranteed to be. So if you need to interact with a plain C library that needs contiguous arrays, or if you care (a lot) about spatial locality, then you might prefer vector .

How does std :: deque work?

A deque is generally implemented as a collection of memory blocks. These memory blocks contains the elements at contiguous locations. When we create a deque object it internally allocates a memory block to store the elements at contigious location.


1 Answers

deque is like a black box. It isn't specified how it is implemented. The implementation is free to use any technique it likes to conform to the performance requirements. Therefore, it can't take the bucket size as a template parameter.

Of course, such a data structure is useful. The standard can have chosen to provide it (under the name deque or as a new container), but they didn't. In contrast, the unordered_* containers are guaranteed to use buckets. Per [unord.req]/9:

The elements of an unordered associative container are organized into buckets. Keys with the same hash code appear in the same bucket. The number of buckets is automatically increased as elements are added to an unordered associative container, so that the average number of elements per bucket is kept below a bound. Rehashing invalidates iterators, changes ordering between elements, and changes which buckets elements appear in, but does not invalidate pointers or references to elements. For unordered_­multiset and unordered_­multimap, rehashing preserves the relative ordering of equivalent elements.

deque does not have similar wording.

like image 196
L. F. Avatar answered Sep 20 '22 13:09

L. F.