Skip to content

Commit 643ba92

Browse files
committed
[lldb][NFCI] Remove use of ConstString from OptionValueProperties
In the interest of keeping the ConstString StringPool small, this patch aims to remove the use of ConstString from OptionValueProperties. We can maintain quick lookups by using an llvm::StringMap to find the correct index by name. Differential Revision: https://reviews.llvm.org/D152210
1 parent b1a7b7f commit 643ba92

File tree

3 files changed

+44
-41
lines changed

3 files changed

+44
-41
lines changed

lldb/include/lldb/Interpreter/OptionValue.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ class OptionValue {
125125

126126
virtual bool IsAggregateValue() const { return false; }
127127

128-
virtual ConstString GetName() const { return ConstString(); }
128+
virtual llvm::StringRef GetName() const { return llvm::StringRef(); }
129129

130130
virtual bool DumpQualifiedName(Stream &strm) const;
131131

lldb/include/lldb/Interpreter/OptionValueProperties.h

+10-14
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#include "lldb/Core/UniqueCStringMap.h"
1616
#include "lldb/Interpreter/OptionValue.h"
1717
#include "lldb/Interpreter/Property.h"
18-
#include "lldb/Utility/ConstString.h"
1918

2019
namespace lldb_private {
2120
class Properties;
@@ -26,7 +25,7 @@ class OptionValueProperties
2625
public:
2726
OptionValueProperties() = default;
2827

29-
OptionValueProperties(ConstString name);
28+
OptionValueProperties(llvm::StringRef name);
3029

3130
~OptionValueProperties() override = default;
3231

@@ -49,7 +48,7 @@ class OptionValueProperties
4948

5049
llvm::json::Value ToJSON(const ExecutionContext *exe_ctx) override;
5150

52-
ConstString GetName() const override { return m_name; }
51+
llvm::StringRef GetName() const override { return m_name; }
5352

5453
virtual Status DumpPropertyValue(const ExecutionContext *exe_ctx,
5554
Stream &strm, llvm::StringRef property_path,
@@ -68,13 +67,13 @@ class OptionValueProperties
6867
// Get the index of a property given its exact name in this property
6968
// collection, "name" can't be a path to a property path that refers to a
7069
// property within a property
71-
virtual size_t GetPropertyIndex(ConstString name) const;
70+
virtual size_t GetPropertyIndex(llvm::StringRef name) const;
7271

7372
// Get a property by exact name exists in this property collection, name can
7473
// not be a path to a property path that refers to a property within a
7574
// property
7675
virtual const Property *
77-
GetProperty(ConstString name,
76+
GetProperty(llvm::StringRef name,
7877
const ExecutionContext *exe_ctx = nullptr) const;
7978

8079
virtual const Property *
@@ -87,14 +86,13 @@ class OptionValueProperties
8786
// "target.process.extra-startup-command"
8887
virtual const Property *
8988
GetPropertyAtPath(const ExecutionContext *exe_ctx,
90-
9189
llvm::StringRef property_path) const;
9290

9391
virtual lldb::OptionValueSP
9492
GetPropertyValueAtIndex(size_t idx, const ExecutionContext *exe_ctx) const;
9593

9694
virtual lldb::OptionValueSP GetValueForKey(const ExecutionContext *exe_ctx,
97-
ConstString key) const;
95+
llvm::StringRef key) const;
9896

9997
lldb::OptionValueSP GetSubValue(const ExecutionContext *exe_ctx,
10098
llvm::StringRef name,
@@ -131,11 +129,11 @@ class OptionValueProperties
131129
OptionValueFileSpecList *GetPropertyAtIndexAsOptionValueFileSpecList(
132130
size_t idx, const ExecutionContext *exe_ctx = nullptr) const;
133131

134-
void AppendProperty(ConstString name, llvm::StringRef desc, bool is_global,
135-
const lldb::OptionValueSP &value_sp);
132+
void AppendProperty(llvm::StringRef name, llvm::StringRef desc,
133+
bool is_global, const lldb::OptionValueSP &value_sp);
136134

137135
lldb::OptionValuePropertiesSP GetSubProperty(const ExecutionContext *exe_ctx,
138-
ConstString name);
136+
llvm::StringRef name);
139137

140138
void SetValueChangedCallback(size_t property_idx,
141139
std::function<void()> callback);
@@ -176,11 +174,9 @@ class OptionValueProperties
176174
return ((idx < m_properties.size()) ? &m_properties[idx] : nullptr);
177175
}
178176

179-
typedef UniqueCStringMap<size_t> NameToIndex;
180-
181-
ConstString m_name;
177+
std::string m_name;
182178
std::vector<Property> m_properties;
183-
NameToIndex m_name_to_index;
179+
llvm::StringMap<size_t> m_name_to_index;
184180
};
185181

186182
} // namespace lldb_private

lldb/source/Interpreter/OptionValueProperties.cpp

+33-26
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,17 @@
2020
using namespace lldb;
2121
using namespace lldb_private;
2222

23-
OptionValueProperties::OptionValueProperties(ConstString name) : m_name(name) {}
23+
OptionValueProperties::OptionValueProperties(llvm::StringRef name)
24+
: m_name(name.str()) {}
2425

2526
void OptionValueProperties::Initialize(const PropertyDefinitions &defs) {
2627
for (const auto &definition : defs) {
2728
Property property(definition);
2829
assert(property.IsValid());
29-
m_name_to_index.Append(ConstString(property.GetName()),
30-
m_properties.size());
30+
m_name_to_index.insert({property.GetName(), m_properties.size()});
3131
property.GetValue()->SetParent(shared_from_this());
3232
m_properties.push_back(property);
3333
}
34-
m_name_to_index.Sort();
3534
}
3635

