aboutsummaryrefslogtreecommitdiff
path: root/util
diff options
context:
space:
mode:
authorPeter Lieven <pl@kamp.de>2015-10-30 12:10:14 +0100
committerGerd Hoffmann <kraxel@redhat.com>2015-11-05 09:09:58 +0100
commitf14c3d85b003d8614144ae67a26157667c1e1245 (patch)
tree0ffdab65f95a165d8a9000351ac0e22364cf39e8 /util
parent4ec5ba151ff3f2ac8dc44dabd058eca5846654a6 (diff)
buffer: allow a buffer to shrink gracefully
the idea behind this patch is to allow the buffer to shrink, but make this a seldom operation. The buffers average size is measured exponentionally smoothed with am alpha of 1/128. Signed-off-by: Peter Lieven <pl@kamp.de> Signed-off-by: Gerd Hoffmann <kraxel@redhat.com> Reviewed-by: Daniel P. Berrange <berrange@redhat.com> Message-id: 1446203414-4013-20-git-send-email-kraxel@redhat.com
Diffstat (limited to 'util')
-rw-r--r--util/buffer.c41
1 files changed, 33 insertions, 8 deletions
diff --git a/util/buffer.c b/util/buffer.c
index fe5a44e708..8b27c08aac 100644
--- a/util/buffer.c
+++ b/util/buffer.c
@@ -24,6 +24,11 @@
#define BUFFER_MIN_INIT_SIZE 4096
#define BUFFER_MIN_SHRINK_SIZE 65536
+/* define the factor alpha for the expentional smoothing
+ * that is used in the average size calculation. a shift
+ * of 7 results in an alpha of 1/2^7. */
+#define BUFFER_AVG_SIZE_SHIFT 7
+
static size_t buffer_req_size(Buffer *buffer, size_t len)
{
return MAX(BUFFER_MIN_INIT_SIZE,
@@ -37,6 +42,11 @@ static void buffer_adj_size(Buffer *buffer, size_t len)
buffer->buffer = g_realloc(buffer->buffer, buffer->capacity);
trace_buffer_resize(buffer->name ?: "unnamed",
old, buffer->capacity);
+
+ /* make it even harder for the buffer to shrink, reset average size
+ * to currenty capacity if it is larger than the average. */
+ buffer->avg_size = MAX(buffer->avg_size,
+ buffer->capacity << BUFFER_AVG_SIZE_SHIFT);
}
void buffer_init(Buffer *buffer, const char *name, ...)
@@ -48,16 +58,29 @@ void buffer_init(Buffer *buffer, const char *name, ...)
va_end(ap);
}
+static uint64_t buffer_get_avg_size(Buffer *buffer)
+{
+ return buffer->avg_size >> BUFFER_AVG_SIZE_SHIFT;
+}
+
void buffer_shrink(Buffer *buffer)
{
- /*
- * Only shrink in case the used size is *much* smaller than the
- * capacity, to avoid bumping up & down the buffers all the time.
- * realloc() isn't exactly cheap ...
- */
- if (buffer->offset < (buffer->capacity >> 3) &&
- buffer->capacity > BUFFER_MIN_SHRINK_SIZE) {
- return;
+ size_t new;
+
+ /* Calculate the average size of the buffer as
+ * avg_size = avg_size * ( 1 - a ) + required_size * a
+ * where a is 1 / 2 ^ BUFFER_AVG_SIZE_SHIFT. */
+ buffer->avg_size *= (1 << BUFFER_AVG_SIZE_SHIFT) - 1;
+ buffer->avg_size >>= BUFFER_AVG_SIZE_SHIFT;
+ buffer->avg_size += buffer_req_size(buffer, 0);
+
+ /* And then only shrink if the average size of the buffer is much
+ * too big, to avoid bumping up & down the buffers all the time.
+ * realloc() isn't exactly cheap ... */
+ new = buffer_req_size(buffer, buffer_get_avg_size(buffer));
+ if (new < buffer->capacity >> 3 &&
+ new >= BUFFER_MIN_SHRINK_SIZE) {
+ buffer_adj_size(buffer, buffer_get_avg_size(buffer));
}
buffer_adj_size(buffer, 0);
@@ -83,6 +106,7 @@ uint8_t *buffer_end(Buffer *buffer)
void buffer_reset(Buffer *buffer)
{
buffer->offset = 0;
+ buffer_shrink(buffer);
}
void buffer_free(Buffer *buffer)
@@ -107,6 +131,7 @@ void buffer_advance(Buffer *buffer, size_t len)
memmove(buffer->buffer, buffer->buffer + len,
(buffer->offset - len));
buffer->offset -= len;
+ buffer_shrink(buffer);
}
void buffer_move_empty(Buffer *to, Buffer *from)