StringRef: Just forward StringRef::find to libc's memchr.
authorBenjamin Kramer <benny.kra@googlemail.com>
Sat, 21 Mar 2015 16:42:35 +0000 (16:42 +0000)
committerBenjamin Kramer <benny.kra@googlemail.com>
Sat, 21 Mar 2015 16:42:35 +0000 (16:42 +0000)
Modern libc's have an SSE version of memchr which is a lot faster than our
hand-rolled version. In the past I was reluctant to use it because Darwin's
memchr used a naive ridiculously slow implementation, but that has been fixed
some versions ago.

Should have zero functional impact.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@232898 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/ADT/StringRef.h

index 6111c42da9dced36ed905fb0e715865b0f4cdd91..95660a49f1f1b2d9fd8ea752423ff2a676710e21 100644 (file)
@@ -238,9 +238,12 @@ namespace llvm {
     /// \returns The index of the first occurrence of \p C, or npos if not
     /// found.
     size_t find(char C, size_t From = 0) const {
-      for (size_t i = std::min(From, Length), e = Length; i != e; ++i)
-        if (Data[i] == C)
-          return i;
+      size_t FindBegin = std::min(From, Length);
+      if (FindBegin < Length) { // Avoid calling memchr with nullptr.
+        // Just forward to memchr, which is faster than a hand-rolled loop.
+        if (const void *P = ::memchr(Data + FindBegin, C, Length - FindBegin))
+          return static_cast<const char *>(P) - Data;
+      }
       return npos;
     }