Improve performance of stringPrintf and related functions
authorChip Turner <chip@fb.com>
Thu, 11 Dec 2014 06:14:09 +0000 (22:14 -0800)
committerJoelMarcey <joelm@fb.com>
Thu, 18 Dec 2014 20:29:40 +0000 (12:29 -0800)
commit70d6b0b6ef1f09c63ab51ceb15cc843024cf4ffe
treedf90a017ca79338a5a01112a64d73903251074c3
parenta33734e4d10197cf085bacf2c3c32ccd9fb0e119
Improve performance of stringPrintf and related functions

Summary:
It turned out at least one optimization we were doing for
stringPrintf (using the tail of the input buffer) was causing a
performance degradation in some cases -- if the string was already
pre-sized, our resize() call would end up memset'ing the tail.  In some
instances, this could cause significant performance penalties, such as
when multiple stringAppendf calls were made.

So, this diff removes the "optimization" around using the tail of the input
buffer and instead uses a standalone stack buffer.  If vsnprintf deems
that buffer too small, a heap buffer is instead used.

As there is no std::string method that resizes the string to fill the
underlying buffer without setting the values to a default, and as it is
not legal to write past the end of the data buffer (even if capacity()
says there is enough), let's just abandon that optimization.  It turns
out this doesn't have a major performance loss for most cases since,
with this diff, most small strings will fit on-stack and then hopefully
in the string's tail anyway.

Test Plan: runtests

Reviewed By: simpkins@fb.com

Subscribers: trunkagent, net-systems@, lins, anca, folly-diffs@

FB internal diff: D1733774

Tasks: 5735468

Signature: t1:1733774:1418323943:ec47007c9756aca6cf0466bce92722ac4c7e89b2
folly/String.cpp
folly/test/StringTest.cpp