raw_ostream: If writing a string that is larger than the buffer, write it directly...
authorBenjamin Kramer <benny.kra@googlemail.com>
Fri, 4 Mar 2011 18:18:16 +0000 (18:18 +0000)
committerBenjamin Kramer <benny.kra@googlemail.com>
Fri, 4 Mar 2011 18:18:16 +0000 (18:18 +0000)
This caps the number of write(2) calls per string to a maximum of 2.

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

lib/Support/raw_ostream.cpp

index 80ea7407b44e577800e13decf62df6da63a46892..b8839e2943b7d541294ad3b947ad5a0a0c943c3a 100644 (file)
@@ -265,15 +265,19 @@ raw_ostream &raw_ostream::write(const char *Ptr, size_t Size) {
       return write(Ptr, Size);
     }
 
-    // Write out the data in buffer-sized blocks until the remainder
-    // fits within the buffer.
-    do {
-      size_t NumBytes = OutBufEnd - OutBufCur;
-      copy_to_buffer(Ptr, NumBytes);
-      flush_nonempty();
-      Ptr += NumBytes;
-      Size -= NumBytes;
-    } while (OutBufCur+Size > OutBufEnd);
+    // If the buffer is empty at this point we have a string that is larger
+    // than the buffer. It's better to write it unbuffered in this case.
+    if (BUILTIN_EXPECT(OutBufCur == OutBufStart, false)) {
+      write_impl(Ptr, Size);
+      return *this;
+    }
+
+    // We don't have enough space in the buffer to fit the string in. Insert as
+    // much as possible, flush and start over with the remainder.
+    size_t NumBytes = OutBufEnd - OutBufCur;
+    copy_to_buffer(Ptr, NumBytes);
+    flush_nonempty();
+    return write(Ptr + NumBytes, Size - NumBytes);
   }
 
   copy_to_buffer(Ptr, Size);