[clang-tools-extra] r365289 - [clangd] Avoid slow ostreams in URI conversion.

Sam McCall via cfe-commits cfe-commits at lists.llvm.org
Sun Jul 7 19:46:21 PDT 2019


Author: sammccall
Date: Sun Jul  7 19:46:21 2019
New Revision: 365289

URL: http://llvm.org/viewvc/llvm-project?rev=365289&view=rev
Log:
[clangd] Avoid slow ostreams in URI conversion.

This speeds up some hot paths significantly (e.g.  dex::generateProximityURIs
by a third or so)

Modified:
    clang-tools-extra/trunk/clangd/URI.cpp

Modified: clang-tools-extra/trunk/clangd/URI.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/URI.cpp?rev=365289&r1=365288&r2=365289&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/URI.cpp (original)
+++ clang-tools-extra/trunk/clangd/URI.cpp Sun Jul  7 19:46:21 2019
@@ -14,8 +14,6 @@
 #include "llvm/Support/FormatVariadic.h"
 #include "llvm/Support/Path.h"
 #include <algorithm>
-#include <iomanip>
-#include <sstream>
 
 LLVM_INSTANTIATE_REGISTRY(clang::clangd::URISchemeRegistry)
 
@@ -96,17 +94,16 @@ bool shouldEscape(unsigned char C) {
 /// - Unreserved characters are not escaped.
 /// - Reserved characters always escaped with exceptions like '/'.
 /// - All other characters are escaped.
-std::string percentEncode(llvm::StringRef Content) {
+void percentEncode(llvm::StringRef Content, std::string &Out) {
   std::string Result;
-  llvm::raw_string_ostream OS(Result);
   for (unsigned char C : Content)
     if (shouldEscape(C))
-      OS << '%' << llvm::format_hex_no_prefix(C, 2, /*Upper = */ true);
-    else
-      OS << C;
-
-  OS.flush();
-  return Result;
+    {
+      Out.push_back('%');
+      Out.push_back(llvm::hexdigit(C / 16));
+      Out.push_back(llvm::hexdigit(C % 16));
+    } else
+    { Out.push_back(C); }
 }
 
 /// Decodes a string according to percent-encoding.
@@ -149,16 +146,18 @@ URI::URI(llvm::StringRef Scheme, llvm::S
 
 std::string URI::toString() const {
   std::string Result;
-  llvm::raw_string_ostream OS(Result);
-  OS << percentEncode(Scheme) << ":";
+  percentEncode(Scheme, Result);
+  Result.push_back(':');
   if (Authority.empty() && Body.empty())
-    return OS.str();
+    return Result;
   // If authority if empty, we only print body if it starts with "/"; otherwise,
   // the URI is invalid.
   if (!Authority.empty() || llvm::StringRef(Body).startswith("/"))
-    OS << "//" << percentEncode(Authority);
-  OS << percentEncode(Body);
-  OS.flush();
+  {
+    Result.append("//");
+    percentEncode(Authority, Result);
+  }
+  percentEncode(Body, Result);
   return Result;
 }
 




More information about the cfe-commits mailing list