Lesson learned the hard way: trust the implementation to do the right thing! Everything else has been strikethrough’ed because my approach was flowed from the beginning. See the quoted comments at the bottom of this post.

Thanks everyone who commented! Looks like this blogging thing will be a great learning exercise for me 🙂

From https://en.cppreference.com/w/cpp/container/vector/shrink_to_fit

It is a non-binding request to reduce capacity() to size(). It depends on the implementation whether the request is fulfilled.

In other words, it can do absolutely nothing! Now what?
Let’s test programmatically what the implementation of STL is actually doing, and in case it does nothing let’s shrink the vector another way.

The test:

Self explanatory I hope 🙂 we create a vector of 1 std::byte, reserve space for 2 entries, then call shrink_to_fit. Finally we test the capacity; if it’s now 1 instead of 2, the call worked. Great! But what if it failed to shrink the vector?

shrink_to_fit that works:

We first test (once, thanks to static) if shrink_to_fit works, if it does, we invoke it. Otherwise we fallback onto a temporary -> swap trick. This works because the temporary vector created from v will have capacity equal to v‘s size. See here: https://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Shrink-to-fit

Test of our solution:

Output of the program from my Xcode console:

10
5
Program ended with exit code: 0

Complete listing:


UPDATE!

I shared my blog on Reddit CPP as well as comp.lang.c++ and received many comments about my shrink_to_fit approach:

Ouch 😉 Very constrictive critique of my implementation. Thank you sakarri and Joerg! So I decided to rewrite the initial shrink_to_fit function, here it is:

In this approach we skip the shrink_to_fit_test and go straight to the vector’s call. If it doesn’t do what we want we do the temporary -> swap trick.

Here’s the complete listing again:


Below are some of the comments I received:

Just figured I’d comment on your post about how  shrink_to_fit sucks. It’s your solution that is significantly inferior in every possible way whereas the standard’s approach is optimal. That may seem blunt but it’s worth understanding why.
First your  shrink_to_fit_test is too pessimistic since it assumes that an implementation either always performs the shrink or never performs it when actually the situation is a lot more complex than that. The whole purpose of the standard leaving it up to the implementation is that for small vectors, forcing a  shrink_to_fit potentially wastes a lot more memory than not doing anything at all. The reason is two fold, first because the default allocator doesn’t allocate memory less than 16 bytes to begin with. In other words a vector of 10 bytes takes up just as much memory as a vector of 1 byte so forcing a  shrink_to_fit in that situation doesn’t save you anything. Furthermore with your implementation it actually ends up potentially causing you to waste 4 kilobytes of RAM because your solution forces a new memory allocation and given how virtual memory works, that allocation can result in having to fetch a new page, so you’ve now wasted 4 kilobytes of physical RAM for nothing.
In situations where something like this seems fishy, it’s best to review the literature on the issue to understand why the committee made the decision they did. Sometimes they do screw up, but that’s not the initial conclusion you should jump to. Take a look at how GCC implements  shrink_to_fit, it delegates the operation to the allocator so the allocator can decide whether to carry it out since the allocator knows more about the memory system than  vector does. It also implements the operation in a way that can avoid any reallocation period and it does its best to provide the strong exception guarantee.
The standard didn’t make this part of the STL non-binding so that the people who work incredibly hard to implement it could be lazy, the standard made it non-binding so that if there are potential optimizations that save more memory and consume fewer CPU cycles by avoiding it, the implementation would be free to leverage them. This is explicitly noted in the standard as follows:
Note: The request is non-binding to allow latitude for implementation-specific optimizations. —end note
Basically there is no situation where you would want to use your implementation of  shrink_to_fit over the one provided by the standard library.

In particular, the January 27 posting “shrink_to_fit that doesn’t suck” 
still provides only solutions (to a non-existing problem) that do suck…

7 Replies to “shrink_to_fit that doesn’t suck”

  1. Please use a monospaced font for code, your code samples look horrible, sorry.
    Look at fluentcpp how nice the samples look.

  2. Great blog, Mr. Vorbrodt! Look forward to future posts and lively blog! Don’t forget to inject Polish humor now and then :).

  3. Hello.

    One could imagine that shrink_to_fit() might actually work different in different scenarios.
    So testing once in a certain scenario does not necessarily give a result that is valid for every call to shrink_to_fit(). (consider different item types, different allocator, optimizations, …)

    And to be sure, you have to test on your actual data. So I’d skip the test and call shrink_to_fit() directly, check capacity and if it did not shrink, do the swap thing.

  4. Uhm.

    I’ll assume that the last shown implementation,

    is the solution (to the problem of using too much memory?) that you’ve currently adopted.

    It does fix the problem with a

    shrink_to_fit

    implementation that does nothing, ever.

    And as far as I can tell with simple testing, with g++ 8.2.0 and Visual C++ 2017 it doesn’t do worse than their

    vector::shrink_to_fit

    , because that code appears to resize down to exactly the size.

    But consider some client code that calls this function often for the same vector that’s changing slightly in size over time. Each call will then cause (1) a costly dynamic allocation, and (2) a potentially costly copying of the items, and at minimum a move of the items. That’s then a lot of extreme time overhead, inefficiency, a great cost, with no real advantage in saved memory.

    And when you have a

    vector::shrink_to_fit

    implementation that avoids needless allocations by not shrinking when the capacity is just over the size (especially, when it’s within what you’d get by upping the size to the allocation granularity), then this code will also just do a lot of needless dynamic allocations that don’t help to conserve memory, but do use a lot of time.

    Unfortunately there’s no way to tell what the allocation granularity of a C++

    new

    expression is. An implementation might use a small objects sub-allocator that greatly reduces per-item overhead for very small items, and if the implementation doesn’t, then a particular class may do so (e.g. using the Loki small objects sub-allocator). But checking whether the capacity is reasonably close to the size, say within a certain percentage, instead of exactly the size, and perhaps also accounting for a potential allocation granularity in the new capacity value, would help to avoid the extreme inefficiency.

Leave a Reply