Skip to content

[lldb] Avoid unnecessary regex check in dwim-print #114608

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

kastiglione
Copy link
Contributor

@kastiglione kastiglione commented Nov 1, 2024

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.

@llvmbot
Copy link
Member

llvmbot commented Nov 1, 2024

@llvm/pr-subscribers-lldb

Author: Dave Lee (kastiglione)

Changes

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

1 Files Affected:

  • (modified) lldb/source/Commands/CommandObjectDWIMPrint.cpp (+8-8)
diff --git a/lldb/source/Commands/CommandObjectDWIMPrint.cpp b/lldb/source/Commands/CommandObjectDWIMPrint.cpp
index 76bed100dc7291..ae35f6e43f6c62 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,20 +114,16 @@ 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 class_po_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;
+        std::regex_match(output.data(), class_po_regex)) {
 
       result.GetOutputStream()
-          << "note: object description requested, but type doesn't implement "
-             "a custom object description. Consider using \"p\" instead of "
+          << "note: object description requested, but type doesn't implement a "
+             "custom object description. Consider using \"p\" instead of "
              "\"po\" (this note will only be shown once per debug session).\n";
       note_shown = true;
     }

Copy link
Contributor

@augusto2112 augusto2112 left a comment

Choose a reason for hiding this comment

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

Nice! This code lives upstream though so you'll need to open a patch there too

EDIT: never mind, for some reason I thought this PR was opened downstream 😅

@kastiglione
Copy link
Contributor Author

I will cherry-pick this downstream to swiftlang/llvm-project too.

Comment on lines +104 to +106
static bool note_shown = false;
if (note_shown)
return;
Copy link
Member

Choose a reason for hiding this comment

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

I realize this code was already there, but this isn't great. Can we store this at the debugger level, for example? I'd expect this to show up once per debugger, and again after having called SBDebugger::Terminate/Initialize.

Copy link
Collaborator

Choose a reason for hiding this comment

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

If we tie it to a module, we could use the mechanism introduced in https://github.com/llvm/llvm-project/pull/112801/files to automatically show it once per session.

I'd expect this to show up once per debugger

You mean once per type and debugger, right?

Copy link
Contributor Author

@kastiglione kastiglione Nov 1, 2024

Choose a reason for hiding this comment

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

I think @augusto2112 and I discussed and agreed not to do once per type. We figured that might make the hint too noisy.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am indifferent about per-debugger. I don't consider this note as being critical, as long as it's shown once in a while, I'm good.

Copy link
Collaborator

Choose a reason for hiding this comment

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

If we already landed on only once per session I'm fine with that too.
You can also manually allocate a std::once and pass it to the Debugger-wide function. Or just do the call_once here.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes the idea was for it not to show up too frequently, I'd be ok if it's once per debugger too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't see how call_once would work here. This lambda might be called once, or it might be called multiple times (even indefinitely).

@kastiglione
Copy link
Contributor Author

@adrian-prantl @JDevlieghere I'm going to merge this, since this change improves performance and doesn't introduce the logic being discussed. I'm happy to do a follow up, but it's still unclear to me what exactly that looks like.

@kastiglione kastiglione force-pushed the lldb-Avoid-unnecessary-regex-check-in-dwim-print branch from e244e45 to e1c631e Compare March 8, 2025 02:28
Copy link

github-actions bot commented Mar 8, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@kastiglione kastiglione force-pushed the lldb-Avoid-unnecessary-regex-check-in-dwim-print branch from e1c631e to a974b17 Compare March 8, 2025 03:11
@kastiglione kastiglione merged commit 61efe36 into llvm:main Mar 8, 2025
10 checks passed
@kastiglione kastiglione deleted the lldb-Avoid-unnecessary-regex-check-in-dwim-print branch March 8, 2025 05:57
kastiglione added a commit to swiftlang/llvm-project that referenced this pull request Mar 15, 2025
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.

(cherry picked from commit 61efe36)
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.

5 participants