From d753ba5f49a061464c90b46de852944fe5fe3763 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Sat, 5 Mar 2022 20:49:26 +0000 Subject: [PATCH] [Minor] Deduplicate sum function --- src/lua/lua_tensor.c | 17 +++-------------- src/rspamd.c | 16 ++-------------- 2 files changed, 5 insertions(+), 28 deletions(-) diff --git a/src/lua/lua_tensor.c b/src/lua/lua_tensor.c index 76756e148..b78a17fc8 100644 --- a/src/lua/lua_tensor.c +++ b/src/lua/lua_tensor.c @@ -634,21 +634,10 @@ lua_tensor_eigen (lua_State *L) } static inline rspamd_tensor_num_t -mean_vec (rspamd_tensor_num_t *x, int n) +mean_vec (rspamd_tensor_num_t *x, gsize n) { - rspamd_tensor_num_t s = 0; - volatile rspamd_tensor_num_t c = 0; - - /* https://en.wikipedia.org/wiki/Kahan_summation_algorithm */ - for (int i = 0; i < n; i ++) { - rspamd_tensor_num_t v = x[i]; - rspamd_tensor_num_t y = v - c; - rspamd_tensor_num_t t = s + y; - c = (t - s) - y; - s = t; - } - - return s / (rspamd_tensor_num_t)n; + float sum = rspamd_sum_floats (x, &n); + return sum / (rspamd_tensor_num_t)n; } static gint diff --git a/src/rspamd.c b/src/rspamd.c index d1eaa205d..610c9aa7b 100644 --- a/src/rspamd.c +++ b/src/rspamd.c @@ -1107,20 +1107,8 @@ rspamd_stat_update_handler (struct ev_loop *loop, ev_timer *w, int revents) cur_stat.actions_stat[METRIC_ACTION_REWRITE_SUBJECT]; gdouble new_ham = cur_stat.actions_stat[METRIC_ACTION_NOACTION]; - /* Kahan sum */ - float sum = 0.0f; - volatile float c = 0.0f; /* We don't want any optimisations around c */ - int cnt = 0; - - for (int i = 0; i < MAX_AVG_TIME_SLOTS; i ++) { - if (!isnan(cur_stat.avg_time.avg_time[i])) { - cnt ++; - float y = cur_stat.avg_time.avg_time[i] - c; - float t = sum + y; - c = (t - sum) - y; - sum = t; - } - } + gsize cnt = MAX_AVG_TIME_SLOTS; + float sum = rspamd_sum_floats (cur_stat.avg_time.avg_time, &cnt); rspamd_snprintf (proctitle, sizeof (proctitle), "main process; %.1f msg/sec, %.1f msg/sec spam, %.1f msg/sec ham; %.2fs avg processing time", -- 2.39.5