Skip to content

[lldb][NFCI] Remove use of ConstString from FilterRule in StructuredDataDarwinLog #68347

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

Merged
merged 1 commit into from
Oct 9, 2023

Conversation

bulbazord
Copy link
Member

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.

…ataDarwinLog

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.
@llvmbot llvmbot added the lldb label Oct 5, 2023
@llvmbot
Copy link
Member

llvmbot commented Oct 5, 2023

@llvm/pr-subscribers-lldb

Changes

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.


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

1 Files Affected:

  • (modified) lldb/source/Plugins/StructuredData/DarwinLog/StructuredDataDarwinLog.cpp (+17-15)
diff --git a/lldb/source/Plugins/StructuredData/DarwinLog/StructuredDataDarwinLog.cpp b/lldb/source/Plugins/StructuredData/DarwinLog/StructuredDataDarwinLog.cpp
index 61e04900da342d2..f8a8df84ca37f29 100644
--- a/lldb/source/Plugins/StructuredData/DarwinLog/StructuredDataDarwinLog.cpp
+++ b/lldb/source/Plugins/StructuredData/DarwinLog/StructuredDataDarwinLog.cpp
@@ -32,6 +32,8 @@
 #include "lldb/Utility/Log.h"
 #include "lldb/Utility/RegularExpression.h"
 
+#include "llvm/ADT/StringMap.h"
+
 #define DARWIN_LOG_TYPE_VALUE "DarwinLog"
 
 using namespace lldb;
@@ -183,21 +185,20 @@ class FilterRule {
       std::function<FilterRuleSP(bool accept, size_t attribute_index,
                                  const std::string &op_arg, Status &error)>;
 
-  static void RegisterOperation(ConstString operation,
+  static void RegisterOperation(llvm::StringRef operation,
                                 const OperationCreationFunc &creation_func) {
     GetCreationFuncMap().insert(std::make_pair(operation, creation_func));
   }
 
   static FilterRuleSP CreateRule(bool match_accepts, size_t attribute,
-                                 ConstString operation,
+                                 llvm::StringRef operation,
                                  const std::string &op_arg, Status &error) {
     // Find the creation func for this type of filter rule.
     auto map = GetCreationFuncMap();
     auto find_it = map.find(operation);
     if (find_it == map.end()) {
-      error.SetErrorStringWithFormat("unknown filter operation \""
-                                     "%s\"",
-                                     operation.GetCString());
+      error.SetErrorStringWithFormatv("unknown filter operation \"{0}\"",
+                                      operation);
       return FilterRuleSP();
     }
 
@@ -217,7 +218,7 @@ class FilterRule {
     dict_p->AddStringItem("attribute", s_filter_attributes[m_attribute_index]);
 
     // Indicate the type of the rule.
-    dict_p->AddStringItem("type", GetOperationType().GetCString());
+    dict_p->AddStringItem("type", GetOperationType());
 
     // Let the rule add its own specific details here.
     DoSerialization(*dict_p);
@@ -227,10 +228,10 @@ class FilterRule {
 
   virtual void Dump(Stream &stream) const = 0;
 
-  ConstString GetOperationType() const { return m_operation; }
+  llvm::StringRef GetOperationType() const { return m_operation; }
 
 protected:
-  FilterRule(bool accept, size_t attribute_index, ConstString operation)
+  FilterRule(bool accept, size_t attribute_index, llvm::StringRef operation)
       : m_accept(accept), m_attribute_index(attribute_index),
         m_operation(operation) {}
 
@@ -243,7 +244,7 @@ class FilterRule {
   }
 
 private:
-  using CreationFuncMap = std::map<ConstString, OperationCreationFunc>;
+  using CreationFuncMap = llvm::StringMap<OperationCreationFunc>;
 
   static CreationFuncMap &GetCreationFuncMap() {
     static CreationFuncMap s_map;
@@ -252,7 +253,8 @@ class FilterRule {
 
   const bool m_accept;
   const size_t m_attribute_index;
-  const ConstString m_operation;
+  // The lifetime of m_operation should be static.
+  const llvm::StringRef m_operation;
 };
 
 using FilterRules = std::vector<FilterRuleSP>;
@@ -296,8 +298,8 @@ class RegexFilterRule : public FilterRule {
     return FilterRuleSP(new RegexFilterRule(accept, attribute_index, op_arg));
   }
 
-  static ConstString StaticGetOperation() {
-    static ConstString s_operation("regex");
+  static llvm::StringRef StaticGetOperation() {
+    static constexpr llvm::StringLiteral s_operation("regex");
     return s_operation;
   }
 
@@ -341,8 +343,8 @@ class ExactMatchFilterRule : public FilterRule {
         new ExactMatchFilterRule(accept, attribute_index, op_arg));
   }
 
-  static ConstString StaticGetOperation() {
-    static ConstString s_operation("match");
+  static llvm::StringRef StaticGetOperation() {
+    static constexpr llvm::StringLiteral s_operation("match");
     return s_operation;
   }
 
@@ -701,7 +703,7 @@ class EnableOptions : public Options {
 
     // add filter spec
     auto rule_sp = FilterRule::CreateRule(
-        accept, attribute_index, ConstString(operation),
+        accept, attribute_index, operation,
         std::string(rule_text.substr(operation_end_pos + 1)), error);
 
     if (rule_sp && error.Success())

@bulbazord bulbazord merged commit 2e82696 into llvm:main Oct 9, 2023
@bulbazord bulbazord deleted the const-string-darwin-log branch October 9, 2023 17:37
@@ -252,7 +253,8 @@ class FilterRule {

const bool m_accept;
const size_t m_attribute_index;
const ConstString m_operation;
// The lifetime of m_operation should be static.
Copy link
Member

Choose a reason for hiding this comment

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

Static seems confusing in this context. Do you mean should be fixed for the lifetime of this object?

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, yeah it is a little confusing now that I look at it. I wanted to convey that the lifetime of the StringRef should be as long as the lifetime of the program (since it's backed by a static string literal right now). Maybe something like:

"m_operation is backed by a static string literal, so lifetime should not be a concern"

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.

4 participants