r5582 - in trunk/varnish-cache: bin/varnishd include lib/libvarnish

phk at varnish-cache.org phk at varnish-cache.org
Mon Nov 22 10:44:48 CET 2010


Author: phk
Date: 2010-11-22 10:44:48 +0100 (Mon, 22 Nov 2010)
New Revision: 5582

Modified:
   trunk/varnish-cache/bin/varnishd/cache_expire.c
   trunk/varnish-cache/include/binary_heap.h
   trunk/varnish-cache/lib/libvarnish/binary_heap.c
Log:
Add a binheap function to shuffle an item into correct location after
a change of key value.

Contrary to the previous comment in cache_expire.c, this process is
guaranteed to work because the shuffle will always terminate either
in the root position or in the bottom row.

Use this function when we adjust ttl on an object.



Modified: trunk/varnish-cache/bin/varnishd/cache_expire.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/cache_expire.c	2010-11-22 09:34:49 UTC (rev 5581)
+++ trunk/varnish-cache/bin/varnishd/cache_expire.c	2010-11-22 09:44:48 UTC (rev 5582)
@@ -215,16 +215,8 @@
 	 * tending to a timer.  If so, we do not muck with it here.
 	 */
 	if (oc->timer_idx != BINHEAP_NOIDX && update_object_when(o)) {
-		/*
-		 * XXX: this could possibly be optimized by shuffling
-		 * XXX: up or down, but that leaves some very nasty
-		 * XXX: corner cases, such as shuffling all the way
-		 * XXX: down the left half, then back up the right half.
-		 */
 		assert(oc->timer_idx != BINHEAP_NOIDX);
-		binheap_delete(exp_heap, oc->timer_idx);
-		assert(oc->timer_idx == BINHEAP_NOIDX);
-		binheap_insert(exp_heap, oc);
+		binheap_reorder(exp_heap, oc->timer_idx);
 		assert(oc->timer_idx != BINHEAP_NOIDX);
 	}
 	Lck_Unlock(&exp_mtx);

Modified: trunk/varnish-cache/include/binary_heap.h
===================================================================
--- trunk/varnish-cache/include/binary_heap.h	2010-11-22 09:34:49 UTC (rev 5581)
+++ trunk/varnish-cache/include/binary_heap.h	2010-11-22 09:44:48 UTC (rev 5582)
@@ -63,6 +63,11 @@
 	 * Insert an item
 	 */
 
+void binheap_reorder(struct binheap *, unsigned idx);
+	/*
+	 * Move an order after changing its key value.
+	 */
+
 void binheap_delete(struct binheap *, unsigned idx);
 	/*
 	 * Delete an item

Modified: trunk/varnish-cache/lib/libvarnish/binary_heap.c
===================================================================
--- trunk/varnish-cache/lib/libvarnish/binary_heap.c	2010-11-22 09:34:49 UTC (rev 5581)
+++ trunk/varnish-cache/lib/libvarnish/binary_heap.c	2010-11-22 09:44:48 UTC (rev 5582)
@@ -374,7 +374,24 @@
 	}
 }
 
+/*
+ * Move an item up/down after changing its key value
+ */
 
+void
+binheap_reorder(struct binheap *bh, unsigned idx)
+{
+
+	assert(bh != NULL);
+	assert(bh->magic == BINHEAP_MAGIC);
+	assert(bh->next > ROOT_IDX);
+	assert(idx < bh->next);
+	assert(idx > 0);
+	assert(A(bh, idx) != NULL);
+	idx = binheap_trickleup(bh, idx);
+	binheap_trickledown(bh, idx);
+}
+
 #ifdef TEST_DRIVER
 /* Test driver -------------------------------------------------------*/
 #include <stdio.h>




More information about the varnish-commit mailing list