]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
lib/nsrep: refactor a piece of code
authorVladimír Čunát <vladimir.cunat@nic.cz>
Thu, 2 May 2019 12:15:23 +0000 (14:15 +0200)
committerVladimír Čunát <vladimir.cunat@nic.cz>
Thu, 30 May 2019 15:15:39 +0000 (17:15 +0200)
This is yet another logically equivalent change after 270d9964.
Now it's written in a way that expresses the original intention more
clearly and without copy&paste or long lines.

It seems easiest to verify by inspecting the meaning of the code
*separartely* for the two cases, based on condition:
cur_addr_score < KR_NS_TIMEOUT

lib/nsrep.c

index c992194f452c5beced7a53bebb4ba88b2bb75a7d..77ef7b802b2ee3647e5eddc6713aa7f2ac40f385 100644 (file)
@@ -137,35 +137,30 @@ static unsigned eval_addr_set(const pack_t *addr_set, struct kr_context *ctx,
                        }
                }
 
+               /* We can't always use favour.  If these conditions held:
+                *
+                * rtt_cache_entry_score[i] < KR_NS_TIMEOUT
+                * rtt_cache_entry_score[i] + favour > KR_NS_TIMEOUT
+                * cur_addr_score < rtt_cache_entry_score[i] + favour
+                *
+                * we would prefer "certainly dead" cur_addr_score
+                * instead of "almost dead but alive" rtt_cache_entry_score[i]
+                */
+               const unsigned cur_favour = cur_addr_score < KR_NS_TIMEOUT ? favour : 0;
                for (size_t i = 0; i < KR_NSREP_MAXADDR; ++i) {
-                       if (cur_addr_score >= KR_NS_TIMEOUT) {
-                               /* We can't use favour here.
-                                * If all of the conditions below are true
-                                *
-                                * rtt_cache_entry_score[i] < KR_NS_TIMEOUT
-                                * rtt_cache_entry_score[i] + favour > KR_NS_TIMEOUT
-                                * cur_addr_score < rtt_cache_entry_score[i] + favour
-                                *
-                                * we will prefer "certainly dead" cur_addr_score
-                                * instead of "almost dead, but alive" rtt_cache_entry_score[i]
-                                */
-                               if (cur_addr_score >= rtt_cache_entry_score[i]) {
-                                       continue;
-                               }
-                       }
-                       if (cur_addr_score >= KR_NS_TIMEOUT
-                           || cur_addr_score < rtt_cache_entry_score[i] + favour) {
-                               /* Shake down previous contenders */
-                               for (size_t j = KR_NSREP_MAXADDR - 1; j > i; --j) {
-                                       addr[j] = addr[j - 1];
-                                       rtt_cache_entry_ptr[j] = rtt_cache_entry_ptr[j - 1];
-                                       rtt_cache_entry_score[j] = rtt_cache_entry_score[j - 1];
-                               }
-                               addr[i] = it;
-                               rtt_cache_entry_score[i] = cur_addr_score;
-                               rtt_cache_entry_ptr[i] = cached;
-                               break;
+                       if (cur_addr_score >= rtt_cache_entry_score[i] + cur_favour)
+                               continue;
+
+                       /* Shake down previous contenders */
+                       for (size_t j = KR_NSREP_MAXADDR - 1; j > i; --j) {
+                               addr[j] = addr[j - 1];
+                               rtt_cache_entry_ptr[j] = rtt_cache_entry_ptr[j - 1];
+                               rtt_cache_entry_score[j] = rtt_cache_entry_score[j - 1];
                        }
+                       addr[i] = it;
+                       rtt_cache_entry_score[i] = cur_addr_score;
+                       rtt_cache_entry_ptr[i] = cached;
+                       break;
                }
        }