[clang-tools-extra] 4b3633c - [clangd] Reuse buffer for JSONTransport::sendMessage

Nathan James via cfe-commits cfe-commits at lists.llvm.org
Tue Dec 22 03:31:09 PST 2020


Author: Nathan James
Date: 2020-12-22T11:30:56Z
New Revision: 4b3633cf2cb67220763494427f6db250bbd87494

URL: https://github.com/llvm/llvm-project/commit/4b3633cf2cb67220763494427f6db250bbd87494
DIFF: https://github.com/llvm/llvm-project/commit/4b3633cf2cb67220763494427f6db250bbd87494.diff

LOG: [clangd] Reuse buffer for JSONTransport::sendMessage

Allocate a Buffer in the JSONTransport to be used when sending messages to the client.
This gets reused each time a message is sent, reducing in fewer malloc, which is always a bonus.

Reviewed By: sammccall

Differential Revision: https://reviews.llvm.org/D93531

Added: 
    

Modified: 
    clang-tools-extra/clangd/JSONTransport.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clangd/JSONTransport.cpp b/clang-tools-extra/clangd/JSONTransport.cpp
index eb5a83882b2b..662e5df4e27b 100644
--- a/clang-tools-extra/clangd/JSONTransport.cpp
+++ b/clang-tools-extra/clangd/JSONTransport.cpp
@@ -126,13 +126,13 @@ class JSONTransport : public Transport {
   bool handleMessage(llvm::json::Value Message, MessageHandler &Handler);
   // Writes outgoing message to Out stream.
   void sendMessage(llvm::json::Value Message) {
-    std::string S;
-    llvm::raw_string_ostream OS(S);
+    OutputBuffer.clear();
+    llvm::raw_svector_ostream OS(OutputBuffer);
     OS << llvm::formatv(Pretty ? "{0:2}" : "{0}", Message);
-    OS.flush();
-    Out << "Content-Length: " << S.size() << "\r\n\r\n" << S;
+    Out << "Content-Length: " << OutputBuffer.size() << "\r\n\r\n"
+        << OutputBuffer;
     Out.flush();
-    vlog(">>> {0}\n", S);
+    vlog(">>> {0}\n", OutputBuffer);
   }
 
   // Read raw string messages from input stream.
@@ -143,6 +143,7 @@ class JSONTransport : public Transport {
   llvm::Optional<std::string> readDelimitedMessage();
   llvm::Optional<std::string> readStandardMessage();
 
+  llvm::SmallVector<char, 0> OutputBuffer;
   std::FILE *In;
   llvm::raw_ostream &Out;
   llvm::raw_ostream &InMirror;


        


More information about the cfe-commits mailing list