Skip to content

Conversation

@kastiglione
Copy link
Contributor

@kastiglione kastiglione commented Apr 22, 2025

The primary changes are:

  1. Avoid allocating a temporary std::string each time in the loop
  2. Use starts_with instead of find(...) == 0

@llvmbot
Copy link
Member

llvmbot commented Apr 22, 2025

@llvm/pr-subscribers-lldb

Author: Dave Lee (kastiglione)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/136760.diff

1 Files Affected:

  • (modified) lldb/include/lldb/Interpreter/CommandObject.h (+4-5)
diff --git a/lldb/include/lldb/Interpreter/CommandObject.h b/lldb/include/lldb/Interpreter/CommandObject.h
index e6fea9e022c43..a9ec43ea3a505 100644
--- a/lldb/include/lldb/Interpreter/CommandObject.h
+++ b/lldb/include/lldb/Interpreter/CommandObject.h
@@ -40,12 +40,11 @@ int AddNamesMatchingPartialString(
     StringList *descriptions = nullptr) {
   int number_added = 0;
 
-  const bool add_all = cmd_str.empty();
-
-  for (auto iter = in_map.begin(), end = in_map.end(); iter != end; iter++) {
-    if (add_all || (iter->first.find(std::string(cmd_str), 0) == 0)) {
+  for (const auto &iter : in_map) {
+    llvm::StringRef full_cmd = iter->first;
+    if (full_cmd.starts_with(cmd_str)) {
       ++number_added;
-      matches.AppendString(iter->first.c_str());
+      matches.AppendString(iter->first);
       if (descriptions)
         descriptions->AppendString(iter->second->GetHelp());
     }

Copy link
Member

@JDevlieghere JDevlieghere left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice

@kastiglione kastiglione merged commit dd17cf4 into llvm:main Apr 23, 2025
10 checks passed
@kastiglione kastiglione deleted the lldb-Minor-improvements-to-AddNamesMatchingPartialString-NFC branch April 23, 2025 22:21
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
…m#136760)

The primary changes are:

1. Avoid allocating a temporary `std::string` each time in the loop
2. Use `starts_with` instead of `find(...) == 0`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants