Skip to content

Commit 4818cfc

Browse files
committed
Merge branch 'jk/lookup-object-prefer-latest'
Optimizes object lookup when the object hashtable starts to become crowded. * jk/lookup-object-prefer-latest: lookup_object: prioritize recently found objects
2 parents feffa04 + 9a41448 commit 4818cfc

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

object.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,30 @@ static unsigned int hashtable_index(const unsigned char *sha1)
7171

7272
struct object *lookup_object(const unsigned char *sha1)
7373
{
74-
unsigned int i;
74+
unsigned int i, first;
7575
struct object *obj;
7676

7777
if (!obj_hash)
7878
return NULL;
7979

80-
i = hashtable_index(sha1);
80+
first = i = hashtable_index(sha1);
8181
while ((obj = obj_hash[i]) != NULL) {
8282
if (!hashcmp(sha1, obj->sha1))
8383
break;
8484
i++;
8585
if (i == obj_hash_size)
8686
i = 0;
8787
}
88+
if (obj && i != first) {
89+
/*
90+
* Move object to where we started to look for it so
91+
* that we do not need to walk the hash table the next
92+
* time we look for it.
93+
*/
94+
struct object *tmp = obj_hash[i];
95+
obj_hash[i] = obj_hash[first];
96+
obj_hash[first] = tmp;
97+
}
8898
return obj;
8999
}
90100

0 commit comments

Comments
 (0)