Skip to content

Commit 61efe36

Browse files
authored
[lldb] Avoid unnecessary regex check in dwim-print (llvm#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.
1 parent f578982 commit 61efe36

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

lldb/source/Commands/CommandObjectDWIMPrint.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ void CommandObjectDWIMPrint::DoExecute(StringRef command,
101101
// Add a hint if object description was requested, but no description
102102
// function was implemented.
103103
auto maybe_add_hint = [&](llvm::StringRef output) {
104+
static bool note_shown = false;
105+
if (note_shown)
106+
return;
107+
104108
// Identify the default output of object description for Swift and
105109
// Objective-C
106110
// "<Name: 0x...>. The regex is:
@@ -110,17 +114,14 @@ void CommandObjectDWIMPrint::DoExecute(StringRef command,
110114
// - Followed by 5 or more hex digits.
111115
// - Followed by ">".
112116
// - End with zero or more whitespace characters.
113-
const std::regex swift_class_regex("^<\\S+: 0x[[:xdigit:]]{5,}>\\s*$");
117+
static const std::regex swift_class_regex(
118+
"^<\\S+: 0x[[:xdigit:]]{5,}>\\s*$");
114119

115120
if (GetDebugger().GetShowDontUsePoHint() && target_ptr &&
116121
(language == lldb::eLanguageTypeSwift ||
117122
language == lldb::eLanguageTypeObjC) &&
118123
std::regex_match(output.data(), swift_class_regex)) {
119124

120-
static bool note_shown = false;
121-
if (note_shown)
122-
return;
123-
124125
result.AppendNote(
125126
"object description requested, but type doesn't implement "
126127
"a custom object description. Consider using \"p\" instead of "

0 commit comments

Comments
 (0)