From 0ed913df7255ceabb3fdc3f90fcf6695811a03f5 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Sat, 9 Dec 2017 13:36:08 +0000 Subject: [PATCH] [Fix] Optimize rspamd_fstring_t reallocations --- src/libutil/fstring.c | 37 ++++++++----------------------- src/plugins/lua/history_redis.lua | 2 +- 2 files changed, 10 insertions(+), 29 deletions(-) diff --git a/src/libutil/fstring.c b/src/libutil/fstring.c index 8b5cae65b..65f24dccc 100644 --- a/src/libutil/fstring.c +++ b/src/libutil/fstring.c @@ -16,7 +16,11 @@ #include "fstring.h" #include "str_util.h" -static const gsize default_initial_size = 48; +#ifdef WITH_JEMALLOC +#include +#endif + +static const gsize default_initial_size = 16; #define fstravail(s) ((s)->allocated - (s)->len) @@ -110,35 +114,12 @@ inline gsize rspamd_fstring_suggest_size (gsize len, gsize allocated, gsize needed_len) { gsize newlen; - /* Maximum size when we double the size of new string */ - static const gsize max_grow = 1024 * 1024; - - newlen = allocated; - - /* - * Stop exponential grow at some point, since it might be slow for the - * vast majority of cases - */ - if (newlen < max_grow) { - newlen *= 2; - } - else { - newlen += max_grow; - } - /* - * Check for overflow - */ - if (newlen <= len + needed_len) { - newlen = len + needed_len; + newlen = MAX (len + needed_len, 1 + allocated * 3 / 2); - if (newlen < max_grow) { - newlen *= 2; - } - else { - newlen += max_grow; - } - } +#ifdef WITH_JEMALLOC + newlen = nallocx (newlen + sizeof (rspamd_fstring_t), 0); +#endif return newlen; } diff --git a/src/plugins/lua/history_redis.lua b/src/plugins/lua/history_redis.lua index a876b46e7..6e26bc06f 100644 --- a/src/plugins/lua/history_redis.lua +++ b/src/plugins/lua/history_redis.lua @@ -22,7 +22,7 @@ local redis_params local settings = { key_prefix = 'rs_history', -- default key name - nrows = 2000, -- default rows limit + nrows = 200, -- default rows limit compress = true, -- use zstd compression when storing data in redis } -- 2.39.5