From 22f25c5395a2efbe6f83f71366bb8399623c1364 Mon Sep 17 00:00:00 2001 From: Eduardo Silva Date: Fri, 15 Mar 2024 16:50:44 -0600 Subject: [PATCH] pack: increase buffer reallocation for higher performance (fix #8492) When converting from msgpack to raw JSON or sds-json, the reallocation strategy is very convervative to keep the memory usage very low, but in most of scenarios this lead to more fragmentation and high CPU usage. This patch changes the memory reallocation with an exponential strategy for better performance. Kudos to @LukasJerabek and @lecaros for helping to troubleshoot and indentify the root cause of this issue. Signed-off-by: Eduardo Silva --- src/flb_pack.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/flb_pack.c b/src/flb_pack.c index 1cacbdf19e3..05754cfe72d 100644 --- a/src/flb_pack.c +++ b/src/flb_pack.c @@ -807,10 +807,11 @@ flb_sds_t flb_msgpack_raw_to_json_sds(const void *in_buf, size_t in_size) while (1) { ret = flb_msgpack_to_json(out_buf, out_size, root); if (ret <= 0) { + realloc_size *= 2; tmp_buf = flb_sds_increase(out_buf, realloc_size); if (tmp_buf) { out_buf = tmp_buf; - out_size += realloc_size; + out_size *= realloc_size; } else { flb_errno(); @@ -1168,7 +1169,7 @@ char *flb_msgpack_to_json_str(size_t size, const msgpack_object *obj) ret = flb_msgpack_to_json(buf, size, obj); if (ret <= 0) { /* buffer is small. retry.*/ - size += 128; + size *= 2; tmp = flb_realloc(buf, size); if (tmp) { buf = tmp;