boost :: container :: small_vector似乎没有就地分配 [英] boost::container::small_vector doesn't seem to allocate in-place

查看:348
本文介绍了boost :: container :: small_vector似乎没有就地分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

测试我对 ,我尝试了下面的示例程序,在该模板中,将向量模板化为就地大小为3,并使用10个元素填充向量.我希望前3个元素可以原地存储,而后7个元素可以在免费存储中不正确地存储,但是当我观察内存布局时,情况似乎并非如此:像常规的std::vector一样,所有项目似乎都是连续且错位存储的.

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.

我尝试了各种编译器(GCC和Clang的不同版本)和不同的Boost版本,但这似乎没有什么不同.以下代码也没有更改:

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:

  • 在免费商店中分配媒介本身.
  • vec局部变量前后用大尺寸局部变量包围,然后才将其装入元素.
  • 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";
    }
}

输出:

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

请参阅: https://wandbox.org/permlink/zMGRxHlM96Riq9Ky

推荐答案

small_vector

small_vector是类似矢量的容器,已针对 包含少量元素的情况.它包含一些预分配的 元素就位,从而避免使用动态存储 当元素的实际数量低于该数量时进行分配 预分配的阈值. small_vector的灵感来自LLVM的SmallVector 容器.与static_vector不同,small_vector的容量可以增加 超出了最初的预分配容量.

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可转换为small_vector_base,这种类型独立于预分配的元素 计数,允许不需要在其上模板化的客户端代码 N个论点. small_vector继承了所有vector的成员函数,因此它 支持所有标准功能,例如布局,状态分配器, 等

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.

通常期望向量"事物是连续的,并带来许多好处. (作为迭代器的原始指针,超快速随机访问等.)放弃这些好处以减少较小的动态内存使用(请记住,"small"_vector)不是一个好交易.

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.

这篇关于boost :: container :: small_vector似乎没有就地分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