Jump to content

  • Log In with Google      Sign In   
  • Create Account


[C++] Custom containers vs STL


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 programci_84   Members   -  Reputation: 335

Like
0Likes
Like

Posted 01 March 2012 - 12:15 AM

Hi all,

I want to write my own container classes and I've some questions.

I've learnt that when we want to add one or more elements to an STL container (e.g. std::vector), it increases the size of the buffer more than we want. For example, if we have a 4-element-vector and if it's capacity is 4, when we want to insert 3 elements somewhere in it, it sets the capacity (or size, whatever you say) more than 7 (e.g. 10). I think this is done to avoid calling ::operator new frequently. But the size increases unnecessarily, right?

So, for an STL-like custom container, which one is more expensive?:
a) Using new/delete every time we want to add/delete elements (To keep the size of the buffer exact: for an n-element-buffer, the size is n*sizeof(element_type) ).
b) Increasing the capacity unnecessarily (To avoid calling ::operator new and ::operator delete every time).

btw, I've heard about "heap compaction" but I don't have any idea about that.

Hope I could describe.

Thx in advance.
-R
There's no "hard", and "the impossible" takes just a little time.

Sponsor:

#2 Hodgman   Moderators   -  Reputation: 24222

Like
3Likes
Like

Posted 01 March 2012 - 12:46 AM

capacity and size are different. Capacity is the amount of memory the vector has currently reserved, whereas size is the amount of that memory that is currently being used.

Whenever you attempt to increase size to be greater than capacity, the vector will have to internally reallocate it's buffer to a larger capacity. If this happens, it has to take a guess at what a good capacity is -- too small and it will have to resize again soon, too large and it will waste memory.

However, if you use vectors properly, this doesn't occur -- you can take control by using the reserve and resize functions, and the swap-with-empty-vector idiom to free allocations.
std::vector<int> test;
//capacity = 0, size = 0
test.reserve(10);//allocate enough space for 10 items
//capacity = 10, size = 0
for( int i=0; i!=10; i++ )
  test.push_back(i);//will not reallocate the buffer - there is enough space already
//capacity = 10, size = 10
test.push_back(10);//will reallocate the buffer as this is the 11th item
//capacity = >10 (maybe 20?), size = 11
test.resize(0);
//capacity = ?, size = 0
{
  vector<int> empty;
  test.swap( empty );
}
//capacity = 0, size = 0
test.resize(100);
//capacity = 100, size = 100


#3 Rattenhirn   Crossbones+   -  Reputation: 1546

Like
1Likes
Like

Posted 01 March 2012 - 01:25 AM

As Hodgman already explained, the capacity of vectors can indeed be controlled.

If you're looking to build a "custom" container that works for all possible use cases, you won't be able to do better than vector.

However, if you have a specific use case you need to optimize for, there might be a better way.

Now, to answer your actual question:

> a) Using new/delete every time we want to add/delete elements (To keep the size of the buffer exact: for an n-element-buffer, the size is n*sizeof(element_type) ).

This is most likely the worst possible solution. Keep in mind that new/delete are usually pretty expensive operations and that you need to copy the complete content of your container on every resize. If you're worried about memory wasting and do not need continuity in memory, you should consider making a vector of (smart) pointers to your element type. In this case, not much memory is wasted, because pointers are small and resizing is cheap, because the big / expensive to copy objects don't have to be moved.

> b) Increasing the capacity unnecessarily (To avoid calling ::operator new and ::operator delete every time).

That's exactly what vector is doing. Not only does it avoid new/delete, but also copying stuff around on resize.

I hope that helps!

#4 programci_84   Members   -  Reputation: 335

Like
0Likes
Like

Posted 01 March 2012 - 02:52 AM

Thank you all! You helped me a lot!
There's no "hard", and "the impossible" takes just a little time.

#5 japro   Members   -  Reputation: 883

Like
1Likes
Like

Posted 01 March 2012 - 03:07 AM

The reason why vector does that is performance. Essentially if instead of just resizing to the exact size you multiply the size by a factor, inserting lots of elements has asymptotically/amortized linear complexity (as opposed to n square). So that behavior isn't unnecessary at all. It is a choice that prioritizes algorithmic complexity over tight memory usage. Which is a very sensible choice given in a lot of cases you are not on ultra tight memory budgets.

#6 snowmanZOMG   Members   -  Reputation: 732

Like
0Likes
Like

Posted 01 March 2012 - 01:27 PM

So, for an STL-like custom container, which one is more expensive?:
a) Using new/delete every time we want to add/delete elements (To keep the size of the buffer exact: for an n-element-buffer, the size is n*sizeof(element_type) ).
b) Increasing the capacity unnecessarily (To avoid calling ::operator new and ::operator delete every time).


As mentioned previously, STL's vector is designed to optimize for access speed and insertion speed. Avoiding new and delete every time is not quite the whole picture for the reasoning of its memory allocation behavior. The main cost in vector's insertion is when a reallocation is made, the container maintains the old elements and appends the new one after the old elements in the newly allocated block. You need to copy all of those old elements from the old memory block to the new one.

This is where the O(n) cost comes from if you reallocate a constant size larger than the old memory block. When you increase the memory block size by a geometric factor, you can achieve an amortized constant time insertion because you end up performing the super expensive memory block copy far less frequently.

#7 iMalc   Crossbones+   -  Reputation: 2214

Like
0Likes
Like

Posted 02 March 2012 - 12:32 AM

It does more than just reduce the number of copies that occur duing the relocation involved in growing, it changes the insertion time from O(n) to amortized constant time i.e. O(1).
Consider the number of copies done to insert 512 items for a vector that doubles in size, starting at 8 items (real-world typical numbers).
To insert the 9th item, the capacity goes from 8 to 16 and the 8 existing items must be copied over...
To insert the 17th item, the capacity goes to 32 and 16 items must be copied over...
To insert the 33rd item 32 items must be copied over...
To insert the 65th item 64 items are copied over...
To insert the 129th item 128 items are copied...
To insert the 257th, 256 are copied...
The remainder require no growing.
Total number of copies for all 512 inserts: (8+16+32+64+128+256) = 504 (approximately the same number of items that were inserted!)

Growing by 1 each time instead gives you:
1+2+3+4+5+6+7+8+9+10+11+12+....511 copies, which equals 131072 copies. That's a boatload of copies, and it only gets rapidly worse.
If you want 100000 items - forget it, that's 5 billion copies required!!

But wait there's still more; Growing by a geometric factor (e.g doubling) also reduces memory fragmentation.

Forget about the fact that a vector allocates more memory than it often uses at any given moment. As that memory has not been touched yet it has not had to be fetched or cached, and it makes no difference whether the memory allocator itself reserves that extra room, or whether the container does it. I'm told this is especially true on Linux where allocated memory isn't even mapped to physical memory until it is touched.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS