[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
This commit is contained in:
Eric Fiselier 2020-01-07 16:31:06 -05:00
parent 9685cf709f
commit 0c5102bd93
1 changed files with 127 additions and 0 deletions

View File

@ -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,