]> granicus.if.org Git - clang/commitdiff
Rewriter: Output RewriteRope contents efficiently
authorAlp Toker <alp@nuanti.com>
Mon, 2 Dec 2013 17:02:49 +0000 (17:02 +0000)
committerAlp Toker <alp@nuanti.com>
Mon, 2 Dec 2013 17:02:49 +0000 (17:02 +0000)
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.

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

include/clang/Rewrite/Core/RewriteRope.h
lib/Rewrite/Core/Rewriter.cpp

index a5192ef1ea1cd92a7c2df1244e07d520719693fb..5167c507525c12673d5fb9feab17fe7e47aaec7f 100644 (file)
@@ -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();
   };
 
index afb1080c66a1985be777ce0a21fccc25557b60d2..51af83954c5a821ebc4f99416f33e2573b183e4f 100644 (file)
 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;
 }