mirror of
https://github.com/llvm/llvm-project.git
synced 2025-05-01 22:46:06 +00:00

As a follow-up to #113852, this PR optimizes the performance of the `insert(const_iterator pos, InputIt first, InputIt last)` function for `input_iterator`-pair inputs in `std::vector` for cases where reallocation occurs during insertion. Additionally, this optimization enhances exception safety by replacing the traditional `try-catch` mechanism with a modern exception guard for the `insert` function. The optimization targets cases where insertion trigger reallocation. In scenarios without reallocation, the implementation remains unchanged. Previous implementation ----------------------- The previous implementation of `insert` is inefficient in reallocation scenarios because it performs the following steps separately: - `reserve()`: This leads to the first round of relocating old elements to new memory; - `rotate()`: This leads to the second round of reorganizing the existing elements; - Move-forward: Moves the elements after the insertion position to their final positions. - Insert: performs the actual insertion. This approach results in a lot of redundant operations, requiring the elements to undergo three rounds of relocations/reorganizations to be placed in their final positions. Proposed implementation ----------------------- The proposed implementation jointly optimize the above 4 steps in the previous implementation such that each element is placed in its final position in just one round of relocation. Specifically, this optimization reduces the total cost from 2 relocations + 1 std::rotate call to just 1 relocation, without needing to call `std::rotate`, thereby significantly improving overall performance.
109 lines
4.2 KiB
C++
109 lines
4.2 KiB
C++
//===----------------------------------------------------------------------===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
|
|
|
|
#include <cstdint>
|
|
#include <cstdlib>
|
|
#include <cstring>
|
|
#include <deque>
|
|
#include <functional>
|
|
#include <memory>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include "benchmark/benchmark.h"
|
|
#include "ContainerBenchmarks.h"
|
|
#include "../GenerateInput.h"
|
|
|
|
using namespace ContainerBenchmarks;
|
|
|
|
constexpr std::size_t TestNumInputs = 1024;
|
|
|
|
BENCHMARK_CAPTURE(BM_ConstructSize, vector_byte, std::vector<unsigned char>{})->Arg(5140480);
|
|
|
|
BENCHMARK_CAPTURE(BM_CopyConstruct, vector_int, std::vector<int>{})->Arg(5140480);
|
|
|
|
BENCHMARK_CAPTURE(BM_Assignment, vector_int, std::vector<int>{})->Arg(5140480);
|
|
|
|
BENCHMARK_CAPTURE(BM_ConstructSizeValue, vector_byte, std::vector<unsigned char>{}, 0)->Arg(5140480);
|
|
|
|
BENCHMARK_CAPTURE(BM_ConstructIterIter, vector_char, std::vector<char>{}, getRandomIntegerInputs<char>)
|
|
->Arg(TestNumInputs);
|
|
|
|
BENCHMARK_CAPTURE(BM_ConstructIterIter, vector_size_t, std::vector<size_t>{}, getRandomIntegerInputs<size_t>)
|
|
->Arg(TestNumInputs);
|
|
|
|
BENCHMARK_CAPTURE(BM_ConstructIterIter, vector_string, std::vector<std::string>{}, getRandomStringInputs)
|
|
->Arg(TestNumInputs);
|
|
|
|
BENCHMARK_CAPTURE(BM_ConstructFromRange, vector_char, std::vector<char>{}, getRandomIntegerInputs<char>)
|
|
->Arg(TestNumInputs);
|
|
|
|
BENCHMARK_CAPTURE(BM_ConstructFromRange, vector_size_t, std::vector<size_t>{}, getRandomIntegerInputs<size_t>)
|
|
->Arg(TestNumInputs);
|
|
|
|
BENCHMARK_CAPTURE(BM_ConstructFromRange, vector_string, std::vector<std::string>{}, getRandomStringInputs)
|
|
->Arg(TestNumInputs);
|
|
|
|
BENCHMARK_CAPTURE(BM_Pushback_no_grow, vector_int, std::vector<int>{})->Arg(TestNumInputs);
|
|
|
|
BENCHMARK_CAPTURE(BM_erase_iter_in_middle, vector_int, std::vector<int>{}, getRandomIntegerInputs<int>)
|
|
->Range(TestNumInputs, TestNumInputs * 10);
|
|
BENCHMARK_CAPTURE(BM_erase_iter_in_middle, vector_string, std::vector<std::string>{}, getRandomStringInputs)
|
|
->Range(TestNumInputs, TestNumInputs * 10);
|
|
|
|
BENCHMARK_CAPTURE(BM_erase_iter_at_start, vector_int, std::vector<int>{}, getRandomIntegerInputs<int>)
|
|
->Range(TestNumInputs, TestNumInputs * 10);
|
|
BENCHMARK_CAPTURE(BM_erase_iter_at_start, vector_string, std::vector<std::string>{}, getRandomStringInputs)
|
|
->Range(TestNumInputs, TestNumInputs * 10);
|
|
|
|
template <class T>
|
|
void bm_grow(benchmark::State& state) {
|
|
for (auto _ : state) {
|
|
std::vector<T> vec;
|
|
benchmark::DoNotOptimize(vec);
|
|
for (size_t i = 0; i != 2048; ++i)
|
|
vec.emplace_back();
|
|
benchmark::DoNotOptimize(vec);
|
|
}
|
|
}
|
|
BENCHMARK(bm_grow<int>);
|
|
BENCHMARK(bm_grow<std::string>);
|
|
BENCHMARK(bm_grow<std::unique_ptr<int>>);
|
|
BENCHMARK(bm_grow<std::deque<int>>);
|
|
|
|
BENCHMARK_CAPTURE(BM_AssignInputIterIter, vector_int, std::vector<int>{}, getRandomIntegerInputs<int>)
|
|
->Args({TestNumInputs, TestNumInputs});
|
|
|
|
BENCHMARK_CAPTURE(
|
|
BM_AssignInputIterIter<32>, vector_string, std::vector<std::string>{}, getRandomStringInputsWithLength)
|
|
->Args({TestNumInputs, TestNumInputs});
|
|
|
|
BENCHMARK_CAPTURE(BM_AssignInputIterIter<100>,
|
|
vector_vector_int,
|
|
std::vector<std::vector<int>>{},
|
|
getRandomIntegerInputsWithLength<int>)
|
|
->Args({TestNumInputs, TestNumInputs});
|
|
|
|
BENCHMARK_CAPTURE(BM_Insert_InputIterIter_NoRealloc, vector_int, std::vector<int>(100, 1), getRandomIntegerInputs<int>)
|
|
->Arg(514048);
|
|
BENCHMARK_CAPTURE(
|
|
BM_Insert_InputIterIter_Realloc_HalfFilled, vector_int, std::vector<int>{}, getRandomIntegerInputs<int>)
|
|
->Arg(514048);
|
|
BENCHMARK_CAPTURE(BM_Insert_InputIterIter_Realloc_NearFull, vector_int, std::vector<int>{}, getRandomIntegerInputs<int>)
|
|
->Arg(514048);
|
|
BENCHMARK_CAPTURE(
|
|
BM_Insert_InputIterIter_Realloc_HalfFilled, vector_string, std::vector<std::string>{}, getSSORandomStringInputs)
|
|
->Arg(514048);
|
|
BENCHMARK_CAPTURE(
|
|
BM_Insert_InputIterIter_Realloc_NearFull, vector_string, std::vector<std::string>{}, getSSORandomStringInputs)
|
|
->Arg(514048);
|
|
|
|
BENCHMARK_MAIN();
|