[llvm] 65b1361 - Default raw_string_ostream to be unbuffered

Benjamin Kramer via llvm-commits llvm-commits at lists.llvm.org
Sun Apr 26 03:59:04 PDT 2020


Author: Benjamin Kramer
Date: 2020-04-26T12:45:57+02:00
New Revision: 65b13610a5226b84889b923bae884ba395ad084d

URL: https://github.com/llvm/llvm-project/commit/65b13610a5226b84889b923bae884ba395ad084d
DIFF: https://github.com/llvm/llvm-project/commit/65b13610a5226b84889b923bae884ba395ad084d.diff

LOG: Default raw_string_ostream to be unbuffered

raw_string_ostream can just use the std::string as a buffer. The buffer
requirement came from the days when the minimum buffer size was 128
(fixed in 2015) and std::string was non-SSO. Now we can just use the
inline capacity for small things and on a good growth strategy later.

This assumes that the standard library isn't doing something bad like
only growing to the exact size. I checked some common implementations
and they grow by 2x (libc++) or 1.5x (msvc) which is reasonable. We
should still check if this incurs any performance regressions though.

Added: 
    

Modified: 
    llvm/include/llvm/Support/raw_ostream.h
    llvm/unittests/Support/raw_ostream_test.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Support/raw_ostream.h b/llvm/include/llvm/Support/raw_ostream.h
index f7223dc8c8cf..72cedc2a6065 100644
--- a/llvm/include/llvm/Support/raw_ostream.h
+++ b/llvm/include/llvm/Support/raw_ostream.h
@@ -524,7 +524,9 @@ class raw_string_ostream : public raw_ostream {
   uint64_t current_pos() const override { return OS.size(); }
 
 public:
-  explicit raw_string_ostream(std::string &O) : OS(O) {}
+  explicit raw_string_ostream(std::string &O) : OS(O) {
+    SetUnbuffered();
+  }
   ~raw_string_ostream() override;
 
   /// Flushes the stream contents to the target string and returns  the string's

diff  --git a/llvm/unittests/Support/raw_ostream_test.cpp b/llvm/unittests/Support/raw_ostream_test.cpp
index 49685ef5c019..3df01be39937 100644
--- a/llvm/unittests/Support/raw_ostream_test.cpp
+++ b/llvm/unittests/Support/raw_ostream_test.cpp
@@ -18,7 +18,10 @@ namespace {
 
 template<typename T> std::string printToString(const T &Value) {
   std::string res;
-  return (llvm::raw_string_ostream(res) << Value).str();
+  llvm::raw_string_ostream OS(res);
+  OS.SetBuffered();
+  OS << Value;
+  return res;
 }
 
 /// printToString - Print the given value to a stream which only has \arg


        


More information about the llvm-commits mailing list