3736
void OptionValueProperties::SetValueChangedCallback(
@@ -41,24 +40,25 @@ void OptionValueProperties::SetValueChangedCallback(
4140
property->SetValueChangedCallback(std::move(callback));
4241
}
4342

44-
void OptionValueProperties::AppendProperty(ConstString name,
43+
void OptionValueProperties::AppendProperty(llvm::StringRef name,
4544
llvm::StringRef desc, bool is_global,
4645
const OptionValueSP &value_sp) {
47-
Property property(name.GetStringRef(), desc, is_global, value_sp);
48-
m_name_to_index.Append(name, m_properties.size());
46+
Property property(name, desc, is_global, value_sp);
47+
m_name_to_index.insert({name, m_properties.size()});
4948
m_properties.push_back(property);
5049
value_sp->SetParent(shared_from_this());
51-
m_name_to_index.Sort();
5250
}
5351

5452
lldb::OptionValueSP
5553
OptionValueProperties::GetValueForKey(const ExecutionContext *exe_ctx,
56-
ConstString key) const {
57-
lldb::OptionValueSP value_sp;
58-
size_t idx = m_name_to_index.Find(key, SIZE_MAX);
59-
if (idx < m_properties.size())
60-
value_sp = GetPropertyAtIndex(idx, exe_ctx)->GetValue();
61-
return value_sp;
54+
llvm::StringRef key) const {
55+
auto iter = m_name_to_index.find(key);
56+
if (iter == m_name_to_index.end())
57+
return OptionValueSP();
58+
const size_t idx = iter->second;
59+
if (idx >= m_properties.size())
60+
return OptionValueSP();
61+
return GetPropertyAtIndex(idx, exe_ctx)->GetValue();
6262
}
6363

6464
lldb::OptionValueSP
@@ -69,13 +69,13 @@ OptionValueProperties::GetSubValue(const ExecutionContext *exe_ctx,
6969
return OptionValueSP();
7070

7171
llvm::StringRef sub_name;
72-
ConstString key;
72+
llvm::StringRef key;
7373
size_t key_len = name.find_first_of(".[{");
7474
if (key_len != llvm::StringRef::npos) {
75-
key.SetString(name.take_front(key_len));
75+
key = name.take_front(key_len);
7676
sub_name = name.drop_front(key_len);
7777
} else
78-
key.SetString(name);
78+
key = name;
7979

8080
value_sp = GetValueForKey(exe_ctx, key);
8181
if (sub_name.empty() || !value_sp)
@@ -138,14 +138,20 @@ Status OptionValueProperties::SetSubValue(const ExecutionContext *exe_ctx,
138138
return error;
139139
}
140140

141-
size_t OptionValueProperties::GetPropertyIndex(ConstString name) const {
142-
return m_name_to_index.Find(name, SIZE_MAX);
141+
size_t OptionValueProperties::GetPropertyIndex(llvm::StringRef name) const {
142+
auto iter = m_name_to_index.find(name);
143+
if (iter == m_name_to_index.end())
144+
return SIZE_MAX;
145+
return iter->second;
143146
}
144147

145148
const Property *
146-
OptionValueProperties::GetProperty(ConstString name,
149+
OptionValueProperties::GetProperty(llvm::StringRef name,
147150
const ExecutionContext *exe_ctx) const {
148-
return GetPropertyAtIndex(m_name_to_index.Find(name, SIZE_MAX), exe_ctx);
151+
auto iter = m_name_to_index.find(name);
152+
if (iter == m_name_to_index.end())
153+
return nullptr;
154+
return GetPropertyAtIndex(iter->second, exe_ctx);
149155
}
150156

151157
lldb::OptionValueSP OptionValueProperties::GetPropertyValueAtIndex(
@@ -399,18 +405,19 @@ OptionValueProperties::DeepCopy(const OptionValueSP &new_parent) const {
399405
const Property *
400406
OptionValueProperties::GetPropertyAtPath(const ExecutionContext *exe_ctx,
401407
llvm::StringRef name) const {
402-
const Property *property = nullptr;
403408
if (name.empty())
404409
return nullptr;
410+
411+
const Property *property = nullptr;
405412
llvm::StringRef sub_name;
406-
ConstString key;
413+
llvm::StringRef key;
407414
size_t key_len = name.find_first_of(".[{");
408415

409416
if (key_len != llvm::StringRef::npos) {
410-
key.SetString(name.take_front(key_len));
417+
key = name.take_front(key_len);
411418
sub_name = name.drop_front(key_len);
412419
} else
413-
key.SetString(name);
420+
key = name;
414421

415422
property = GetProperty(key, exe_ctx);
416423
if (sub_name.empty() || !property)
@@ -473,7 +480,7 @@ void OptionValueProperties::Apropos(
473480

474481
lldb::OptionValuePropertiesSP
475482
OptionValueProperties::GetSubProperty(const ExecutionContext *exe_ctx,
476-
ConstString name) {
483+
llvm::StringRef name) {
477484
lldb::OptionValueSP option_value_sp(GetValueForKey(exe_ctx, name));
478485
if (option_value_sp) {
479486
OptionValueProperties *ov_properties = option_value_sp->GetAsProperties();

0 commit comments

Comments
 (0)