Photo by ThisisEngineering RAEng

Memory Resizing

So I never really considered why the resize of vector used a constant expansion of 1.5 or 2 (in some popular implementations). That was until I did my previous article series “Vector” where I concentrated a lot on resource management and did a section on resizing the vector. Initially, I tried to be clever in the code, a mistake. I used a resize value of 1.62 (an approximation of Phi) because I vaguely remembered reading an article that this was the optimum resize factor. When I put this out for code review, it was pointed out that this value was too large, the optimum value must be less than or equal to Phi (1.6180339887), and that exceeding this limit made things much worse. ...

March 25, 2016 · 3 min · Loki Astari (C)2016
Photo by Matthew Brodeur

Vector - Simple Optimizations

Now that we have used std::is_nothrow_move_constructible, we can also examine a couple of other types available in the template utility library. Optimized Destruction Since we have to manually call the destructor on all objects in the container (because we are using placement new), we can look to see if we can optimize that. The type std::is_trivially_destructible detects if the type is Trivially destructible. This basically means that there will be no side effects from the destructor (See: Section 12.4 Paragraph 5 of the standard). For types we don’t need to call the destructor of the object. For the Vector class, this means we can eliminate the call to the destructor but, more importantly, the loop. ...

March 19, 2016 · 5 min · Loki Astari, (C)2016
Photo by Alex Knight

Vector - Resize

Because resizing a vector is expensive, the std::vector class uses exponential growth to minimize the number of times that the vector is resized: a technique we replicate in this version. But every now and then, you still need to resize the internal buffer. In the current version, resizing the vector requires allocating a new buffer and copying all the members into it. We use the copy and swap idiom to provide the strong exception guarantee (If an exception is thrown, all resources are cleaned up, and the object remains unchanged). ...

March 12, 2016 · 7 min · Loki Astari, (C)2016
Photo by Ben Griffiths

Vector - Resource Management Copy Swap

In the previous article, I went over basic allocation for a Vector like class. In this article, I want to put some detail around the copy assignment operator and resizing the underlying Vector. Unlike the other methods previously discussed, these methods have to deal with both construction and destruction of elements and the potential of exceptions interrupting the processes. The goal is to provide exception safe methods that provide the strong exception guarantee for the object and do not leak resources. ...

February 29, 2016 · 7 min · Loki Astari, (C)2016
Photo by Oscar Nilsson

Vector - Resource Management Allocation

Many new developers of C++ attempt to build a Vector like container as a learning process. Getting a simple version of this working for POD types (like int) is not that complicated. The next step in getting this working for arbitrary data types takes a significant leap forward in thinking in C++, especially when you start looking at efficiency and exception safety. This set of five articles looks at building an efficient Vector implementation. I show some of the common mistakes and explain why and how to resolve the problems: ...

February 27, 2016 · 11 min · Loki Astari, (C)2016