]> source.dussan.org Git - rspamd.git/commitdiff
[Minor] Deduplicate sum function
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Sat, 5 Mar 2022 20:49:26 +0000 (20:49 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Sat, 5 Mar 2022 20:49:26 +0000 (20:49 +0000)
src/lua/lua_tensor.c
src/rspamd.c

index 76756e14836572c0cb6d7f509f93f0f1e82b8d69..b78a17fc888453d058a29dbbd7fd10127115aeac 100644 (file)
@@ -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
index d1eaa205d2fba94a683f67da7d469eb69d2cdafb..610c9aa7bb601a94e411e9b19bda6d15b045a3f9 100644 (file)
@@ -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",