-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[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
[lldb] Avoid unnecessary regex check in dwim-print #114608
Conversation
@llvm/pr-subscribers-lldb Author: Dave Lee (kastiglione) ChangesFull diff: https://github.com/llvm/llvm-project/pull/114608.diff 1 Files Affected:
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;
}
|
There was a problem hiding this 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 😅
I will cherry-pick this downstream to swiftlang/llvm-project too. |
static bool note_shown = false; | ||
if (note_shown) | ||
return; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).
@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. |
e244e45
to
e1c631e
Compare
✅ With the latest revision this PR passed the C/C++ code formatter. |
e1c631e
to
a974b17
Compare
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)
An (unmeasured) improvement to performance of
dwim-print
when used aspo
.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.