Skip to content

Commit

Permalink
Default raw_string_ostream to be unbuffered
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
d0k committed Apr 26, 2020
1 parent 9caac56 commit 65b1361
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
4 changes: 3 additions & 1 deletion llvm/include/llvm/Support/raw_ostream.h
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion llvm/unittests/Support/raw_ostream_test.cpp
Expand Up @@ -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
Expand Down

0 comments on commit 65b1361

Please sign in to comment.