[llvm-commits] [llvm] r127010 - /llvm/trunk/lib/Support/raw_ostream.cpp

Benjamin Kramer benny.kra at googlemail.com
Fri Mar 4 10:18:16 PST 2011


Author: d0k
Date: Fri Mar  4 12:18:16 2011
New Revision: 127010

URL: http://llvm.org/viewvc/llvm-project?rev=127010&view=rev
Log:
raw_ostream: If writing a string that is larger than the buffer, write it directly instead of doing many buffer-sized writes.

This caps the number of write(2) calls per string to a maximum of 2.

Modified:
    llvm/trunk/lib/Support/raw_ostream.cpp

Modified: llvm/trunk/lib/Support/raw_ostream.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/raw_ostream.cpp?rev=127010&r1=127009&r2=127010&view=diff
==============================================================================
--- llvm/trunk/lib/Support/raw_ostream.cpp (original)
+++ llvm/trunk/lib/Support/raw_ostream.cpp Fri Mar  4 12:18:16 2011
@@ -265,15 +265,19 @@
       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);





More information about the llvm-commits mailing list