From 92e5c858c246f3a01104f511c23217fd75c272f4 Mon Sep 17 00:00:00 2001 From: Nikias Bassen Date: Mon, 17 Dec 2018 01:50:11 +0100 Subject: xplist: Write base64 directly to output buffer to improve memory usage Now that we grow the output buffer enough before writing XML output we can just write the base64 encoded data directly to the ouput buffer instead of using a heap buffer that will then be copied to the output buffer. This makes writing XML output more memory efficient (and slightly faster). --- src/xplist.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/xplist.c b/src/xplist.c index 2eeccf9..c89b143 100644 --- a/src/xplist.c +++ b/src/xplist.c @@ -290,12 +290,10 @@ static void node_to_xml(node_t* node, bytearray_t **outbuf, uint32_t depth) tagOpen = TRUE; str_buf_append(*outbuf, "\n", 1); if (node_data->length > 0) { - char *buf = malloc(80); uint32_t j = 0; uint32_t indent = (depth > 8) ? 8 : depth; uint32_t maxread = MAX_DATA_BYTES_PER_LINE(indent); size_t count = 0; - size_t b64count = 0; size_t amount = (node_data->length / 3 * 4) + 4 + (((node_data->length / maxread) + 1) * (indent+1)); if ((*outbuf)->len + amount > (*outbuf)->capacity) { str_buf_grow(*outbuf, amount); @@ -305,12 +303,11 @@ static void node_to_xml(node_t* node, bytearray_t **outbuf, uint32_t depth) str_buf_append(*outbuf, "\t", 1); } count = (node_data->length-j < maxread) ? node_data->length-j : maxread; - b64count = base64encode(buf, node_data->buff + j, count); - str_buf_append(*outbuf, buf, b64count); + assert((*outbuf)->len + count < (*outbuf)->capacity); + (*outbuf)->len += base64encode((char*)(*outbuf)->data + (*outbuf)->len, node_data->buff + j, count); str_buf_append(*outbuf, "\n", 1); j+=count; } - free(buf); } for (i = 0; i < depth; i++) { str_buf_append(*outbuf, "\t", 1); -- cgit v1.1-32-gdbae