commit 4f34e29: [Fix] Fix ordering when sorting symcache

Vsevolod Stakhov vsevolod at rspamd.com
Sat May 14 19:21:06 UTC 2022


Author: Vsevolod Stakhov
Date: 2022-05-14 20:15:48 +0100
URL: https://github.com/rspamd/rspamd/commit/4f34e29a44b66e38d1fd1b7a84af2036ad95145e (HEAD -> master)

[Fix] Fix ordering when sorting symcache

---
 src/libserver/symcache/symcache_impl.cxx | 69 +++++++++++---------------------
 1 file changed, 23 insertions(+), 46 deletions(-)

diff --git a/src/libserver/symcache/symcache_impl.cxx b/src/libserver/symcache/symcache_impl.cxx
index fb3d2d965..479617c4a 100644
--- a/src/libserver/symcache/symcache_impl.cxx
+++ b/src/libserver/symcache/symcache_impl.cxx
@@ -547,52 +547,29 @@ auto symcache::resort() -> void
 	};
 
 	auto cache_order_cmp = [&](const auto &it1, const auto &it2) -> auto {
-		auto o1 = tsort_unmask(it1.get()), o2 = tsort_unmask(it2.get());
-		double w1 = 0., w2 = 0.;
-
-		if (o1 == o2) {
-			/* No topological order */
-			if (it1->priority == it2->priority) {
-
-				auto augmentations1 = it1->get_augmentation_weight();
-				auto augmentations2 = it2->get_augmentation_weight();
-
-				if (augmentations1 == augmentations2) {
-					auto avg_freq = ((double) total_hits / used_items);
-					auto avg_weight = (total_weight / used_items);
-					auto f1 = (double) it1->st->total_hits / avg_freq;
-					auto f2 = (double) it2->st->total_hits / avg_freq;
-					auto weight1 = std::fabs(it1->st->weight) / avg_weight;
-					auto weight2 = std::fabs(it2->st->weight) / avg_weight;
-					auto t1 = it1->st->avg_time;
-					auto t2 = it2->st->avg_time;
-					w1 = score_functor(weight1, f1, t1);
-					w2 = score_functor(weight2, f2, t2);
-				}
-				else {
-					w1 = augmentations1;
-					w2 = augmentations2;
-				}
-			}
-			else {
-				/* Strict sorting */
-				w1 = std::abs(it1->priority);
-				w2 = std::abs(it2->priority);
-			}
-		}
-		else {
-			w1 = o1;
-			w2 = o2;
-		}
-
-		if (w2 > w1) {
-			return 1;
-		}
-		else if (w2 < w1) {
-			return -1;
-		}
-
-		return 0;
+		constexpr const auto topology_mult = 1e7,
+				priority_mult = 1e6,
+				augmentations1_mult = 1e5;
+		auto w1 = tsort_unmask(it1.get()) * topology_mult,
+			w2 = tsort_unmask(it2.get()) * topology_mult;
+
+		w1 += it1->priority * priority_mult;
+		w2 += it2->priority * priority_mult;
+		w1 += it1->get_augmentation_weight() * augmentations1_mult;
+		w2 += it2->get_augmentation_weight() * augmentations1_mult;
+
+		auto avg_freq = ((double) total_hits / used_items);
+		auto avg_weight = (total_weight / used_items);
+		auto f1 = (double) it1->st->total_hits / avg_freq;
+		auto f2 = (double) it2->st->total_hits / avg_freq;
+		auto weight1 = std::fabs(it1->st->weight) / avg_weight;
+		auto weight2 = std::fabs(it2->st->weight) / avg_weight;
+		auto t1 = it1->st->avg_time;
+		auto t2 = it2->st->avg_time;
+		w1 += score_functor(weight1, f1, t1);
+		w2 += score_functor(weight2, f2, t2);
+
+		return w1 > w2;
 	};
 
 	std::stable_sort(std::begin(ord->d), std::end(ord->d), cache_order_cmp);


More information about the Commits mailing list