Add fast path for raw_ostream output of strings.
authorDaniel Dunbar <daniel@zuster.org>
Fri, 3 Apr 2009 18:43:17 +0000 (18:43 +0000)
committerDaniel Dunbar <daniel@zuster.org>
Fri, 3 Apr 2009 18:43:17 +0000 (18:43 +0000)
 - Particularly nice for small constant strings, which get optimized
   down nicely. On a synthetic benchmark writing out "hello" in a
   loop, this is about 2x faster with gcc and 3x faster with
   llvm-gcc. llc on insn-attrtab.bc from 403.gcc is about .5% faster.

 - I tried for a fancier solution which wouldn't increase code size as
   much (by trying to match constant arrays), but can't quite make it
   fly.

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

include/llvm/Support/raw_ostream.h

index 3de31d667691cf5a047984aa5504200c4297560c..96adc465a7b77a1be0468f3b494d384f1501b7ba 100644 (file)
@@ -119,7 +119,17 @@ public:
   }
 
   raw_ostream &operator<<(const char *Str) {
-    write(Str, strlen(Str));
+    // Inline fast path, particulary for constant strings where a
+    // sufficiently smart compiler will simplify strlen.
+
+    unsigned Size = strlen(Str);
+
+    // Make sure we can use the fast path.
+    if (OutBufCur+Size > OutBufEnd)
+      return write(Str, Size);
+
+    memcpy(OutBufCur, Str, Size);
+    OutBufCur += Size;
     return *this;
   }