Merge branch 'jk/lookup-object-prefer-latest'
authorJunio C Hamano <gitster@pobox.com>
Wed, 29 May 2013 21:29:58 +0000 (14:29 -0700)
committerJunio C Hamano <gitster@pobox.com>
Wed, 29 May 2013 21:29:59 +0000 (14:29 -0700)
Optimizes object lookup when the object hashtable starts to become
crowded.

* jk/lookup-object-prefer-latest:
lookup_object: prioritize recently found objects

1  2 
object.c
diff --combined object.c
index 20703f52ed24aa227d451bec332ac73cc3d49d3a,3fb1caa29fe4f76b1cf49a08d30a47a143081298..88d0bece8116862681ba9fee3f9e0dfdec7f8b17
+++ b/object.c
@@@ -71,13 -71,13 +71,13 @@@ static unsigned int hashtable_index(con
  
  struct object *lookup_object(const unsigned char *sha1)
  {
-       unsigned int i;
+       unsigned int i, first;
        struct object *obj;
  
        if (!obj_hash)
                return NULL;
  
-       i = hashtable_index(sha1);
+       first = i = hashtable_index(sha1);
        while ((obj = obj_hash[i]) != NULL) {
                if (!hashcmp(sha1, obj->sha1))
                        break;
                if (i == obj_hash_size)
                        i = 0;
        }
+       if (obj && i != first) {
+               /*
+                * Move object to where we started to look for it so
+                * that we do not need to walk the hash table the next
+                * time we look for it.
+                */
+               struct object *tmp = obj_hash[i];
+               obj_hash[i] = obj_hash[first];
+               obj_hash[first] = tmp;
+       }
        return obj;
  }
  
@@@ -185,16 -195,6 +195,16 @@@ struct object *parse_object_buffer(cons
        return obj;
  }
  
 +struct object *parse_object_or_die(const unsigned char *sha1,
 +                                 const char *name)
 +{
 +      struct object *o = parse_object(sha1);
 +      if (o)
 +              return o;
 +
 +      die(_("unable to parse object: %s"), name ? name : sha1_to_hex(sha1));
 +}
 +
  struct object *parse_object(const unsigned char *sha1)
  {
        unsigned long size;