I learned this fact after reading Inside STL: The deque, implementation, and I am very shocked by it. 16 bytes!? That’s only twice the size of a pointer! I know that the major implementations of the standard library is an array of pointers to Blocks, and since a Block is only twice the size of a pointer, why not just store the elements themselves directly? (Here, we temporarily do not consider the situation where larger elements are stored.)
I opened the <deque>
header in my MSVC and found the Block’s size:
// deque standard header in MSVC 2023 Community
static constexpr int _Block_size = _Bytes <= 1 ? 16
: _Bytes <= 2 ? 8
: _Bytes <= 4 ? 4
: _Bytes <= 8 ? 2
: 1; // elements per block (a power of 2)
So, when the elements stored in the deque are less than 16 bytes, the deque’s Block size is only 16 bytes or less.
My question is, can I modify this part of the content in the deque standard header? For example, modify it to this:
static constexpr int _Block_size = _Bytes <= 1 ? 64
: _Bytes <= 2 ? 32
: _Bytes <= 4 ? 16
: _Bytes <= 8 ? 8
: _Bytes <= 16 ? 4
: _Bytes <= 32 ? 2
: 2; // elements per block (a power of 2)
What would be the impact of doing this? If I make these changes and save it as a separate deque_.h
file, would I need to move it out of the std namespace (and modify the include guards)? Or, since I only modified the Block size, can I avoid introducing and completely overwrite std::deque’s _Block_size by placing #include "deque_.h"
at the top and using the same include guards as <deque>
?
By the way, since I increased the Block size, I also reduced the initial map size:
static constexpr int _Minimum_map_size = 4; // Originally 8
許恩嘉 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1