[Lldb-commits] [lldb] [lldb][NFCI] Remove unneccessary allocation in ScriptInterpreterPythonImpl::GetSyntheticTypeName (PR #66724)

Alex Langford via lldb-commits lldb-commits at lists.llvm.org
Mon Sep 18 17:34:48 PDT 2023


https://github.com/bulbazord created https://github.com/llvm/llvm-project/pull/66724

Instead of copying memory out of the PythonString (via a std::string) and then using that to create a ConstString, it would make more sense to just create the ConstString from the original StringRef in the first place.

>From 8cd3cba6baf85e18b57a1b2853f03b7fc7db43f7 Mon Sep 17 00:00:00 2001
From: Alex Langford <alangford at apple.com>
Date: Mon, 18 Sep 2023 17:32:28 -0700
Subject: [PATCH] [lldb][NFCI] Remove unneccessary allocation in
 ScriptInterpreterPythonImpl::GetSyntheticTypeName

Instead of copying memory out of the PythonString (via a std::string)
and then using that to create a ConstString, it would make more sense to
just create the ConstString from the original StringRef in the first
place.
---
 .../Python/ScriptInterpreterPython.cpp        | 21 ++++---------------
 1 file changed, 4 insertions(+), 17 deletions(-)

diff --git a/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp b/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
index 3fbab6bacec7dbb..6280084ca806828 100644
--- a/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
+++ b/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
@@ -2438,24 +2438,11 @@ ConstString ScriptInterpreterPythonImpl::GetSyntheticTypeName(
   }
 
   PythonObject py_return = std::move(expected_py_return.get());
+  if (!py_return.IsAllocated() || !PythonString::Check(py_return.get()))
+    return {};
 
-  ConstString ret_val;
-  bool got_string = false;
-  std::string buffer;
-
-  if (py_return.IsAllocated() && PythonString::Check(py_return.get())) {
-    PythonString py_string(PyRefType::Borrowed, py_return.get());
-    llvm::StringRef return_data(py_string.GetString());
-    if (!return_data.empty()) {
-      buffer.assign(return_data.data(), return_data.size());
-      got_string = true;
-    }
-  }
-
-  if (got_string)
-    ret_val.SetCStringWithLength(buffer.c_str(), buffer.size());
-
-  return ret_val;
+  PythonString type_name(PyRefType::Borrowed, py_return.get());
+  return ConstString(py_string.GetString());
 }
 
 bool ScriptInterpreterPythonImpl::RunScriptFormatKeyword(



More information about the lldb-commits mailing list