简体   繁体   English

std :: vector resize(0)或clear() - 但保持它的容量

[英]std::vector resize(0) or clear() - but keep it's capacity

I'm merging many objects into a single vector containing render data (a mesh). 我正在将许多对象合并到包含渲染数据(网格)的单个向量中。 This vector gets cleared and refilled on each frame (well, almost). 这个向量被清除并重新填充每个帧(好吧,差不多)。

The issue is that clearing and then again reserving the vector size has a huge impact on performance in my case, because clear() may also change the capacity. 问题是,在我的情况下,清除然后再次保留向量大小会对性能产生巨大影响,因为clear()也可能会改变容量。

In other words, I need to control when the capacity of the vector gets changed. 换句话说,我需要控制向量的容量何时发生变化。 I want to keep the old capacity for quite some time, until I decide myself that it's time to change it. 我想保留旧容量很长一段时间,直到我决定自己是时候改变它了。

I see two options: 我看到两个选择:

  • Figure out how to control when the capacity of std::vector is about to change 弄清楚如何控制std::vector的容量何时即将发生变化
  • Implement a memory pool for large memory objects which will fetch a large data object of <= required size and reuse / release it as I need. 为大内存对象实现一个内存池,它将获取<=所需大小的大型数据对象,并根据需要重用/释放它。

Update 更新

In addition, what if for example a resize(10) , and later a resize(5) was called (just for illustration, multiply actual numbers by some millions)? 另外,如果调用resize(10) ,然后调用resize(5) (仅用于说明,将实际数字乘以数百万),该怎么办?

Will the later call to resize(5) cause the vector to, maybe, reallocate? 稍后调用resize(5)会导致向量重新分配吗?

Actually the clear member function keeps the vector capacity unchanged. 实际上, clear成员函数保持向量容量不变。 It only destroys (calls the destructor) each of the vector elements and sets the vector size to 0. 它只会破坏(调用析构函数)每个向量元素并将向量大小设置为0。

In this situation, at each iteration, I would call clear() to destroy all the vector elements, then call the member function reserve(size) which, in the case where the vector capacity is too small, will increase it to at least size . 在这种情况下,在每次迭代时,我会调用clear()来销毁所有向量元素,然后调用成员函数reserve(size) ,在向量容量太小的情况下,将其增加到至少size

This vector gets cleared and refilled on each frame (well, almost). 这个向量被清除并重新填充每个帧(好吧,差不多)。

I would recommend a different approach. 我会推荐一种不同的方法。

Create a class that acts as a buffer for the rendering data. 创建一个充当渲染数据缓冲区的类。

If I am not mistaken, you never reduce the capacity of the buffer. 如果我没有弄错,你永远不会减少缓冲区的容量。 You only increase its capacity when needed. 您只需在需要时增加容量。

Make sure that class is an implementation detail and only instance is ever constructed. 确保该类是实现细节,并且只构造实例。

Here's a skeletal implementation of what I am thinking. 这是我正在思考的骨架实现。

namespace Impl_Detail
{
   struct Buffer
   {
      size_t capacity;
      size_t size;
      std::vector<char> data;

      // Pick whatever default capacity makes sense for your need.
      Buffer(size_t cap = 100) : capacity_(cap), size_(0), data(cap) {}

      void ensureCapacity(size_t cap)
      {
         if ( capacity_ < cap )
         {
            capacity_ = cap;
            data.resize(capacity_);
         }
      }


      // Add any other helpful member functions as needed.
   };

   // Create an instance and use it in the implementation.
   Buffer theBuffer;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM