diff options
author | Eric Fiselier <eric@efcs.ca> | 2020-01-07 16:31:06 -0500 |
---|---|---|
committer | Eric Fiselier <eric@efcs.ca> | 2020-01-07 16:31:40 -0500 |
commit | 0c5102bd939131b27105b74e73fc25b90207ef36 (patch) | |
tree | 95e41ea12198248892564be0ac58355461a609b8 /libcxx | |
parent | 9685cf709ff29843814de1ea602572052d88acec (diff) | |
download | bcm5719-llvm-0c5102bd939131b27105b74e73fc25b90207ef36.tar.gz bcm5719-llvm-0c5102bd939131b27105b74e73fc25b90207ef36.zip |
[libc++] Add additional benchmark functions to libcxx/benchmarks/string.bench
This change adds the following benchmarks:
- StringAssignStr
Assign a const basic::string& value
- StringAssignAsciiz
Assign a const char* asciiz value
StringAssignAsciizMix
Assign mixed long/short const char* asciiz values
- StringResizeDefaultInit
Resize default init benchmark
Patch by Martijn Vels (mvels@google.com)
Reviewed as D72343
Diffstat (limited to 'libcxx')
-rw-r--r-- | libcxx/benchmarks/string.bench.cpp | 127 |
1 files changed, 127 insertions, 0 deletions
diff --git a/libcxx/benchmarks/string.bench.cpp b/libcxx/benchmarks/string.bench.cpp index c4f209039c8..fe8a1c533d9 100644 --- a/libcxx/benchmarks/string.bench.cpp +++ b/libcxx/benchmarks/string.bench.cpp @@ -124,6 +124,20 @@ TEST_ALWAYS_INLINE const char* getHugeString(DiffType D) { } } +TEST_ALWAYS_INLINE const char* getString(Length L, + DiffType D = DiffType::Control) { + switch (L) { + case Length::Empty: + return ""; + case Length::Small: + return getSmallString(D); + case Length::Large: + return getLargeString(D); + case Length::Huge: + return getHugeString(D); + } +} + TEST_ALWAYS_INLINE std::string makeString(Length L, DiffType D = DiffType::Control, Opacity O = Opacity::Transparent) { @@ -220,6 +234,112 @@ struct StringMove { static std::string name() { return "BM_StringMove" + Length::name(); } }; +template <class Length, class Opaque> +struct StringResizeDefaultInit { + static void run(benchmark::State& state) { + constexpr bool opaque = Opaque{} == Opacity::Opaque; + constexpr int kNumStrings = 4 << 10; + size_t length = makeString(Length()).size(); + std::string strings[kNumStrings]; + while (state.KeepRunningBatch(kNumStrings)) { + state.PauseTiming(); + for (int i = 0; i < kNumStrings; ++i) { + std::string().swap(strings[i]); + } + benchmark::DoNotOptimize(strings); + state.ResumeTiming(); + for (int i = 0; i < kNumStrings; ++i) { + strings[i].__resize_default_init(maybeOpaque(length, opaque)); + } + } + } + + static std::string name() { + return "BM_StringResizeDefaultInit" + Length::name() + Opaque::name(); + } +}; + +template <class Length, class Opaque> +struct StringAssignStr { + static void run(benchmark::State& state) { + constexpr bool opaque = Opaque{} == Opacity::Opaque; + constexpr int kNumStrings = 4 << 10; + std::string src = makeString(Length()); + std::string strings[kNumStrings]; + while (state.KeepRunningBatch(kNumStrings)) { + state.PauseTiming(); + for (int i = 0; i < kNumStrings; ++i) { + std::string().swap(strings[i]); + } + benchmark::DoNotOptimize(strings); + state.ResumeTiming(); + for (int i = 0; i < kNumStrings; ++i) { + strings[i] = *maybeOpaque(&src, opaque); + } + } + } + + static std::string name() { + return "BM_StringAssignStr" + Length::name() + Opaque::name(); + } +}; + +template <class Length, class Opaque> +struct StringAssignAsciiz { + static void run(benchmark::State& state) { + constexpr bool opaque = Opaque{} == Opacity::Opaque; + constexpr int kNumStrings = 4 << 10; + std::string strings[kNumStrings]; + while (state.KeepRunningBatch(kNumStrings)) { + state.PauseTiming(); + for (int i = 0; i < kNumStrings; ++i) { + std::string().swap(strings[i]); + } + benchmark::DoNotOptimize(strings); + state.ResumeTiming(); + for (int i = 0; i < kNumStrings; ++i) { + strings[i] = maybeOpaque(getString(Length()), opaque); + } + } + } + + static std::string name() { + return "BM_StringAssignAsciiz" + Length::name() + Opaque::name(); + } +}; + +template <class Opaque> +struct StringAssignAsciizMix { + static void run(benchmark::State& state) { + constexpr auto O = Opaque{}; + constexpr auto D = DiffType::Control; + constexpr int kNumStrings = 4 << 10; + std::string strings[kNumStrings]; + while (state.KeepRunningBatch(kNumStrings)) { + state.PauseTiming(); + for (int i = 0; i < kNumStrings; ++i) { + std::string().swap(strings[i]); + } + benchmark::DoNotOptimize(strings); + state.ResumeTiming(); + for (int i = 0; i < kNumStrings - 7; i += 8) { + strings[i + 0] = maybeOpaque(getSmallString(D), O == Opacity::Opaque); + strings[i + 1] = maybeOpaque(getSmallString(D), O == Opacity::Opaque); + strings[i + 2] = maybeOpaque(getLargeString(D), O == Opacity::Opaque); + strings[i + 3] = maybeOpaque(getSmallString(D), O == Opacity::Opaque); + strings[i + 4] = maybeOpaque(getSmallString(D), O == Opacity::Opaque); + strings[i + 5] = maybeOpaque(getSmallString(D), O == Opacity::Opaque); + strings[i + 6] = maybeOpaque(getLargeString(D), O == Opacity::Opaque); + strings[i + 7] = maybeOpaque(getSmallString(D), O == Opacity::Opaque); + } + } + } + + static std::string name() { + return "BM_StringAssignAsciizMix" + Opaque::name(); + } +}; + enum class Relation { Eq, Less, Compare }; struct AllRelations : EnumValuesAsTuple<AllRelations, Relation, 3> { static constexpr const char* Names[] = {"Eq", "Less", "Compare"}; @@ -426,9 +546,16 @@ int main(int argc, char** argv) { makeCartesianProductBenchmark<StringConstructDestroyCStr, AllLengths, AllOpacity>(); + + makeCartesianProductBenchmark<StringAssignStr, AllLengths, AllOpacity>(); + makeCartesianProductBenchmark<StringAssignAsciiz, AllLengths, AllOpacity>(); + makeCartesianProductBenchmark<StringAssignAsciizMix, AllOpacity>(); + makeCartesianProductBenchmark<StringCopy, AllLengths>(); makeCartesianProductBenchmark<StringMove, AllLengths>(); makeCartesianProductBenchmark<StringDestroy, AllLengths>(); + makeCartesianProductBenchmark<StringResizeDefaultInit, AllLengths, + AllOpacity>(); makeCartesianProductBenchmark<StringRelational, AllRelations, AllLengths, AllLengths, AllDiffTypes>(); makeCartesianProductBenchmark<StringRelationalLiteral, AllRelations, |