r196119 - Rewriter: Output RewriteRope contents efficiently

Alp Toker alp at nuanti.com
Mon Dec 2 09:02:50 PST 2013


Author: alp
Date: Mon Dec  2 11:02:49 2013
New Revision: 196119

URL: http://llvm.org/viewvc/llvm-project?rev=196119&view=rev
Log:
Rewriter: Output RewriteRope contents efficiently

This avoids allocation of temporary std::strings for file contents, instead
writing chunks directly to the output stream.

The old character-based B-tree iterator remains intact for the time being.

Modified:
    cfe/trunk/include/clang/Rewrite/Core/RewriteRope.h
    cfe/trunk/lib/Rewrite/Core/Rewriter.cpp

Modified: cfe/trunk/include/clang/Rewrite/Core/RewriteRope.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Rewrite/Core/RewriteRope.h?rev=196119&r1=196118&r2=196119&view=diff
==============================================================================
--- cfe/trunk/include/clang/Rewrite/Core/RewriteRope.h (original)
+++ cfe/trunk/include/clang/Rewrite/Core/RewriteRope.h Mon Dec  2 11:02:49 2013
@@ -14,6 +14,7 @@
 #ifndef LLVM_CLANG_REWRITEROPE_H
 #define LLVM_CLANG_REWRITEROPE_H
 
+#include "llvm/ADT/StringRef.h"
 #include "llvm/Support/Compiler.h"
 #include <cassert>
 #include <cstddef>
@@ -144,7 +145,11 @@ namespace clang {
     inline RopePieceBTreeIterator operator++(int) { // Postincrement
       RopePieceBTreeIterator tmp = *this; ++*this; return tmp;
     }
-  private:
+
+    llvm::StringRef piece() const {
+      return llvm::StringRef(&(*CurPiece)[0], CurPiece->size());
+    }
+
     void MoveToNextPiece();
   };
 

Modified: cfe/trunk/lib/Rewrite/Core/Rewriter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Rewrite/Core/Rewriter.cpp?rev=196119&r1=196118&r2=196119&view=diff
==============================================================================
--- cfe/trunk/lib/Rewrite/Core/Rewriter.cpp (original)
+++ cfe/trunk/lib/Rewrite/Core/Rewriter.cpp Mon Dec  2 11:02:49 2013
@@ -26,8 +26,11 @@
 using namespace clang;
 
 raw_ostream &RewriteBuffer::write(raw_ostream &os) const {
-  // FIXME: eliminate the copy by writing out each chunk at a time
-  os << std::string(begin(), end());
+  // Walk RewriteRope chunks efficiently using MoveToNextPiece() instead of the
+  // character iterator.
+  for (RopePieceBTreeIterator I = begin(), E = end(); I != E;
+       I.MoveToNextPiece())
+    os << I.piece();
   return os;
 }
 





More information about the cfe-commits mailing list