stl: time spent for adding elements to a vector

Please explain where the legs can grow in time costs when working with the addition to the vector:

There are the following data types:

// элемент и совокупность элементов
using element_t = std::pair<int, int>;
using elements_t = std::vector<element_t>;

// ветка элементов
struct fork_t {
    elements_t sequence;
    int        weight;

    fork_t () {
         sequence.reserve(c_elements_count);
    }
};

// цепочка элементов
struct chain_t {
    fork_t     forks[2];
    int        weight;
};

// совокупность цепочек элементов
using chains_t = std::vector<chain_t>;

That is, in general, not very complex data

In the main algorithm, chains are formed (there are fewer of them than the elements that are included in the chains)

chains_t chains;
chains.reverse(c_chains_count);

for (...)
{
    // сразу добавляем цепочку, чтобы избежать лишнего копирования элементов
    chains.push_back(chain_t());
    auto& it = chains.back();

    // добавляем элементы
    add_elements(it);
}

Inside the add_elements loop, the{[9] elements are added]}

chain.fork[index].sequence.emplace_back(value1, value2);

So when I looked in the profiler, it turned out that on chains.push_back(chain_t()); several times more time is spent than on all emplace_back inside add_elements

At the same time, the number of chains is much (several times) less than the number of elements added to the chains.

I have a question about this-what can this behavior be related to? The types are simple, some complex manipulations with them in the constructors or somewhere else do not occur, then why add empty ones !!! chains for which only 2 reserve() are called for each spending so much time?

Author: Zhihar, 2021-01-26