Skip to content

Commit 2e82696

Browse files
authored
[lldb][NFCI] Remove use of ConstString from FilterRule in StructuredDataDarwinLog (#68347)
There are only ever 2 FilterRules and their operations are either "regex" or "match". This does not benefit from deduplication since the strings have static lifetime and we can just compare StringRefs pointing to them. This is also not on a fast path, so it doesn't really benefit from the pointer comparisons of ConstStrings.
1 parent 303e020 commit 2e82696

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

lldb/source/Plugins/StructuredData/DarwinLog/StructuredDataDarwinLog.cpp

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
#include "lldb/Utility/Log.h"
3333
#include "lldb/Utility/RegularExpression.h"
3434

35+
#include "llvm/ADT/StringMap.h"
36+
3537
#define DARWIN_LOG_TYPE_VALUE "DarwinLog"
3638

3739
using namespace lldb;
@@ -183,21 +185,20 @@ class FilterRule {
183185
std::function<FilterRuleSP(bool accept, size_t attribute_index,
184186
const std::string &op_arg, Status &error)>;
185187

186-
static void RegisterOperation(ConstString operation,
188+
static void RegisterOperation(llvm::StringRef operation,
187189
const OperationCreationFunc &creation_func) {
188190
GetCreationFuncMap().insert(std::make_pair(operation, creation_func));
189191
}
190192

191193
static FilterRuleSP CreateRule(bool match_accepts, size_t attribute,
192-
ConstString operation,
194+
llvm::StringRef operation,
193195
const std::string &op_arg, Status &error) {
194196
// Find the creation func for this type of filter rule.
195197
auto map = GetCreationFuncMap();
196198
auto find_it = map.find(operation);
197199
if (find_it == map.end()) {
198-
error.SetErrorStringWithFormat("unknown filter operation \""
199-
"%s\"",
200-
operation.GetCString());
200+
error.SetErrorStringWithFormatv("unknown filter operation \"{0}\"",
201+
operation);
201202
return FilterRuleSP();
202203
}
203204

@@ -217,7 +218,7 @@ class FilterRule {
217218
dict_p->AddStringItem("attribute", s_filter_attributes[m_attribute_index]);
218219

219220
// Indicate the type of the rule.
220-
dict_p->AddStringItem("type", GetOperationType().GetCString());
221+
dict_p->AddStringItem("type", GetOperationType());
221222

222223
// Let the rule add its own specific details here.
223224
DoSerialization(*dict_p);
@@ -227,10 +228,10 @@ class FilterRule {
227228

228229
virtual void Dump(Stream &stream) const = 0;
229230

230-
ConstString GetOperationType() const { return m_operation; }
231+
llvm::StringRef GetOperationType() const { return m_operation; }
231232

232233
protected:
233-
FilterRule(bool accept, size_t attribute_index, ConstString operation)
234+
FilterRule(bool accept, size_t attribute_index, llvm::StringRef operation)
234235
: m_accept(accept), m_attribute_index(attribute_index),
235236
m_operation(operation) {}
236237

@@ -243,7 +244,7 @@ class FilterRule {
243244
}
244245

245246
private:
246-
using CreationFuncMap = std::map<ConstString, OperationCreationFunc>;
247+
using CreationFuncMap = llvm::StringMap<OperationCreationFunc>;
247248

248249
static CreationFuncMap &GetCreationFuncMap() {
249250
static CreationFuncMap s_map;
@@ -252,7 +253,8 @@ class FilterRule {
252253

253254
const bool m_accept;
254255
const size_t m_attribute_index;
255-
const ConstString m_operation;
256+
// The lifetime of m_operation should be static.
257+
const llvm::StringRef m_operation;
256258
};
257259

258260
using FilterRules = std::vector<FilterRuleSP>;
@@ -296,8 +298,8 @@ class RegexFilterRule : public FilterRule {
296298
return FilterRuleSP(new RegexFilterRule(accept, attribute_index, op_arg));
297299
}
298300

299-
static ConstString StaticGetOperation() {
300-
static ConstString s_operation("regex");
301+
static llvm::StringRef StaticGetOperation() {
302+
static constexpr llvm::StringLiteral s_operation("regex");
301303
return s_operation;
302304
}
303305

@@ -341,8 +343,8 @@ class ExactMatchFilterRule : public FilterRule {
341343
new ExactMatchFilterRule(accept, attribute_index, op_arg));
342344
}
343345

344-
static ConstString StaticGetOperation() {
345-
static ConstString s_operation("match");
346+
static llvm::StringRef StaticGetOperation() {
347+
static constexpr llvm::StringLiteral s_operation("match");
346348
return s_operation;
347349
}
348350

@@ -701,7 +703,7 @@ class EnableOptions : public Options {
701703

702704
// add filter spec
703705
auto rule_sp = FilterRule::CreateRule(
704-
accept, attribute_index, ConstString(operation),
706+
accept, attribute_index, operation,
705707
std::string(rule_text.substr(operation_end_pos + 1)), error);
706708

707709
if (rule_sp && error.Success())

0 commit comments

Comments
 (0)