[libcxx-commits] [PATCH] D73740: Add benchmarks for basic_string::erase

Martijn Vels via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Thu Jan 30 13:05:49 PST 2020


mvels created this revision.
mvels added a reviewer: EricWF.
Herald added subscribers: libcxx-commits, christof.
Herald added a project: libc++.
mvels edited the summary of this revision.
mvels added a reviewer: ldionne.
Herald added a subscriber: dexonsmith.
Herald added a reviewer: mclow.lists.

This change adds 2 benchmarks for basic_string::erase

EraseToEnd - erases data from a pos to end (npos). This is an intended fast path for V2
 EraseWithMove - erases data in the middle of the string. This is the slow path requiring moving data about.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D73740

Files:
  libcxx/benchmarks/string.bench.cpp


Index: libcxx/benchmarks/string.bench.cpp
===================================================================
--- libcxx/benchmarks/string.bench.cpp
+++ libcxx/benchmarks/string.bench.cpp
@@ -308,6 +308,58 @@
   }
 };
 
+template <class Length, class Opaque>
+struct StringEraseToEnd {
+  static void run(benchmark::State& state) {
+    constexpr bool opaque = Opaque{} == Opacity::Opaque;
+    constexpr int kNumStrings = 4 << 10;
+    std::string strings[kNumStrings];
+    const int mid = makeString(Length()).size() / 2;
+    while (state.KeepRunningBatch(kNumStrings)) {
+      state.PauseTiming();
+      for (int i = 0; i < kNumStrings; ++i) {
+        strings[i] = makeString(Length());
+      }
+      benchmark::DoNotOptimize(strings);
+      state.ResumeTiming();
+      for (int i = 0; i < kNumStrings; ++i) {
+        strings[i].erase(maybeOpaque(mid, opaque),
+                         maybeOpaque(std::string::npos, opaque));
+      }
+    }
+  }
+
+  static std::string name() {
+    return "BM_StringEraseToEnd" + Length::name() + Opaque::name();
+  }
+};
+
+template <class Length, class Opaque>
+struct StringEraseWithMove {
+  static void run(benchmark::State& state) {
+    constexpr bool opaque = Opaque{} == Opacity::Opaque;
+    constexpr int kNumStrings = 4 << 10;
+    std::string strings[kNumStrings];
+    const int n = makeString(Length()).size() / 2;
+    const int pos = n / 2;
+    while (state.KeepRunningBatch(kNumStrings)) {
+      state.PauseTiming();
+      for (int i = 0; i < kNumStrings; ++i) {
+        strings[i] = makeString(Length());
+      }
+      benchmark::DoNotOptimize(strings);
+      state.ResumeTiming();
+      for (int i = 0; i < kNumStrings; ++i) {
+        strings[i].erase(maybeOpaque(pos, opaque), maybeOpaque(n, opaque));
+      }
+    }
+  }
+
+  static std::string name() {
+    return "BM_StringEraseWithMove" + Length::name() + Opaque::name();
+  }
+};
+
 template <class Opaque>
 struct StringAssignAsciizMix {
   static void run(benchmark::State& state) {
@@ -556,6 +608,8 @@
   makeCartesianProductBenchmark<StringDestroy, AllLengths>();
   makeCartesianProductBenchmark<StringResizeDefaultInit, AllLengths,
                                 AllOpacity>();
+  makeCartesianProductBenchmark<StringEraseToEnd, AllLengths, AllOpacity>();
+  makeCartesianProductBenchmark<StringEraseWithMove, AllLengths, AllOpacity>();
   makeCartesianProductBenchmark<StringRelational, AllRelations, AllLengths,
                                 AllLengths, AllDiffTypes>();
   makeCartesianProductBenchmark<StringRelationalLiteral, AllRelations,


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D73740.241558.patch
Type: text/x-patch
Size: 2594 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20200130/8978db18/attachment.bin>


More information about the libcxx-commits mailing list