[Lldb-commits] [lldb] [lldb][NFCI] Remove use of ConstString from FilterRule in StructuredDataDarwinLog (PR #68347)

Alex Langford via lldb-commits lldb-commits at lists.llvm.org
Thu Oct 5 12:57:21 PDT 2023


https://github.com/bulbazord created https://github.com/llvm/llvm-project/pull/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.

>From 2c8ae3c5d1ff8b841255a79f7f64f81dd2bf9df1 Mon Sep 17 00:00:00 2001
From: Alex Langford <alangford at apple.com>
Date: Thu, 5 Oct 2023 12:52:10 -0700
Subject: [PATCH] [lldb][NFCI] Remove use of ConstString from FilterRule in
 StructuredDataDarwinLog

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.
---
 .../DarwinLog/StructuredDataDarwinLog.cpp     | 32 ++++++++++---------
 1 file changed, 17 insertions(+), 15 deletions(-)

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())



More information about the lldb-commits mailing list