MCPcopy Create free account
hub / github.com/apache/impala / CalculateReserveForVector

Function CalculateReserveForVector

be/src/gutil/strings/split.cc:347–366  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

345// and use the existing template function, but probably this is more clear
346// and more sure to get optimized to reasonable code.
347static 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()

Callers 1

SplitStringUsingFunction · 0.85

Calls 2

dataMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected