1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
|
#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
#include <algorithm>
// 利用SSO优化的字符串处理类
class OptimizedStringProcessor
{
public:
// 优化的字符串分割(利用SSO)
static std::vector<std::string> split(const std::string& str, char delimiter)
{
std::vector<std::string> result;
size_t start = 0;
for (size_t i = 0; i <= str.length(); ++i)
{
if (i == str.length() || str[i] == delimiter)
{
if (i > start)
{
// 对于短字符串,这会利用SSO
result.emplace_back(str.substr(start, i - start));
}
start = i + 1;
}
}
return result;
}
// 优化的字符串连接
static std::string join(const std::vector<std::string>& strings, const std::string& separator)
{
if (strings.empty()) return "";
// 预计算总长度以避免多次重新分配
size_t totalLength = 0;
for (const auto& str : strings)
{
totalLength += str.length();
}
totalLength += separator.length() * (strings.size() - 1);
std::string result;
result.reserve(totalLength);
for (size_t i = 0; i < strings.size(); ++i)
{
if (i > 0) result += separator;
result += strings[i];
}
return result;
}
// 字符串缓存(利用SSO减少小字符串的内存开销)
class StringCache
{
private:
std::unordered_map<std::string, std::string> cache;
public:
const std::string& getOrCreate(const std::string& key, std::function<std::string()> factory)
{
auto it = cache.find(key);
if (it != cache.end())
{
return it->second;
}
auto result = cache.emplace(key, factory());
return result.first->second;
}
void printStats() const
{
size_t ssoCount = 0;
size_t heapCount = 0;
for (const auto& [key, value] : cache)
{
// 简单的SSO检测(基于长度)
if (key.length() <= 15 && value.length() <= 15)
{
ssoCount++;
}
else
{
heapCount++;
}
}
std::cout << "Cache stats:" << std::endl;
std::cout << " Total entries: " << cache.size() << std::endl;
std::cout << " Likely SSO entries: " << ssoCount << std::endl;
std::cout << " Likely heap entries: " << heapCount << std::endl;
}
};
};
// SSO友好的数据结构设计
struct Person
{
std::string firstName; // 通常较短,利用SSO
std::string lastName; // 通常较短,利用SSO
std::string email; // 可能较长,但很多情况下仍能利用SSO
std::string phone; // 通常较短,利用SSO
Person(const std::string& first, const std::string& last,
const std::string& mail, const std::string& ph)
: firstName(first), lastName(last), email(mail), phone(ph) {}
std::string getFullName() const
{
// 对于大多数姓名,这会利用SSO
return firstName + " " + lastName;
}
void print() const
{
std::cout << "Person: " << getFullName() << std::endl;
std::cout << " Email: " << email << std::endl;
std::cout << " Phone: " << phone << std::endl;
// 分析SSO使用情况
auto checkSSO = [](const std::string& str, const std::string& name) {
const char* objStart = reinterpret_cast<const char*>(&str);
const char* objEnd = objStart + sizeof(std::string);
const char* dataPtr = str.data();
bool likelySSO = (dataPtr >= objStart && dataPtr < objEnd);
std::cout << " " << name << " likely using SSO: " << (likelySSO ? "Yes" : "No")
<< " (len: " << str.length() << ")" << std::endl;
};
checkSSO(firstName, "firstName");
checkSSO(lastName, "lastName");
checkSSO(email, "email");
checkSSO(phone, "phone");
}
};
void PracticalApplicationsDemo()
{
std::cout << "=== Practical Applications Demo ===" << std::endl;
// 1. 字符串分割和连接
std::cout << "1. String processing:" << std::endl;
std::string csv = "apple,banana,cherry,date,elderberry";
auto parts = OptimizedStringProcessor::split(csv, ',');
std::cout << "Split result:" << std::endl;
for (const auto& part : parts)
{
std::cout << " \"" << part << "\" (len: " << part.length() << ")" << std::endl;
}
auto rejoined = OptimizedStringProcessor::join(parts, " | ");
std::cout << "Rejoined: " << rejoined << std::endl;
// 2. 字符串缓存
std::cout << "\n2. String cache:" << std::endl;
OptimizedStringProcessor::StringCache cache;
// 添加一些短字符串(利用SSO)
cache.getOrCreate("user1", []() { return std::string("Alice"); });
cache.getOrCreate("user2", []() { return std::string("Bob"); });
cache.getOrCreate("user3", []() { return std::string("Charlie"); });
// 添加一些长字符串
cache.getOrCreate("desc1", []() { return std::string("This is a very long description that will not fit in SSO buffer"); });
cache.getOrCreate("desc2", []() { return std::string("Another long description that requires heap allocation"); });
cache.printStats();
// 3. 人员数据结构
std::cout << "\n3. Person data structure:" << std::endl;
std::vector<Person> people = {
{"John", "Doe", "john.doe@email.com", "123-456-7890"},
{"Jane", "Smith", "jane.smith@company.com", "098-765-4321"},
{"Bob", "Johnson", "bob.johnson@verylongcompanyname.com", "555-123-4567"}
};
for (const auto& person : people)
{
person.print();
std::cout << std::endl;
}
}
void BestPracticesDemo()
{
std::cout << "=== Best Practices Demo ===" << std::endl;
std::cout << "SSO Best Practices:" << std::endl;
std::cout << "1. Keep strings short when possible (< 16 characters typically)" << std::endl;
std::cout << "2. Use string literals and const strings for fixed text" << std::endl;
std::cout << "3. Avoid unnecessary string concatenations that exceed SSO threshold" << std::endl;
std::cout << "4. Consider string_view for read-only string parameters" << std::endl;
std::cout << "5. Use reserve() for strings that will grow beyond SSO size" << std::endl;
std::cout << "6. Profile your specific use case - SSO thresholds vary by implementation" << std::endl;
// 演示最佳实践
std::cout << "\nDemonstrating best practices:" << std::endl;
// 好的做法:短字符串
std::string shortId = "ID123";
std::string shortName = "Alice";
std::cout << "Good: Short strings (ID: " << shortId.length() << " chars, Name: " << shortName.length() << " chars)" << std::endl;
// 需要注意的做法:字符串连接
std::string combined = shortName + " " + shortId; // 仍然较短,可能使用SSO
std::cout << "Combined string: \"" << combined << "\" (len: " << combined.length() << ")" << std::endl;
// 预分配大字符串
std::string largeBuffer;
largeBuffer.reserve(1000); // 预分配避免多次重新分配
std::cout << "Pre-allocated large buffer capacity: " << largeBuffer.capacity() << std::endl;
}
int main()
{
PracticalApplicationsDemo();
BestPracticesDemo();
return 0;
}
|