From b2e4a7e21ce7ee8fee857ee71d51ad1ec13feb2e Mon Sep 17 00:00:00 2001 From: Gerald Combs Date: Thu, 21 Jul 2016 17:34:56 -0700 Subject: Minimize allocations for frame tvbuffs and Buffers. Try to minimize the number of times we allocate memory for Buffers and Buffer data. Change-Id: I738fdc64e571772ef4ba6335d49087277dd7b430 Reviewed-on: https://code.wireshark.org/review/16577 Reviewed-by: Gerald Combs Petri-Dish: Gerald Combs Tested-by: Petri Dish Buildbot Reviewed-by: Anders Broman --- frame_tvbuff.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'frame_tvbuff.c') diff --git a/frame_tvbuff.c b/frame_tvbuff.c index 311653ce1a..9af5d2ff4d 100644 --- a/frame_tvbuff.c +++ b/frame_tvbuff.c @@ -69,6 +69,8 @@ frame_read(struct tvb_frame *frame_tvb, struct wtap_pkthdr *phdr, Buffer *buf) return TRUE; } +static GPtrArray *buffer_cache = NULL; + static void frame_cache(struct tvb_frame *frame_tvb) { @@ -77,9 +79,14 @@ frame_cache(struct tvb_frame *frame_tvb) wtap_phdr_init(&phdr); if (frame_tvb->buf == NULL) { - frame_tvb->buf = (struct Buffer *) g_malloc(sizeof(struct Buffer)); + if G_UNLIKELY(!buffer_cache) buffer_cache = g_ptr_array_sized_new(1024); + + if (buffer_cache->len > 0) { + frame_tvb->buf = (struct Buffer *) g_ptr_array_remove_index(buffer_cache, buffer_cache->len - 1); + } else { + frame_tvb->buf = (struct Buffer *) g_malloc(sizeof(struct Buffer)); + } - /* XXX, register frame_tvb to some list which frees from time to time not used buffers :] */ ws_buffer_init(frame_tvb->buf, frame_tvb->tvb.length + frame_tvb->offset); if (!frame_read(frame_tvb, &phdr, frame_tvb->buf)) @@ -98,8 +105,7 @@ frame_free(tvbuff_t *tvb) if (frame_tvb->buf) { ws_buffer_free(frame_tvb->buf); - - g_free(frame_tvb->buf); + g_ptr_array_add(buffer_cache, frame_tvb->buf); } } -- cgit v1.2.1