[Lldb-commits] [PATCH] D122848: Reduce extraneous temp strings in debugserver, free objects when they're not longer needed

Jonas Devlieghere via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Thu Mar 31 14:28:38 PDT 2022


JDevlieghere added a comment.

If debugserver linked against libSupport we could have saved the additional copy altogether by using `llvm::raw_string_ostream`:

  std::string str;
  llvm::raw_string_ostream stream(str);
  stream.str() // Flushes and returns a reference to the stack allocated str

Barring that, I wonder if if a little wrapper around `std::ostringstream` could improve readability and avoid bugs where someone forgets to call `stream.str("")`.

  class AggressiveStream {
  public:
    AggressiveStream() : m_stream(std::make_unique<std::ostringstream>()) {}
  
    std::ostringstream &operator*() {
      assert(m_stream && "cannot use stream after having called str()");
      return *m_stream;
    }
  
    std::string str() {
      std::string s = m_stream->str();
      m_stream.reset();
      return std::move(s);
    }
  
  private:
    std::unique_ptr<std::ostringstream> m_stream;
  };

WDYT?



================
Comment at: lldb/tools/debugserver/source/RNBRemote.cpp:588
+  stream.str(std::string());
+  stream.clear();
   return SendPacket(payload);
----------------
`clear` doesn't do what you think it does, it modifies the state flag which isn't relevant here.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D122848/new/

https://reviews.llvm.org/D122848



More information about the lldb-commits mailing list