[llvm-commits] [llvm] r77461 - in /llvm/trunk: include/llvm/Support/FormattedStream.h lib/Support/FormattedStream.cpp

David Greene greened at obbligato.org
Wed Jul 29 09:08:35 PDT 2009


Author: greened
Date: Wed Jul 29 11:08:27 2009
New Revision: 77461

URL: http://llvm.org/viewvc/llvm-project?rev=77461&view=rev
Log:

Re-apply previous changes and improve column padding performance some more.

Modified:
    llvm/trunk/include/llvm/Support/FormattedStream.h
    llvm/trunk/lib/Support/FormattedStream.cpp

Modified: llvm/trunk/include/llvm/Support/FormattedStream.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/FormattedStream.h?rev=77461&r1=77460&r2=77461&view=diff

==============================================================================
--- llvm/trunk/include/llvm/Support/FormattedStream.h (original)
+++ llvm/trunk/include/llvm/Support/FormattedStream.h Wed Jul 29 11:08:27 2009
@@ -49,14 +49,21 @@
     ///
     bool DeleteStream;
 
-    /// Column - The current output column of the stream.  The column
-    /// scheme is zero-based.
+    /// ColumnScanned - The current output column of the data that's
+    /// been flushed and the portion of the buffer that's been
+    /// scanned.  The column scheme is zero-based.
     ///
-    unsigned Column;
+    unsigned ColumnScanned;
+
+    /// Scanned - This points to one past the last character in the
+    /// buffer we've scanned.
+    ///
+    iterator Scanned;
 
     virtual void write_impl(const char *Ptr, size_t Size) {
-      ComputeColumn(Ptr, Size);
+      ComputeColumn();
       TheStream->write(Ptr, Size);
+      Scanned = begin();
     }
 
     /// current_pos - Return the current position within the stream,
@@ -70,7 +77,7 @@
     /// ComputeColumn - Examine the current output and figure out
     /// which column we end up in after output.
     ///
-    void ComputeColumn(const char *Ptr, size_t Size);
+    void ComputeColumn();
 
   public:
     /// formatted_raw_ostream - Open the specified file for
@@ -84,13 +91,16 @@
     /// underneath it.
     ///
     formatted_raw_ostream(raw_ostream &Stream, bool Delete = false) 
-      : raw_ostream(), TheStream(0), DeleteStream(false), Column(0) {
+      : raw_ostream(), TheStream(0), DeleteStream(false), ColumnScanned(0) {
       setStream(Stream, Delete);
     }
     explicit formatted_raw_ostream()
-      : raw_ostream(), TheStream(0), DeleteStream(false), Column(0) {}
+      : raw_ostream(), TheStream(0), DeleteStream(false), ColumnScanned(0) {
+      Scanned = begin();
+    }
 
     ~formatted_raw_ostream() {
+      flush();
       if (DeleteStream)
         delete TheStream;
     }
@@ -110,6 +120,8 @@
       if (size_t BufferSize = TheStream->GetNumBytesInBuffer())
         SetBufferSize(BufferSize);
       TheStream->SetUnbuffered();
+
+      Scanned = begin();
     }
 
     /// PadToColumn - Align the output to some column number.

Modified: llvm/trunk/lib/Support/FormattedStream.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/FormattedStream.cpp?rev=77461&r1=77460&r2=77461&view=diff

==============================================================================
--- llvm/trunk/lib/Support/FormattedStream.cpp (original)
+++ llvm/trunk/lib/Support/FormattedStream.cpp Wed Jul 29 11:08:27 2009
@@ -19,16 +19,23 @@
 /// ComputeColumn - Examine the current output and figure out which
 /// column we end up in after output.
 ///
-void formatted_raw_ostream::ComputeColumn(const char *Ptr, size_t Size) {
+void formatted_raw_ostream::ComputeColumn() {
   // Keep track of the current column by scanning the string for
   // special characters
 
-  for (const char *epos = Ptr + Size; Ptr != epos; ++Ptr) {
-    ++Column;
-    if (*Ptr == '\n' || *Ptr == '\r')
-      Column = 0;
-    else if (*Ptr == '\t')
-      Column += (8 - (Column & 0x7)) & 0x7;
+  // The buffer may have been allocated underneath us.
+  if (Scanned == 0 && GetNumBytesInBuffer() != 0) {
+    Scanned = begin();
+  }
+
+  while (Scanned != end()) {
+    ++ColumnScanned;
+    if (*Scanned == '\n' || *Scanned == '\r')
+      ColumnScanned = 0;
+    else if (*Scanned == '\t')
+      // Assumes tab stop = 8 characters.
+      ColumnScanned += (8 - (ColumnScanned & 0x7)) & 0x7;
+    ++Scanned;
   }
 }
 
@@ -38,21 +45,18 @@
 /// \param MinPad - The minimum space to give after the most recent
 /// I/O, even if the current column + minpad > newcol.
 ///
-void formatted_raw_ostream::PadToColumn(unsigned NewCol, unsigned MinPad) {
-  flush();
+void formatted_raw_ostream::PadToColumn(unsigned NewCol, unsigned MinPad) { 
+  // Figure out what's in the buffer and add it to the column count.
+  ComputeColumn();
 
   // Output spaces until we reach the desired column.
-  unsigned num = NewCol - Column;
-  if (NewCol < Column || num < MinPad)
+  unsigned num = NewCol - ColumnScanned;
+  if (NewCol < ColumnScanned || num < MinPad)
     num = MinPad;
 
   // Keep a buffer of spaces handy to speed up processing.
-  static char Spaces[MAX_COLUMN_PAD];
-  static bool Initialized = false;
-  if (!Initialized) {
-    std::fill_n(Spaces, MAX_COLUMN_PAD, ' '),
-    Initialized = true;
-  }
+  const char *Spaces = "                                                      "
+    "                                                                         ";
 
   assert(num < MAX_COLUMN_PAD && "Unexpectedly large column padding");
 





More information about the llvm-commits mailing list