Skip to content

Commit 65b1361

Browse files
committed
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.
1 parent 9caac56 commit 65b1361

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

llvm/include/llvm/Support/raw_ostream.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,9 @@ class raw_string_ostream : public raw_ostream {
524524
uint64_t current_pos() const override { return OS.size(); }
525525

526526
public:
527-
explicit raw_string_ostream(std::string &O) : OS(O) {}
527+
explicit raw_string_ostream(std::string &O) : OS(O) {
528+
SetUnbuffered();
529+
}
528530
~raw_string_ostream() override;
529531

530532
/// Flushes the stream contents to the target string and returns the string's

llvm/unittests/Support/raw_ostream_test.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ namespace {
1818

1919
template<typename T> std::string printToString(const T &Value) {
2020
std::string res;
21-
return (llvm::raw_string_ostream(res) << Value).str();
21+
llvm::raw_string_ostream OS(res);
22+
OS.SetBuffered();
23+
OS << Value;
24+
return res;
2225
}
2326

2427
/// printToString - Print the given value to a stream which only has \arg

0 commit comments

Comments
 (0)