folly::Unit::Drop.
[folly.git] / folly / Range.cpp
index 42ee048d638dc25738794c70b28b95c86325868e..bf4e28d0f7f23954e39e7d96a8cff9ffde2a6aa6 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2014 Facebook, Inc.
+ * Copyright 2015 Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -32,36 +32,6 @@ namespace folly {
 const AsciiCaseSensitive asciiCaseSensitive = AsciiCaseSensitive();
 const AsciiCaseInsensitive asciiCaseInsensitive = AsciiCaseInsensitive();
 
-std::ostream& operator<<(std::ostream& os, const StringPiece piece) {
-  os.write(piece.start(), piece.size());
-  return os;
-}
-
-std::ostream& operator<<(std::ostream& os, const MutableStringPiece piece) {
-  os.write(piece.start(), piece.size());
-  return os;
-}
-
-namespace detail {
-
-size_t qfind_first_byte_of_memchr(const StringPiece haystack,
-                                  const StringPiece needles) {
-  size_t best = haystack.size();
-  for (char needle: needles) {
-    const void* ptr = memchr(haystack.data(), needle, best);
-    if (ptr) {
-      auto found = static_cast<const char*>(ptr) - haystack.data();
-      best = std::min<size_t>(best, found);
-    }
-  }
-  if (best == haystack.size()) {
-    return StringPiece::npos;
-  }
-  return best;
-}
-
-}  // namespace detail
-
 namespace {
 
 // It's okay if pages are bigger than this (as powers of two), but they should
@@ -95,15 +65,13 @@ size_t qfind_first_byte_of_needles16(const StringPiece haystack,
   DCHECK(!haystack.empty());
   DCHECK(!needles.empty());
   DCHECK_LE(needles.size(), 16);
-  // benchmarking shows that memchr beats out SSE for small needle-sets
-  // with large haystacks.
   if ((needles.size() <= 2 && haystack.size() >= 256) ||
       // must bail if we can't even SSE-load a single segment of haystack
       (haystack.size() < 16 &&
        PAGE_FOR(haystack.end() - 1) != PAGE_FOR(haystack.data() + 15)) ||
       // can't load needles into SSE register if it could cross page boundary
       PAGE_FOR(needles.end() - 1) != PAGE_FOR(needles.data() + 15)) {
-    return detail::qfind_first_byte_of_memchr(haystack, needles);
+    return detail::qfind_first_byte_of_nosse(haystack, needles);
   }
 
   auto arr2 = __builtin_ia32_loaddqu(needles.data());
@@ -179,7 +147,7 @@ size_t qfind_first_byte_of_byteset(const StringPiece haystack,
 template <bool HAYSTACK_ALIGNED>
 size_t scanHaystackBlock(const StringPiece haystack,
                          const StringPiece needles,
-                         int64_t idx)
+                         uint64_t idx)
 // inline is okay because it's only called from other sse4.2 functions
   __attribute__ ((__target__("sse4.2")))
 // Turn off ASAN because the "arr2 = ..." assignment in the loop below reads
@@ -195,7 +163,7 @@ size_t scanHaystackBlock(const StringPiece haystack,
 template <bool HAYSTACK_ALIGNED>
 size_t scanHaystackBlock(const StringPiece haystack,
                          const StringPiece needles,
-                         int64_t blockStartIdx) {
+                         uint64_t blockStartIdx) {
   DCHECK_GT(needles.size(), 16);  // should handled by *needles16() method
   DCHECK(blockStartIdx + 16 <= haystack.size() ||
          (PAGE_FOR(haystack.data() + blockStartIdx) ==
@@ -278,16 +246,12 @@ size_t qfind_first_byte_of_nosse(const StringPiece haystack,
   // The thresholds below were empirically determined by benchmarking.
   // This is not an exact science since it depends on the CPU, the size of
   // needles, and the size of haystack.
-  if (haystack.size() == 1 ||
-      (haystack.size() < 4 && needles.size() <= 16)) {
-    return qfind_first_of(haystack, needles, asciiCaseSensitive);
-  } else if ((needles.size() >= 4 && haystack.size() <= 10) ||
+  if ((needles.size() >= 4 && haystack.size() <= 10) ||
              (needles.size() >= 16 && haystack.size() <= 64) ||
              needles.size() >= 32) {
     return qfind_first_byte_of_byteset(haystack, needles);
   }
-
-  return qfind_first_byte_of_memchr(haystack, needles);
+  return qfind_first_of(haystack, needles, asciiCaseSensitive);
 }
 
 }  // namespace detail