Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Grow StringData multiplicatively on appends

Some endpoints like WebGraphQLIPhoneFeedController and
/api/perflab_graphql:feed_before do a lot of string appends, for which
we end up allocating O(n^2) memory.

When we append to a string, it is usually a good indication that
we will do more appends. This diff allocates memory for newly
appended strings in a multiplicative manner.
  • Loading branch information...
commit b2ad32673e5db39dddf7246160d2fa4738a9929e 1 parent 76da87d
aravind authored sgolemon committed
Showing with 8 additions and 2 deletions.
  1. +8 −2 src/runtime/base/string_data.cpp
View
10 src/runtime/base/string_data.cpp
@@ -373,12 +373,18 @@ void StringData::append(const char *s, int len) {
ASSERT((oldp > s && oldp - s > len) ||
(oldp < s && s - oldp > oldlen)); // no overlapping
newlen = oldlen + len;
- char* newdata = (char*) smart_realloc(oldp, newlen + 1);
+ char* newdata;
+ if ((int)newlen <= capacity()) {
+ newdata = oldp;
+ } else {
+ uint32_t nlen = newlen + (newlen >> 2);
+ newdata = (char*) smart_realloc(oldp, nlen + 1);
+ m_big.cap = nlen | IsSmart;
+ }
memcpy(newdata + oldlen, s, len);
newdata[newlen] = 0;
m_len = newlen;
m_data = newdata;
- m_big.cap = newlen | IsSmart;
m_hash = 0;
} else {
// generic "big string concat" path. realloc buffer.
Please sign in to comment.
Something went wrong with that request. Please try again.