pspp-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[patch 3/5] Cache repeated lookups of a single tower element.


From: blp
Subject: [patch 3/5] Cache repeated lookups of a single tower element.
Date: Sun, 03 Jun 2007 15:47:14 -0700
User-agent: quilt/0.45-1

This turns such lookups into O(1) operations without harming the big-O
of other operations.  The datasheet code does such lookups commonly.

Index: merge/src/libpspp/tower.h
===================================================================
--- merge.orig/src/libpspp/tower.h      2007-05-31 22:06:31.000000000 -0700
+++ merge/src/libpspp/tower.h   2007-05-31 22:36:10.000000000 -0700
@@ -74,7 +74,9 @@
 /* A tower. */
 struct tower 
   {
-    struct abt abt;              /* Tree. */
+    struct abt abt;                   /* Tree. */
+    struct tower_node *cache;         /* Cache node. */
+    unsigned long int cache_bottom;   /* Height of cache's bottom. */
   };
 
 void tower_init (struct tower *);
Index: merge/src/libpspp/tower.c
===================================================================
--- merge.orig/src/libpspp/tower.c      2007-05-31 22:06:31.000000000 -0700
+++ merge/src/libpspp/tower.c   2007-05-31 22:36:10.000000000 -0700
@@ -20,5 +20,7 @@
 
 #include <libpspp/tower.h>
 
+#include <limits.h>
+
 #include <libpspp/assertion.h>
 #include <libpspp/compiler.h>
@@ -38,6 +43,7 @@
 tower_init (struct tower *t) 
 {
   abt_init (&t->abt, NULL, reaugment_tower_node, NULL);
+  t->cache_bottom = ULONG_MAX;
 }
 
 /* Returns true if T contains no nodes, false otherwise. */
@@ -64,6 +70,7 @@
   new->height = height;
   abt_insert_before (&t->abt, under ? &under->abt_node : NULL,
                      &new->abt_node);
+  t->cache_bottom = ULONG_MAX;
 }
 
 /* Deletes NODE from tower T. */
@@ -72,6 +79,7 @@
 {
   struct tower_node *next = next_node (t, node);
   abt_delete (&t->abt, &node->abt_node);
+  t->cache_bottom = ULONG_MAX;
   return next;
 }
 
@@ -83,6 +91,7 @@
   assert (new_height > 0);
   node->height = new_height;
   abt_reaugmented (&t->abt, &node->abt_node);
+  t->cache_bottom = ULONG_MAX;
 }
 
 /* Removes nodes FIRST through LAST (exclusive) from tower SRC
@@ -110,6 +119,7 @@
       abt_insert_before (&dst->abt, under ? &under->abt_node : NULL,
                          &first->abt_node);
     }
+  dst->cache_bottom = src->cache_bottom = ULONG_MAX;
 }
 
 /* Returns the node at the given HEIGHT from the bottom of tower
@@ -118,14 +128,21 @@
    of the returned node, which may be less than HEIGHT if HEIGHT
    refers to the middle of a node instead of its bottom. */
 struct tower_node *
-tower_lookup (const struct tower *t,
+tower_lookup (const struct tower *t_,
               unsigned long height,
               unsigned long *node_start) 
 {
+  struct tower *t = (struct tower *) t_;
   struct abt_node *p;
 
   assert (height < tower_height (t));
 
+  if (height >= t->cache_bottom && height - t->cache_bottom < t->cache->height)
+    {
+      *node_start = t->cache_bottom;
+      return t->cache; 
+    }
+
   *node_start = 0;
   p = t->abt.root;
   for (;;)
@@ -147,6 +164,8 @@
           if (height < node_height) 
             {
               /* Our goal height is in P. */
+              t->cache = node;
+              t->cache_bottom = *node_start;
               return node; 
             }
           else 

--





reply via email to

[Prev in Thread] Current Thread [Next in Thread]