[master] f29d2049b Simplify HSH_Lookup 1/N

Poul-Henning Kamp phk at FreeBSD.org
Tue Dec 4 23:47:08 UTC 2018


commit f29d2049b8fca02b714e27953069ccb074220cd1
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Tue Dec 4 21:54:16 2018 +0000

    Simplify HSH_Lookup 1/N
    
    Move successful loop termination out of the loop.
    
    Inspired by:    #2856

diff --git a/bin/varnishd/cache/cache_hash.c b/bin/varnishd/cache/cache_hash.c
index 5d68a1392..55d1a8de9 100644
--- a/bin/varnishd/cache/cache_hash.c
+++ b/bin/varnishd/cache/cache_hash.c
@@ -439,51 +439,11 @@ HSH_Lookup(struct req *req, struct objcore **ocp, struct objcore **bocp,
 		}
 
 		if (EXP_Ttl(req, oc) > req->t_req) {
-			/* If still valid, use it */
 			assert(oh->refcnt > 1);
 			assert(oc->objhead == oh);
-			if (oc->flags & OC_F_HFP) {
-				xid = ObjGetXID(wrk, oc);
-				dttl = EXP_Dttl(req, oc);
-				oc = NULL;
-				retval = HSH_HITPASS;
-			} else if (oc->flags & OC_F_HFM) {
-				xid = ObjGetXID(wrk, oc);
-				dttl = EXP_Dttl(req, oc);
-				*bocp = hsh_insert_busyobj(wrk, oh);
-				oc->refcnt++;
-				retval = HSH_HITMISS;
-			} else {
-				oc->refcnt++;
-				if (oc->hits < LONG_MAX)
-					oc->hits++;
-				retval = HSH_HIT;
-			}
-			*ocp = oc;
-			if (*bocp == NULL)
-				AN(hsh_deref_objhead_unlock(wrk, &oh));
-			else
-				Lck_Unlock(&oh->mtx);
-
-			switch (retval) {
-			case HSH_HITPASS:
-				wrk->stats->cache_hitpass++;
-				VSLb(req->vsl, SLT_HitPass, "%u %.6f",
-				     xid, dttl);
-				break;
-			case HSH_HITMISS:
-				wrk->stats->cache_hitmiss++;
-				VSLb(req->vsl, SLT_HitMiss, "%u %.6f",
-				     xid, dttl);
-				break;
-			case HSH_HIT:
-				break;
-			default:
-				INCOMPL();
-			}
-
-			return (retval);
+			break;
 		}
+
 		if (EXP_Ttl(NULL, oc) < req->t_req && /* ignore req.ttl */
 		    oc->t_origin > exp_t_origin) {
 			/* record the newest object */
@@ -492,6 +452,48 @@ HSH_Lookup(struct req *req, struct objcore **ocp, struct objcore **bocp,
 		}
 	}
 
+	if (oc != NULL) {
+		if (oc->flags & OC_F_HFP) {
+			xid = ObjGetXID(wrk, oc);
+			dttl = EXP_Dttl(req, oc);
+			oc = NULL;
+			retval = HSH_HITPASS;
+		} else if (oc->flags & OC_F_HFM) {
+			xid = ObjGetXID(wrk, oc);
+			dttl = EXP_Dttl(req, oc);
+			*bocp = hsh_insert_busyobj(wrk, oh);
+			oc->refcnt++;
+			retval = HSH_HITMISS;
+		} else {
+			oc->refcnt++;
+			if (oc->hits < LONG_MAX)
+				oc->hits++;
+			retval = HSH_HIT;
+		}
+		*ocp = oc;
+		if (*bocp == NULL)
+			AN(hsh_deref_objhead_unlock(wrk, &oh));
+		else
+			Lck_Unlock(&oh->mtx);
+
+		switch (retval) {
+		case HSH_HITPASS:
+			wrk->stats->cache_hitpass++;
+			VSLb(req->vsl, SLT_HitPass, "%u %.6f", xid, dttl);
+			break;
+		case HSH_HITMISS:
+			wrk->stats->cache_hitmiss++;
+			VSLb(req->vsl, SLT_HitMiss, "%u %.6f", xid, dttl);
+			break;
+		case HSH_HIT:
+			break;
+		default:
+			INCOMPL();
+		}
+
+		return (retval);
+	}
+
 	if (exp_oc != NULL && exp_oc->flags & OC_F_HFM) {
 		/*
 		 * expired HFM ("grace/keep HFM")


More information about the varnish-commit mailing list