one more simple folly::join optimization
authorPhilip Pronin <philipp@fb.com>
Fri, 17 Aug 2012 23:21:26 +0000 (16:21 -0700)
committerTudor Bosman <tudorb@fb.com>
Sun, 26 Aug 2012 18:13:46 +0000 (11:13 -0700)
Summary:
Before:

_bin/folly/test/string_test --benchmark --bm_regex=join --bm_min_usec=1000000
============================================================================
folly/test/StringTest.cpp                       relative  time/iter  iters/s
============================================================================
joinCharStr                                                  3.59us  278.29K
joinStrStr                                                   4.44us  225.09K
joinInt                                                     10.55us   94.76K
============================================================================

And after:

_bin/folly/test/string_test --benchmark --bm_regex=join --bm_min_usec=1000000
============================================================================
folly/test/StringTest.cpp                       relative  time/iter  iters/s
============================================================================
joinCharStr                                                  3.61us  277.01K
joinStrStr                                                   3.77us  264.97K
joinInt                                                      9.92us  100.81K
============================================================================

Test Plan: unittests, benchmark

Reviewed By: tudorb@fb.com

FB internal diff: D552364

folly/String-inl.h
folly/test/StringTest.cpp

index 02d376d82f06e02fd4b64b30f6758705091cc0ca..4bff753e0ab757b291a9a6154a6bf173f31b321c 100644 (file)
@@ -312,6 +312,11 @@ void internalJoinAppend(Delim delimiter,
                         Iterator end,
                         String& output) {
   assert(begin != end);
                         Iterator end,
                         String& output) {
   assert(begin != end);
+  if (std::is_same<Delim, StringPiece>::value &&
+      delimSize(delimiter) == 1) {
+    internalJoinAppend(delimFront(delimiter), begin, end, output);
+    return;
+  }
   toAppend(*begin, &output);
   while (++begin != end) {
     toAppend(delimiter, *begin, &output);
   toAppend(*begin, &output);
   while (++begin != end) {
     toAppend(delimiter, *begin, &output);
index ec01eb6c628c26dce6409208381ce19c99022a2d..04281c65decde19a080bd2b3f7da101348a7228d 100644 (file)
@@ -761,7 +761,16 @@ BENCHMARK(boost_splitOnSingleChar, iters) {
   }
 }
 
   }
 }
 
-BENCHMARK(joinStr, iters) {
+BENCHMARK(joinCharStr, iters) {
+  static const std::vector<std::string> input = {
+    "one", "two", "three", "four", "five", "six", "seven" };
+  for (int i = 0; i < iters << 4; ++i) {
+    std::string output;
+    folly::join(':', input, output);
+  }
+}
+
+BENCHMARK(joinStrStr, iters) {
   static const std::vector<std::string> input = {
     "one", "two", "three", "four", "five", "six", "seven" };
   for (int i = 0; i < iters << 4; ++i) {
   static const std::vector<std::string> input = {
     "one", "two", "three", "four", "five", "six", "seven" };
   for (int i = 0; i < iters << 4; ++i) {