Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Standard container re-allocation multipliers across popular toolchains

Containers like std::basic_string and std::vector perform automatic re-allocations when internal capacity runs out. The standard specifies that, after a re-allocation, .capacity() >= .size().

What are some of the actual multipliers used by mainstream toolchains when performing re-allocations?


Update

So far, I have:

Dinkumware: 1.5 (ships with MSVS and possibly ICC)

GNU libstdc++: 2 (ships with GCC and possibly ICC)

RW/Apache stdcxx: 1.618 (aka φ)

STLport: 2

like image 337
Lightness Races in Orbit Avatar asked Mar 23 '11 11:03

Lightness Races in Orbit


2 Answers

New answer for an old question.

Rationale: The answer can be answered programatically, and with online compilers, relatively easily. Here is a program that can help you answer this question:

#include <climits>
#include <cstddef>
#include <cstdlib>
#ifndef _MSC_VER
#   include <cxxabi.h>
#endif
#include <iostream>
#include <memory>
#include <string>
#include <typeinfo>
#include <type_traits>
#include <limits>
#include <vector>
#include <string>

template <typename T>
std::string
type_name()
{
    typedef typename std::remove_reference<T>::type TR;
    std::unique_ptr<char, void(*)(void*)> own
           (
#ifndef _MSC_VER
                abi::__cxa_demangle(typeid(TR).name(), nullptr,
                                           nullptr, nullptr),
#else
                nullptr,
#endif
                std::free
           );
    std::string r = own != nullptr ? own.get() : typeid(TR).name();
    if (std::is_const<TR>::value)
        r += " const";
    if (std::is_volatile<TR>::value)
        r += " volatile";
    if (std::is_lvalue_reference<T>::value)
        r += "&";
    else if (std::is_rvalue_reference<T>::value)
        r += "&&";
    return r;
}

template <class C>
void
test()
{
    C c;
    std::cout << type_name<C>() << ":\n";
    std::size_t c0 = c.capacity();
    std::cout << "    Initial capacity is " << c0 << '\n';
    c.resize(c0);
    for (int i = 0; i < 10; ++i)
    {
        c.push_back(typename C::value_type{});
        std::size_t c1 = c.capacity();
        if (c0 != 0)
        {
            float f = static_cast<float>(c1)/c0;
            std::cout << "    growth factor appears to be " << f << '\n';
        }
        c0 = c1;
        c.resize(c0);
    }
}

int
main()
{
    test<std::vector<int>>();
    test<std::string>();
}

Most of the complexity is a bit unnecessary as it is just to get type_name working.

libstdc++:

http://melpon.org/wandbox/permlink/njaIG2uiR2vlCLZz

appears to answer a solid 2 for both vector and string.

VS:

http://webcompiler.cloudapp.net

is very close to 1.5 for both vector and string.

libc++

http://melpon.org/wandbox/permlink/mXshrLJHgNuvE1mD

is very close to 2 for both vector and string.

Note that this program also tells you what the short string buffer is for string: 15 for both libstdc++ and VS, and 22 for libc++.

like image 68
Howard Hinnant Avatar answered Oct 12 '22 21:10

Howard Hinnant


Dinkumware STL (shipped with Visual Studio) uses a 1.5 multiplier, Gcc uses 2. I cannot really tell for the rest, but I think those are the most often used numbers. (IIRC, I read once that most implementations used 2)

As a side comment, you are correctly calling it a multiplier since the standard requires the growth to be (at least) geometrical.

like image 42
David Rodríguez - dribeas Avatar answered Oct 12 '22 22:10

David Rodríguez - dribeas