Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3077)

Unified Diff: base/strings/string_util.cc

Issue 2709983006: base::JoinString: Pre-allocate the string to its final size. (Closed)
Patch Set: Revert PS 3; removes the fast path. Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/strings/string_util.cc
diff --git a/base/strings/string_util.cc b/base/strings/string_util.cc
index 49ae9435e934febcd1383437616287af2d22ede5..71ae894dd6e7559e1efa0a2623e6328353ce61d4 100644
--- a/base/strings/string_util.cc
+++ b/base/strings/string_util.cc
@@ -875,13 +875,20 @@ char16* WriteInto(string16* str, size_t length_with_null) {
template <typename list_type, typename string_type>
static string_type JoinStringT(const list_type& parts,
BasicStringPiece<string_type> sep) {
- auto iter = parts.begin();
- // Early-exit to avoid bad access to the first element.
- if (iter == parts.end())
+ if (parts.size() == 0)
return string_type();
- // Begin constructing the result from the first element.
- string_type result(iter->data(), iter->size());
+ // Pre-allocate the eventual size of the string. Start with the size of all of
+ // the separators (note that this *assumes* parts.size() > 0).
+ size_t total_size = (parts.size() - 1) * sep.size();
+ for (const auto& part : parts)
+ total_size += part.size();
+ string_type result;
+ result.reserve(total_size);
+
+ auto iter = parts.begin();
+ DCHECK(iter != parts.end());
+ AppendToString(&result, *iter);
++iter;
for (; iter != parts.end(); ++iter) {
@@ -892,6 +899,9 @@ static string_type JoinStringT(const list_type& parts,
AppendToString(&result, *iter);
}
+ // Sanity-check that we pre-allocated correctly.
+ DCHECK_EQ(total_size, result.size());
+
return result;
}
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698