[Strings] Split String Optimizations (#1325)

* Switch the 2 split calls to SplitString

* Nuke duplicate split in favor of SplitString #1263

* Add a test for SplitString

* Optimize SplitString

Benchmarking:
--------------------------------------------------------------
Benchmark                       Time           CPU Iterations
--------------------------------------------------------------
bench_oldsplit               5201 ns       5201 ns     129500
bench_split                  1269 ns       1269 ns     548906

This is splitting a VERY long SpecialAbilities string. This is ~75%
speed up.
This commit is contained in:
Michael Cook (mackal)
2021-04-23 00:36:39 -04:00
committed by GitHub
parent 00fb9bc9f9
commit dba3010c89
5 changed files with 24 additions and 24 deletions
+11 -20
View File
@@ -74,18 +74,6 @@ const std::string str_tolower(std::string s)
return s;
}
std::vector<std::string> split(std::string str_to_split, char delimiter)
{
std::stringstream ss(str_to_split);
std::string item;
std::vector<std::string> exploded_values;
while (std::getline(ss, item, delimiter)) {
exploded_values.push_back(item);
}
return exploded_values;
}
const std::string str_toupper(std::string s)
{
std::transform(
@@ -113,15 +101,18 @@ const std::string StringFormat(const char *format, ...)
return output;
}
std::vector<std::string> SplitString(const std::string &str, char delim) {
std::vector<std::string> SplitString(const std::string &str, const char delim) {
std::vector<std::string> ret;
std::stringstream ss(str);
std::string item;
while(std::getline(ss, item, delim)) {
ret.push_back(item);
}
std::string::size_type start = 0;
auto end = str.find(delim);
while (end != std::string::npos) {
ret.emplace_back(str, start, end - start);
start = end + 1;
end = str.find(delim, start);
}
// this will catch the last word since the string is unlikely to end with a delimiter
if (str.length() > start)
ret.emplace_back(str, start, str.length() - start);
return ret;
}