[Lldb-commits] [lldb] 77e3914 - [lldb][NFCI] Stop creating additional temporary string in Log::VAPrintf

Alex Langford via lldb-commits lldb-commits at lists.llvm.org
Wed Apr 19 14:18:20 PDT 2023


Author: Alex Langford
Date: 2023-04-19T14:16:12-07:00
New Revision: 77e3914be7c99a76a2f728adbb2a169d7cfc5a10

URL: https://github.com/llvm/llvm-project/commit/77e3914be7c99a76a2f728adbb2a169d7cfc5a10
DIFF: https://github.com/llvm/llvm-project/commit/77e3914be7c99a76a2f728adbb2a169d7cfc5a10.diff

LOG: [lldb][NFCI] Stop creating additional temporary string in Log::VAPrintf

Instead of creating a std::string from the `SmallString`,
let's just use a std::string from the start. I initially tried to make
`SmallString` work but getting it right proved complicated because
`LogHandler::Emit` will take its `StringRef` parameter and touch the raw
`const char *` from it directly, which isn't guaranteed to be
null-terminated with a `SmallString`.

I changed `WriteMessage` to take a `StringRef` instead of a
`const std::string &` for flexibility.

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

Added: 
    

Modified: 
    lldb/include/lldb/Utility/Log.h
    lldb/source/Utility/Log.cpp

Removed: 
    


################################################################################
diff  --git a/lldb/include/lldb/Utility/Log.h b/lldb/include/lldb/Utility/Log.h
index 5d8530483594e..2984a4bd43bb7 100644
--- a/lldb/include/lldb/Utility/Log.h
+++ b/lldb/include/lldb/Utility/Log.h
@@ -265,7 +265,7 @@ class Log final {
 
   void WriteHeader(llvm::raw_ostream &OS, llvm::StringRef file,
                    llvm::StringRef function);
-  void WriteMessage(const std::string &message);
+  void WriteMessage(llvm::StringRef message);
 
   void Format(llvm::StringRef file, llvm::StringRef function,
               const llvm::formatv_object_base &payload);

diff  --git a/lldb/source/Utility/Log.cpp b/lldb/source/Utility/Log.cpp
index 045e0f2cb68a2..7d94a89a8c974 100644
--- a/lldb/source/Utility/Log.cpp
+++ b/lldb/source/Utility/Log.cpp
@@ -146,8 +146,8 @@ void Log::Printf(const char *format, ...) {
 // callback registered, then we call the logging callback. If we have a valid
 // file handle, we also log to the file.
 void Log::VAPrintf(const char *format, va_list args) {
-  llvm::SmallString<64> FinalMessage;
-  llvm::raw_svector_ostream Stream(FinalMessage);
+  std::string FinalMessage;
+  llvm::raw_string_ostream Stream(FinalMessage);
   WriteHeader(Stream, "", "");
 
   llvm::SmallString<64> Content;
@@ -155,7 +155,7 @@ void Log::VAPrintf(const char *format, va_list args) {
 
   Stream << Content << "\n";
 
-  WriteMessage(std::string(FinalMessage.str()));
+  WriteMessage(FinalMessage);
 }
 
 // Printing of errors that are not fatal.
@@ -344,7 +344,7 @@ void Log::WriteHeader(llvm::raw_ostream &OS, llvm::StringRef file,
   }
 }
 
-void Log::WriteMessage(const std::string &message) {
+void Log::WriteMessage(llvm::StringRef message) {
   // Make a copy of our stream shared pointer in case someone disables our log
   // while we are logging and releases the stream
   auto handler_sp = GetHandler();


        


More information about the lldb-commits mailing list