[llvm] 875b3b2 - [Support] Add reserve() method to the raw_ostream.

Alexey Lapshin via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 23 03:07:19 PST 2021


Author: Alexey Lapshin
Date: 2021-02-23T14:06:38+03:00
New Revision: 875b3b2cdda105c01af9a1330b1cb6a3f1e1b822

URL: https://github.com/llvm/llvm-project/commit/875b3b2cdda105c01af9a1330b1cb6a3f1e1b822
DIFF: https://github.com/llvm/llvm-project/commit/875b3b2cdda105c01af9a1330b1cb6a3f1e1b822.diff

LOG: [Support] Add reserve() method to the raw_ostream.

If resulting size of the output stream is already known,
then the space for stream data could be preliminary
allocated in some cases. f.e. raw_string_ostream could
preallocate the space for the target string(it allows
to avoid reallocations during writing into the stream).

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

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 7d572fe06f6f..201a4a857964 100644
--- a/llvm/include/llvm/Support/raw_ostream.h
+++ b/llvm/include/llvm/Support/raw_ostream.h
@@ -137,6 +137,13 @@ class raw_ostream {
   // Configuration Interface
   //===--------------------------------------------------------------------===//
 
+  /// If possible, pre-allocate \p ExtraSize bytes for stream data.
+  /// i.e. it extends internal buffers to keep additional ExtraSize bytes.
+  /// So that the stream could keep at least tell() + ExtraSize bytes
+  /// without re-allocations. reserveExtraSpace() does not change
+  /// the size/data of the stream.
+  virtual void reserveExtraSpace(uint64_t ExtraSize) {}
+
   /// Set the stream to be buffered, with an automatically determined buffer
   /// size.
   void SetBuffered();
@@ -626,6 +633,10 @@ class raw_string_ostream : public raw_ostream {
     flush();
     return OS;
   }
+
+  void reserveExtraSpace(uint64_t ExtraSize) override {
+    OS.reserve(tell() + ExtraSize);
+  }
 };
 
 /// A raw_ostream that writes to an SmallVector or SmallString.  This is a
@@ -659,6 +670,10 @@ class raw_svector_ostream : public raw_pwrite_stream {
 
   /// Return a StringRef for the vector contents.
   StringRef str() const { return StringRef(OS.data(), OS.size()); }
+
+  void reserveExtraSpace(uint64_t ExtraSize) override {
+    OS.reserve(tell() + ExtraSize);
+  }
 };
 
 /// A raw_ostream that discards all output.

diff  --git a/llvm/unittests/Support/raw_ostream_test.cpp b/llvm/unittests/Support/raw_ostream_test.cpp
index 6488bd7698f3..78fdb04bcdaa 100644
--- a/llvm/unittests/Support/raw_ostream_test.cpp
+++ b/llvm/unittests/Support/raw_ostream_test.cpp
@@ -455,4 +455,18 @@ TEST(raw_ostreamTest, flush_tied_to_stream_on_write) {
   TiedStream << "0";
   EXPECT_EQ("acegostuv", TiedToBuffer);
 }
+
+TEST(raw_ostreamTest, reserve_stream) {
+  std::string Str;
+  raw_string_ostream OS(Str);
+  OS << "11111111111111111111";
+  uint64_t CurrentPos = OS.tell();
+  OS.reserveExtraSpace(1000);
+  EXPECT_TRUE(Str.capacity() >= CurrentPos + 1000);
+  OS << "hello";
+  OS << 1;
+  OS << 'w' << 'o' << 'r' << 'l' << 'd';
+  OS.flush();
+  EXPECT_EQ("11111111111111111111hello1world", Str);
+}
 }


        


More information about the llvm-commits mailing list