diff options
author | Nick Wellnhofer <wellnhofer@aevum.de> | 2015-05-30 00:38:20 +0200 |
---|---|---|
committer | Nick Wellnhofer <wellnhofer@aevum.de> | 2015-06-07 21:42:15 +0200 |
commit | 1a38daeb81db11ef2acd57690aad36b4ce3fe3da (patch) | |
tree | 99866ef84b7b20b6da065f48ca774afef404e72a /src/buffer.c | |
parent | 1193050109dee6be85c82bd29a1c817532dde912 (diff) |
Simplify oversizing of strbufs
Always add 50% on top of target size. No need for a loop.
Diffstat (limited to 'src/buffer.c')
-rw-r--r-- | src/buffer.c | 11 |
1 files changed, 4 insertions, 7 deletions
diff --git a/src/buffer.c b/src/buffer.c index 78d0a00..8ec38b0 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -42,18 +42,15 @@ void cmark_strbuf_grow(cmark_strbuf *buf, bufsize_t target_size) return; if (buf->asize == 0) { - new_size = target_size; new_ptr = NULL; } else { - new_size = buf->asize; new_ptr = buf->ptr; } - /* grow the buffer size by 1.5, until it's big enough - * to fit our target size */ - while (new_size < target_size) - // TODO: Check for overflow. - new_size = (new_size << 1) - (new_size >> 1); + /* Oversize the buffer by 50% to guarantee amortized linear time + * complexity on append operations. */ + // TODO: Check for overflow. + new_size = target_size + target_size / 2; /* round allocation up to multiple of 8 */ // TODO: Check for overflow. |