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
This commit is contained in:
parent
4ec5ba151f
commit
f14c3d85b0
@ -37,6 +37,7 @@ struct Buffer {
|
|||||||
char *name;
|
char *name;
|
||||||
size_t capacity;
|
size_t capacity;
|
||||||
size_t offset;
|
size_t offset;
|
||||||
|
uint64_t avg_size;
|
||||||
uint8_t *buffer;
|
uint8_t *buffer;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -24,6 +24,11 @@
|
|||||||
#define BUFFER_MIN_INIT_SIZE 4096
|
#define BUFFER_MIN_INIT_SIZE 4096
|
||||||
#define BUFFER_MIN_SHRINK_SIZE 65536
|
#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)
|
static size_t buffer_req_size(Buffer *buffer, size_t len)
|
||||||
{
|
{
|
||||||
return MAX(BUFFER_MIN_INIT_SIZE,
|
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);
|
buffer->buffer = g_realloc(buffer->buffer, buffer->capacity);
|
||||||
trace_buffer_resize(buffer->name ?: "unnamed",
|
trace_buffer_resize(buffer->name ?: "unnamed",
|
||||||
old, buffer->capacity);
|
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, ...)
|
void buffer_init(Buffer *buffer, const char *name, ...)
|
||||||
@ -48,16 +58,29 @@ void buffer_init(Buffer *buffer, const char *name, ...)
|
|||||||
va_end(ap);
|
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)
|
void buffer_shrink(Buffer *buffer)
|
||||||
{
|
{
|
||||||
/*
|
size_t new;
|
||||||
* Only shrink in case the used size is *much* smaller than the
|
|
||||||
* capacity, to avoid bumping up & down the buffers all the time.
|
/* Calculate the average size of the buffer as
|
||||||
* realloc() isn't exactly cheap ...
|
* avg_size = avg_size * ( 1 - a ) + required_size * a
|
||||||
*/
|
* where a is 1 / 2 ^ BUFFER_AVG_SIZE_SHIFT. */
|
||||||
if (buffer->offset < (buffer->capacity >> 3) &&
|
buffer->avg_size *= (1 << BUFFER_AVG_SIZE_SHIFT) - 1;
|
||||||
buffer->capacity > BUFFER_MIN_SHRINK_SIZE) {
|
buffer->avg_size >>= BUFFER_AVG_SIZE_SHIFT;
|
||||||
return;
|
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);
|
buffer_adj_size(buffer, 0);
|
||||||
@ -83,6 +106,7 @@ uint8_t *buffer_end(Buffer *buffer)
|
|||||||
void buffer_reset(Buffer *buffer)
|
void buffer_reset(Buffer *buffer)
|
||||||
{
|
{
|
||||||
buffer->offset = 0;
|
buffer->offset = 0;
|
||||||
|
buffer_shrink(buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
void buffer_free(Buffer *buffer)
|
void buffer_free(Buffer *buffer)
|
||||||
@ -107,6 +131,7 @@ void buffer_advance(Buffer *buffer, size_t len)
|
|||||||
memmove(buffer->buffer, buffer->buffer + len,
|
memmove(buffer->buffer, buffer->buffer + len,
|
||||||
(buffer->offset - len));
|
(buffer->offset - len));
|
||||||
buffer->offset -= len;
|
buffer->offset -= len;
|
||||||
|
buffer_shrink(buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
void buffer_move_empty(Buffer *to, Buffer *from)
|
void buffer_move_empty(Buffer *to, Buffer *from)
|
||||||
|
Loading…
Reference in New Issue
Block a user