From: Alp Toker Date: Mon, 2 Dec 2013 17:02:49 +0000 (+0000) Subject: Rewriter: Output RewriteRope contents efficiently X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=bf32c5e4f89f1cccb45c1af6edbc27eb352f54c9;p=clang 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. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@196119 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Rewrite/Core/RewriteRope.h b/include/clang/Rewrite/Core/RewriteRope.h index a5192ef1ea..5167c50752 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 #include @@ -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 afb1080c66..51af83954c 100644 --- a/lib/Rewrite/Core/Rewriter.cpp +++ b/lib/Rewrite/Core/Rewriter.cpp @@ -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; }