From 05cfdb0eac37eb5571518e1ef2ade9f0397dbc80 Mon Sep 17 00:00:00 2001 From: Raphael Isemann Date: Fri, 26 Apr 2019 07:21:36 +0000 Subject: Allow direct comparison of ConstString against StringRef Summary: When we want to compare a ConstString against a string literal (or any other non-ConstString), we currently have to explicitly turn the other string into a ConstString. This makes sense as comparing ConstStrings against each other is only a fast pointer comparison. However, currently we (rather incorrectly) use in several places in LLDB temporary ConstStrings when we just want to compare a given ConstString against a hardcoded value, for example like this: ``` if (extension != ConstString(".oat") && extension != ConstString(".odex")) ``` Obviously this kind of defeats the point of ConstStrings. In the comparison above we would construct two temporary ConstStrings every time we hit the given code. Constructing a ConstString is relatively expensive: we need to go to the StringPool, take a read and possibly an exclusive write-lock and then look up our temporary string in the string map of the pool. So we do a lot of heavy work for essentially just comparing a <6 characters in two strings. I initially wanted to just fix these issues by turning the temporary ConstString in static variables/ members, but that made the code much less readable. Instead I propose to add a new overload for the ConstString comparison operator that takes a StringRef. This comparison operator directly compares the ConstString content against the given StringRef without turning the StringRef into a ConstString. This means that the example above can look like this now: ``` if (extension != ".oat" && extension != ".odex") ``` It also no longer has to unlock/lock two locks and call multiple functions in other TUs for constructing the temporary ConstString instances. Instead this should end up just being a direct string comparison of the two given strings on most compilers. This patch also directly updates all uses of temporary and short ConstStrings in LLDB to use this new comparison operator. It also adds a some unit tests for the new and old comparison operator. Reviewers: #lldb, JDevlieghere, espindola, amccarth Reviewed By: JDevlieghere, amccarth Subscribers: amccarth, clayborg, JDevlieghere, emaste, arichardson, MaskRay, lldb-commits Tags: #lldb Differential Revision: https://reviews.llvm.org/D60667 llvm-svn: 359281 --- lldb/source/Plugins/Language/CPlusPlus/LibStdcpp.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'lldb/source/Plugins/Language/CPlusPlus/LibStdcpp.cpp') diff --git a/lldb/source/Plugins/Language/CPlusPlus/LibStdcpp.cpp b/lldb/source/Plugins/Language/CPlusPlus/LibStdcpp.cpp index 0aac93c3c76..0e0f6663c92 100644 --- a/lldb/source/Plugins/Language/CPlusPlus/LibStdcpp.cpp +++ b/lldb/source/Plugins/Language/CPlusPlus/LibStdcpp.cpp @@ -142,9 +142,9 @@ bool LibstdcppMapIteratorSyntheticFrontEnd::MightHaveChildren() { return true; } size_t LibstdcppMapIteratorSyntheticFrontEnd::GetIndexOfChildWithName( ConstString name) { - if (name == ConstString("first")) + if (name == "first") return 0; - if (name == ConstString("second")) + if (name == "second") return 1; return UINT32_MAX; } @@ -224,7 +224,7 @@ bool VectorIteratorSyntheticFrontEnd::MightHaveChildren() { return true; } size_t VectorIteratorSyntheticFrontEnd::GetIndexOfChildWithName( ConstString name) { - if (name == ConstString("item")) + if (name == "item") return 0; return UINT32_MAX; } @@ -374,7 +374,7 @@ bool LibStdcppSharedPtrSyntheticFrontEnd::MightHaveChildren() { return true; } size_t LibStdcppSharedPtrSyntheticFrontEnd::GetIndexOfChildWithName( ConstString name) { - if (name == ConstString("_M_ptr")) + if (name == "_M_ptr") return 0; return UINT32_MAX; } -- cgit v1.2.3