Optimize wmem_strbuf_grow.

As suggested by Anders, in the case of repeated calls to wmem_strbuf_append_c
(and other functions which append very little data) the growth check was a
substantial portion of the over-all running time. By short-circuiting the check
in the case where growth isn't needed (as opposed to letting it fall-through
naturally) we shave ~25% off the cost of such repeated calls in my benchmarks.

The function (wmem_strbuf_grow) is inline, so the compiler should be able to
optimize each caller individually for the short-circuit.

Change-Id: I76419020f4d8fa675906eb77798969b6c61c7732
Reviewed-on: https://code.wireshark.org/review/1467
Reviewed-by: Anders Broman <a.broman58@gmail.com>
This commit is contained in:
Evan Huus 2014-05-02 09:53:33 -04:00 committed by Anders Broman
parent 34fa169719
commit f181756640
1 changed files with 8 additions and 0 deletions

View File

@ -106,6 +106,14 @@ wmem_strbuf_grow(wmem_strbuf_t *strbuf, const gsize to_add)
{
gsize new_alloc_len, new_len;
/* short-circuit for efficiency if we have room already; greatly speeds up
* repeated calls to wmem_strbuf_append_c and others which grow a little bit
* at a time.
*/
if (WMEM_STRBUF_ROOM(strbuf) >= to_add) {
return;
}
new_alloc_len = strbuf->alloc_len;
new_len = strbuf->len + to_add;