Author: Dave Lee
Date: 2025-03-07T21:57:05-08:00
New Revision: 61efe360f9ba70736ca27ad289277c5c8268ffc0

URL: 
https://github.com/llvm/llvm-project/commit/61efe360f9ba70736ca27ad289277c5c8268ffc0
DIFF: 
https://github.com/llvm/llvm-project/commit/61efe360f9ba70736ca27ad289277c5c8268ffc0.diff

LOG: [lldb] Avoid unnecessary regex check in dwim-print (#114608)

An (unmeasured) improvement to performance of `dwim-print` when used as `po`.

This change lifts the check for `note_shown` to the top of the lambda, to avoid 
all subsequent work when the hint has already been shown. The main effect is to 
avoid performing a regex match when the hint is not going to be shown.

This change also constructs the `std::regex` only once, by making it static.

Added: 
    

Modified: 
    lldb/source/Commands/CommandObjectDWIMPrint.cpp

Removed: 
    


################################################################################
diff  --git a/lldb/source/Commands/CommandObjectDWIMPrint.cpp 
b/lldb/source/Commands/CommandObjectDWIMPrint.cpp
index 04142427717bd..17c60297a521e 100644
--- a/lldb/source/Commands/CommandObjectDWIMPrint.cpp
+++ b/lldb/source/Commands/CommandObjectDWIMPrint.cpp
@@ -101,6 +101,10 @@ void CommandObjectDWIMPrint::DoExecute(StringRef command,
   // Add a hint if object description was requested, but no description
   // function was implemented.
   auto maybe_add_hint = [&](llvm::StringRef output) {
+    static bool note_shown = false;
+    if (note_shown)
+      return;
+
     // Identify the default output of object description for Swift and
     // Objective-C
     // "<Name: 0x...>. The regex is:
@@ -110,17 +114,14 @@ void CommandObjectDWIMPrint::DoExecute(StringRef command,
     // - Followed by 5 or more hex digits.
     // - Followed by ">".
     // - End with zero or more whitespace characters.
-    const std::regex swift_class_regex("^<\\S+: 0x[[:xdigit:]]{5,}>\\s*$");
+    static const std::regex swift_class_regex(
+        "^<\\S+: 0x[[:xdigit:]]{5,}>\\s*$");
 
     if (GetDebugger().GetShowDontUsePoHint() && target_ptr &&
         (language == lldb::eLanguageTypeSwift ||
          language == lldb::eLanguageTypeObjC) &&
         std::regex_match(output.data(), swift_class_regex)) {
 
-      static bool note_shown = false;
-      if (note_shown)
-        return;
-
       result.AppendNote(
           "object description requested, but type doesn't implement "
           "a custom object description. Consider using \"p\" instead of "


        
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to