]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
lib/selection: tweak computation of RTT estimates
authorVladimír Čunát <vladimir.cunat@nic.cz>
Tue, 29 Dec 2020 14:51:50 +0000 (15:51 +0100)
committerVladimír Čunát <vladimir.cunat@nic.cz>
Thu, 31 Dec 2020 14:36:11 +0000 (15:36 +0100)
- fix switched \alpha and \beta from the RFC (no big deal, I think)
- use the same order as in the RFC (perhaps that caused the switch?)
- avoid floating-point arithmetics (it's simple with these formulas)
- simplify the the backoff formula (MINs instead of branches)

lib/selection.c

index 192275535b28df11f15633cf4b59311ee7d73a76..e6c39a259ad2d8c16f446535db25348e5534b4c8 100644 (file)
@@ -134,15 +134,9 @@ static bool no_rtt_info(struct rtt_state s)
 
 static unsigned back_off_timeout(uint32_t to, int pow)
 {
-       if (pow > MAX_BACKOFF) {
-               to *= 1 << MAX_BACKOFF;
-       } else {
-               to *= (1 << pow);
-       }
-       if (to > MAX_TIMEOUT) {
-               to = MAX_TIMEOUT;
-       }
-       return to;
+       pow = MIN(pow, MAX_BACKOFF);
+       to <<= pow;
+       return MIN(to, MAX_TIMEOUT);
 }
 
 /* This is verbatim (minus the default timeout value and minimal variance)
@@ -161,12 +155,10 @@ static struct rtt_state calc_rtt_state(struct rtt_state old, unsigned new_rtt)
                return (struct rtt_state){ new_rtt, new_rtt / 2, 0 };
        }
 
-       struct rtt_state ret;
-
-       ret.srtt = (int32_t)(0.75 * old.srtt + 0.25 * new_rtt);
-       ret.variance = (int32_t)(0.875 * old.variance +
-                                0.125 * abs(old.srtt - (int32_t)new_rtt));
-       ret.consecutive_timeouts = 0;
+       struct rtt_state ret = { 0 };
+       ret.variance = (3 * old.variance + abs(old.srtt - (int32_t)new_rtt)
+                       + 2/*rounding*/) / 4;
+       ret.srtt = (7 * old.srtt + new_rtt + 4/*rounding*/) / 8;
 
        return ret;
 }