[Lldb-commits] [PATCH] D155035: [lldb][NFCI] Remove unneeded temporary std::string allocations in SBAPI

Alex Langford via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Tue Jul 11 18:14:37 PDT 2023


bulbazord created this revision.
bulbazord added reviewers: JDevlieghere, mib, jingham, clayborg.
Herald added a project: All.
bulbazord requested review of this revision.
Herald added a project: LLDB.
Herald added a subscriber: lldb-commits.

This uses some friend class trickery to avoid some unneeded temporary
std::string allocations.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D155035

Files:
  lldb/include/lldb/API/SBStringList.h
  lldb/source/API/SBDebugger.cpp
  lldb/source/API/SBStringList.cpp
  lldb/source/API/SBStructuredData.cpp
  lldb/source/API/SBThread.cpp


Index: lldb/source/API/SBThread.cpp
===================================================================
--- lldb/source/API/SBThread.cpp
+++ lldb/source/API/SBThread.cpp
@@ -457,7 +457,7 @@
             info_root_sp->GetObjectForDotSeparatedPath(path);
         if (node) {
           if (node->GetType() == eStructuredDataTypeString) {
-            strm.Printf("%s", node->GetAsString()->GetValue().str().c_str());
+            strm.ref() << node->GetAsString()->GetValue();
             success = true;
           }
           if (node->GetType() == eStructuredDataTypeInteger) {
Index: lldb/source/API/SBStructuredData.cpp
===================================================================
--- lldb/source/API/SBStructuredData.cpp
+++ lldb/source/API/SBStructuredData.cpp
@@ -16,6 +16,7 @@
 #include "lldb/Utility/Event.h"
 #include "lldb/Utility/Status.h"
 #include "lldb/Utility/Stream.h"
+#include "lldb/Utility/StringList.h"
 #include "lldb/Utility/StructuredData.h"
 
 using namespace lldb;
@@ -138,9 +139,9 @@
   StructuredData::Array *key_arr = array_sp->GetAsArray();
   assert(key_arr);
 
-  key_arr->ForEach([&keys] (StructuredData::Object *object) -> bool {
+  key_arr->ForEach([&keys](StructuredData::Object *object) -> bool {
     llvm::StringRef key = object->GetStringValue("");
-    keys.AppendString(key.str().c_str());
+    keys->AppendString(key);
     return true;
   });
   return true;
Index: lldb/source/API/SBStringList.cpp
===================================================================
--- lldb/source/API/SBStringList.cpp
+++ lldb/source/API/SBStringList.cpp
@@ -37,6 +37,13 @@
 
 SBStringList::~SBStringList() = default;
 
+lldb_private::StringList *SBStringList::operator->() {
+  if (!IsValid())
+    m_opaque_up = std::make_unique<lldb_private::StringList>();
+
+  return m_opaque_up.get();
+}
+
 const lldb_private::StringList *SBStringList::operator->() const {
   return m_opaque_up.get();
 }
Index: lldb/source/API/SBDebugger.cpp
===================================================================
--- lldb/source/API/SBDebugger.cpp
+++ lldb/source/API/SBDebugger.cpp
@@ -1399,9 +1399,9 @@
 
   Log *log = GetLog(LLDBLog::API);
 
-  LLDB_LOGF(log, "SBDebugger(%p)::GetPrompt () => \"%s\"",
-            static_cast<void *>(m_opaque_sp.get()),
-            (m_opaque_sp ? m_opaque_sp->GetPrompt().str().c_str() : ""));
+  LLDB_LOG(log, "SBDebugger({0:x})::GetPrompt () => \"{1}\"",
+           static_cast<void *>(m_opaque_sp.get()),
+           (m_opaque_sp ? m_opaque_sp->GetPrompt() : ""));
 
   return (m_opaque_sp ? ConstString(m_opaque_sp->GetPrompt()).GetCString()
                       : nullptr);
Index: lldb/include/lldb/API/SBStringList.h
===================================================================
--- lldb/include/lldb/API/SBStringList.h
+++ lldb/include/lldb/API/SBStringList.h
@@ -47,11 +47,14 @@
   friend class SBBreakpoint;
   friend class SBBreakpointLocation;
   friend class SBBreakpointName;
+  friend class SBStructuredData;
 
   SBStringList(const lldb_private::StringList *lldb_strings);
 
   void AppendList(const lldb_private::StringList &strings);
 
+  lldb_private::StringList *operator->();
+
   const lldb_private::StringList *operator->() const;
 
   const lldb_private::StringList &operator*() const;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D155035.539354.patch
Type: text/x-patch
Size: 3285 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20230712/142ce511/attachment.bin>


More information about the lldb-commits mailing list