[llvm] r354819 - [Support] Make raw_string_ostream unbuffered
Roman Lebedev via llvm-commits
llvm-commits at lists.llvm.org
Mon Feb 25 12:51:49 PST 2019
Author: lebedevri
Date: Mon Feb 25 12:51:49 2019
New Revision: 354819
URL: http://llvm.org/viewvc/llvm-project?rev=354819&view=rev
Log:
[Support] Make raw_string_ostream unbuffered
Summary:
In D58580 i have noted that `llvm::to_string()` is a memory hog.
It uses `raw_string_ostream`, and since it was buffered,
every `raw_string_ostream` had a cost of `BUFSIZ` bytes
(which is `8192` at least here). So every `llvm::to_string()`
call, even to just print an `int`, costed `8192` bytes.
In D58580, getting rid of that buffering //had// significant
performance and memory consumption improvements for `llvm-xray convert`.
Similarly, in D58580 @rnk pointed out that the `raw_svector_ostream`
is already unbuffered, and `write_unsigned_impl` and friends
do internal buffering. So it should be ok performance-wise to just
make the `raw_string_ostream` itself unbuffered.
Here, i don't have any perf measurements.
Another letdown is that i'm leaving a loose-end - not deleting the
`flush()` method. I don't expect that cleanup to be anything more
than just fixing every new compiler error, but i'm presently unable
to do that. Will look into that later.
Reviewers: rnk, zturner
Reviewed By: rnk
Subscribers: kristina, jdoerfert, llvm-commits, rnk
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D58643
Modified:
llvm/trunk/include/llvm/Support/raw_ostream.h
Modified: llvm/trunk/include/llvm/Support/raw_ostream.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/raw_ostream.h?rev=354819&r1=354818&r2=354819&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/raw_ostream.h (original)
+++ llvm/trunk/include/llvm/Support/raw_ostream.h Mon Feb 25 12:51:49 2019
@@ -479,6 +479,9 @@ raw_ostream &nulls();
/// A raw_ostream that writes to an std::string. This is a simple adaptor
/// class. This class does not encounter output errors.
+/// raw_string_ostream operates without a buffer, delegating all memory
+/// management to the std::string. Thus the std::string is always up-to-date,
+/// may be used directly and there is no need to call flush().
class raw_string_ostream : public raw_ostream {
std::string &OS;
@@ -490,13 +493,16 @@ class raw_string_ostream : public raw_os
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)
+ : raw_ostream(/*Unbuffered=*/true), OS(O) {}
+
~raw_string_ostream() override;
- /// Flushes the stream contents to the target string and returns the string's
- /// reference.
+ // FIXME: uncomment and deal with the fallout.
+ // void flush() = delete;
+
+ /// Returns the string's reference.
std::string& str() {
- flush();
return OS;
}
};
More information about the llvm-commits
mailing list