If we know how much to allocate for a vector of strings, we can allocate the vector only once and directly to the right size. This saves in between 33-66 % of memory space needed for the result, and runs faster in the microbenchmarks. The reserve is only implemented for the single character delim. The implementation for counting is cut-and-pasted from SplitStringToIteratorUsing. I could
| 345 | // and use the existing template function, but probably this is more clear |
| 346 | // and more sure to get optimized to reasonable code. |
| 347 | static int CalculateReserveForVector(const string& full, const char* delim) { |
| 348 | int count = 0; |
| 349 | if (delim[0] != '\0' && delim[1] == '\0') { |
| 350 | // Optimize the common case where delim is a single character. |
| 351 | char c = delim[0]; |
| 352 | const char* p = full.data(); |
| 353 | const char* end = p + full.size(); |
| 354 | while (p != end) { |
| 355 | if (*p == c) { // This could be optimized with hasless(v,1) trick. |
| 356 | ++p; |
| 357 | } else { |
| 358 | while (++p != end && *p != c) { |
| 359 | // Skip to the next occurence of the delimiter. |
| 360 | } |
| 361 | ++count; |
| 362 | } |
| 363 | } |
| 364 | } |
| 365 | return count; |
| 366 | } |
| 367 | |
| 368 | // ---------------------------------------------------------------------- |
| 369 | // SplitStringUsing() |
no test coverage detected