[PATCH] Make RewriteBuffer write in chunks

Alp Toker alp at nuanti.com
Fri Nov 8 01:41:06 PST 2013


Updated patch to use StringRef instead of RopePieceBTreeIterator internals.

Check it out

Alp.


On 07/11/2013 07:08, Alp Toker wrote:
> Rewrite was previously allocating potentially huge std::strings with
> each call and filling it one character at a time.
>
> This has become a hot path for Refactoring, Modernize, FixIt, Format,
> and particularly ARCMT/ObjCMT which call it multiple times on the full
> source tree -- so copying out contiguous chunks while avoiding large
> allocations is a good idea.
>
> This commit preserves the existing interface of RewriteRope and pokes
> into it directly for access to the byte vectors.
>
> Resolves a FIXME back from r101521. No change in behaviour.
>

-- 
http://www.nuanti.com
the browser experts

-------------- next part --------------
diff --git a/include/clang/Rewrite/Core/RewriteRope.h b/include/clang/Rewrite/Core/RewriteRope.h
index a5192ef..5167c50 100644
--- a/include/clang/Rewrite/Core/RewriteRope.h
+++ b/include/clang/Rewrite/Core/RewriteRope.h
@@ -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();
   };
 
diff --git a/lib/Rewrite/Core/Rewriter.cpp b/lib/Rewrite/Core/Rewriter.cpp
index afb1080..28b63a5 100644
--- a/lib/Rewrite/Core/Rewriter.cpp
+++ b/lib/Rewrite/Core/Rewriter.cpp
@@ -26,8 +26,9 @@
 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());
+  for (RopePieceBTreeIterator I = begin(), E = end(); I != E;
+       I.MoveToNextPiece())
+    os << I.piece();
   return os;
 }
 


More information about the cfe-commits mailing list