From ed9b5fc67693f2e6fdb635e3a475570fab2fe384 Mon Sep 17 00:00:00 2001 From: John Esmet Date: Tue, 21 Jan 2014 13:02:59 -0500 Subject: [PATCH] fixes #140 Pre-size the fifo before appending data during nonleaf serialization. Assume the fifo is about 1kb larger than the read buffer (in practice, the fifo is 5 bytes smaller than the read buffer). --- ft/fifo.cc | 17 ++++++++--------- ft/fifo.h | 2 ++ ft/ft_node-serialize.cc | 1 + 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/ft/fifo.cc b/ft/fifo.cc index 3d55334e5c2..7f6e1778df6 100644 --- a/ft/fifo.cc +++ b/ft/fifo.cc @@ -135,6 +135,11 @@ int toku_fifo_create(FIFO *ptr) { return 0; } +void toku_fifo_resize(FIFO fifo, size_t new_size) { + XREALLOC_N(new_size, fifo->memory); + fifo->memory_size = new_size; +} + void toku_fifo_free(FIFO *ptr) { FIFO fifo = *ptr; if (fifo->memory) toku_free(fifo->memory); @@ -162,16 +167,10 @@ int toku_fifo_enq(FIFO fifo, const void *key, unsigned int keylen, const void *d + xids_get_size(xids) - sizeof(XIDS_S); //Prevent double counting int need_space_total = fifo->memory_used+need_space_here; - if (fifo->memory == NULL) { - fifo->memory_size = next_power_of_two(need_space_total); - XMALLOC_N(fifo->memory_size, fifo->memory); - } - if (need_space_total > fifo->memory_size) { - // Out of memory at the end. + if (fifo->memory == NULL || need_space_total > fifo->memory_size) { + // resize the fifo to the next power of 2 greater than the needed space int next_2 = next_power_of_two(need_space_total); - // resize the fifo - XREALLOC_N(next_2, fifo->memory); - fifo->memory_size = next_2; + toku_fifo_resize(fifo, next_2); } struct fifo_entry *entry = (struct fifo_entry *)(fifo->memory + fifo->memory_used); fifo_entry_set_msg_type(entry, type); diff --git a/ft/fifo.h b/ft/fifo.h index 00caa3d742f..e178ec6de24 100644 --- a/ft/fifo.h +++ b/ft/fifo.h @@ -136,6 +136,8 @@ typedef struct fifo *FIFO; int toku_fifo_create(FIFO *); +void toku_fifo_resize(FIFO fifo, size_t new_size); + void toku_fifo_free(FIFO *); int toku_fifo_n_entries(FIFO); diff --git a/ft/ft_node-serialize.cc b/ft/ft_node-serialize.cc index d25a053992b..8d6ece221fe 100644 --- a/ft/ft_node-serialize.cc +++ b/ft/ft_node-serialize.cc @@ -1055,6 +1055,7 @@ deserialize_child_buffer(NONLEAF_CHILDINFO bnc, struct rbuf *rbuf, XMALLOC_N(n_in_this_buffer, fresh_offsets); XMALLOC_N(n_in_this_buffer, broadcast_offsets); } + toku_fifo_resize(bnc->buffer, rbuf->size + 64); for (int i = 0; i < n_in_this_buffer; i++) { bytevec key; ITEMLEN keylen; bytevec val; ITEMLEN vallen;