diff --git a/libcxx/benchmarks/string.bench.cpp b/libcxx/benchmarks/string.bench.cpp --- a/libcxx/benchmarks/string.bench.cpp +++ b/libcxx/benchmarks/string.bench.cpp @@ -124,6 +124,20 @@ } } +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,87 @@ static std::string name() { return "BM_StringMove" + Length::name(); } }; +template +struct StringAssignStr { + static void run(benchmark::State& state) { + constexpr bool opaque = Opaque{} == Opacity::Opaque; + constexpr size_t 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 +struct StringAssignAsciiz { + static void run(benchmark::State& state) { + constexpr bool opaque = Opaque{} == Opacity::Opaque; + constexpr size_t 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 +struct StringAssignAsciizMix { + static void run(benchmark::State& state) { + constexpr Opaque O{}; + constexpr auto D = DiffType::Control; + constexpr size_t 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 { static constexpr const char* Names[] = {"Eq", "Less", "Compare"}; @@ -426,6 +521,11 @@ makeCartesianProductBenchmark(); + + makeCartesianProductBenchmark(); + makeCartesianProductBenchmark(); + makeCartesianProductBenchmark(); + makeCartesianProductBenchmark(); makeCartesianProductBenchmark(); makeCartesianProductBenchmark(); diff --git a/libcxx/include/string b/libcxx/include/string --- a/libcxx/include/string +++ b/libcxx/include/string @@ -2260,7 +2260,7 @@ if (this != &__str) { __copy_assign_alloc(__str); - assign(__str.data(), __str.size()); + return assign(__str.data(), __str.size()); } return *this; }