From eb21ac9ea0d7c06f0915ed878258750f7be8fc9d Mon Sep 17 00:00:00 2001 From: dmiller Date: Wed, 26 Jun 2024 21:54:29 +0000 Subject: [PATCH] Refactor HSS::nextTimeout for efficiency Avoid function call in macro expansion. Reduce number of struct timeval assignments. --- scan_engine.cc | 38 +++++++++++++++----------------------- scan_engine.h | 8 ++++---- 2 files changed, 19 insertions(+), 27 deletions(-) diff --git a/scan_engine.cc b/scan_engine.cc index 9831c0d0a..328b2bb5e 100644 --- a/scan_engine.cc +++ b/scan_engine.cc @@ -633,15 +633,11 @@ bool HostScanStats::sendOK(struct timeval *when) const { return false; } -/* If there are pending probe timeouts, fills in when with the time of - the earliest one and returns true. Otherwise returns false and - puts now in when. */ -bool HostScanStats::nextTimeout(struct timeval *when) const { - struct timeval earliest_to = USI->now; +/* If there are pending probe timeouts, compares the earliest one with `when`; + if it is earlier than `when`, replaces `when` with the time of + the earliest one and returns true. Otherwise returns false. */ +bool HostScanStats::soonerTimeout(struct timeval *when) const { std::list::const_iterator probeI, endI; - bool pending_probes = false; - - assert(when); /* For any given invocation, the probe timeout is the same for all probes, so * we can get the earliest-sent probe and then add the timeout to that. @@ -650,22 +646,21 @@ bool HostScanStats::nextTimeout(struct timeval *when) const { probeI != endI; probeI++) { UltraProbe *probe = *probeI; if (!probe->timedout) { - pending_probes = true; - if (TIMEVAL_BEFORE(probe->sent, earliest_to)) { - earliest_to = probe->sent; - } + unsigned long usec_to = probeTimeout(); + struct timeval our_when; + TIMEVAL_ADD(our_when, probe->sent, usec_to); // probes_outstanding is in order by time sent, so // the first one we find is the earliest. + if (TIMEVAL_BEFORE(our_when, *when)) { + // If ours is earlier, replace when. + *when = our_when; + return true; + } + // regardless, there are no earlier probes, so stop looking. break; } } - if (pending_probes) { - TIMEVAL_ADD(*when, earliest_to, probeTimeout()); - } - else { - *when = USI->now; - } - return pending_probes; + return false; } /* gives the maximum try number (try numbers start at zero and @@ -1060,10 +1055,7 @@ bool UltraScanInfo::sendOK(struct timeval *when) const { // or probe timeout. for (host = incompleteHosts.begin(); host != incompleteHosts.end(); host++) { - if ((*host)->nextTimeout(&tmptv)) { - if (TIMEVAL_BEFORE(tmptv, lowhtime)) - lowhtime = tmptv; - } + (*host)->soonerTimeout(&lowhtime); } *when = lowhtime; } diff --git a/scan_engine.h b/scan_engine.h index 4a8b66e60..d66f35979 100644 --- a/scan_engine.h +++ b/scan_engine.h @@ -391,10 +391,10 @@ public: true. */ bool sendOK(struct timeval *when) const; - /* If there are pending probe timeouts, fills in when with the time of - the earliest one and returns true. Otherwise returns false and - puts now in when. */ - bool nextTimeout(struct timeval *when) const; + /* If there are pending probe timeouts, compares the earliest one with `when`; + if it is earlier than `when`, replaces `when` with the time of + the earliest one and returns true. Otherwise returns false. */ + bool soonerTimeout(struct timeval *when) const; UltraScanInfo *USI; /* The USI which contains this HSS */ /* Removes a probe from probes_outstanding, adjusts HSS and USS