boost::container::small_vector doesn't seem to allocate in-place

删除回忆录丶 提交于 2019-12-11 15:48:55

问题


To test my understanding of small_vector, I tried the sample program below, where I template the vector with an in-place size of 3 and populate the vector with 10 elements. I'd expect the first 3 elements to be stored in-place and the last 7 elements to be stored out-of-place on the free-store, but that doesn't seem to be the case when I observe the memory layout: all of the items seem to be stored contiguously and out-of-place, as with a regular std::vector.

I tried various compilers (different versions of GCC and Clang) and different Boost versions, but that doesn't seem to make a difference. Neither do the following code changes:

  • Allocating the vector itself on the free-store.
  • Surrounding the vec local with large-size locals before and after, and only then loading it up with elements.

Is there any good explanation for this?

#include <iostream>
#include <boost/container/small_vector.hpp>

int main()
{
    auto vec = boost::container::small_vector<int, 3> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };  

    for (const auto& num : vec)
    {
        std::cout <<
            "Index: " << num <<
            " Distance from vec[0]: " << (long)&num - (long)&vec[0] <<
            " Distance from vec: " << (long)&num - (long)&vec << "\n";
    }
}

Output:

Index: 0 Distance from vec[0]: 0 Distance from vec: -140731961813152
Index: 1 Distance from vec[0]: 4 Distance from vec: -140731961813148
Index: 2 Distance from vec[0]: 8 Distance from vec: -140731961813144
Index: 3 Distance from vec[0]: 12 Distance from vec: -140731961813140
Index: 4 Distance from vec[0]: 16 Distance from vec: -140731961813136
Index: 5 Distance from vec[0]: 20 Distance from vec: -140731961813132
Index: 6 Distance from vec[0]: 24 Distance from vec: -140731961813128
Index: 7 Distance from vec[0]: 28 Distance from vec: -140731961813124
Index: 8 Distance from vec[0]: 32 Distance from vec: -140731961813120
Index: 9 Distance from vec[0]: 36 Distance from vec: -140731961813116
Index: 10 Distance from vec[0]: 40 Distance from vec: -140731961813112

See: https://wandbox.org/permlink/zMGRxHlM96Riq9Ky


回答1:


In boost documentation,

small_vector

small_vector is a vector-like container optimized for the case when it contains few elements. It contains some preallocated elements in-place, which allows it to avoid the use of dynamic storage allocation when the actual number of elements is below that preallocated threshold. small_vector is inspired by LLVM's SmallVector container. Unlike static_vector, small_vector's capacity can grow beyond the initial preallocated capacity.

small_vector is convertible to small_vector_base, a type that is independent from the preallocated element count, allowing client code that does not need to be templated on that N argument. small_vector inherits all vector's member functions so it supports all standard features like emplacement, stateful allocators, etc.

It says its capacity can grow beyond the initial, but doesn't say that the initial storage will be used when the capacity grows.

The "vector" things are usually expected as contiguous, and it makes many benefits. (raw pointers as iterators, super-speedy random access, etc..) Giving up these benefits to reduce small dynamic memory usage (remember that is "small"_vector) is not a good trade.



来源:https://stackoverflow.com/questions/46115267/boostcontainersmall-vector-doesnt-seem-to-allocate-in-place

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!