Merge branch 'np/lookup-object-hashing'
Micro optimize hash function used in the object hash table. * np/lookup-object-hashing: lookup_object: remove hashtable_index() and optimize hash_obj()
This commit is contained in:
22
object.c
22
object.c
@ -43,16 +43,17 @@ int type_from_string(const char *str)
|
|||||||
die("invalid object type \"%s\"", str);
|
die("invalid object type \"%s\"", str);
|
||||||
}
|
}
|
||||||
|
|
||||||
static unsigned int hash_obj(struct object *obj, unsigned int n)
|
static unsigned int hash_obj(const unsigned char *sha1, unsigned int n)
|
||||||
{
|
{
|
||||||
unsigned int hash;
|
unsigned int hash;
|
||||||
memcpy(&hash, obj->sha1, sizeof(unsigned int));
|
memcpy(&hash, sha1, sizeof(unsigned int));
|
||||||
return hash % n;
|
/* Assumes power-of-2 hash sizes in grow_object_hash */
|
||||||
|
return hash & (n - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void insert_obj_hash(struct object *obj, struct object **hash, unsigned int size)
|
static void insert_obj_hash(struct object *obj, struct object **hash, unsigned int size)
|
||||||
{
|
{
|
||||||
unsigned int j = hash_obj(obj, size);
|
unsigned int j = hash_obj(obj->sha1, size);
|
||||||
|
|
||||||
while (hash[j]) {
|
while (hash[j]) {
|
||||||
j++;
|
j++;
|
||||||
@ -62,13 +63,6 @@ static void insert_obj_hash(struct object *obj, struct object **hash, unsigned i
|
|||||||
hash[j] = obj;
|
hash[j] = obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
static unsigned int hashtable_index(const unsigned char *sha1)
|
|
||||||
{
|
|
||||||
unsigned int i;
|
|
||||||
memcpy(&i, sha1, sizeof(unsigned int));
|
|
||||||
return i % obj_hash_size;
|
|
||||||
}
|
|
||||||
|
|
||||||
struct object *lookup_object(const unsigned char *sha1)
|
struct object *lookup_object(const unsigned char *sha1)
|
||||||
{
|
{
|
||||||
unsigned int i, first;
|
unsigned int i, first;
|
||||||
@ -77,7 +71,7 @@ struct object *lookup_object(const unsigned char *sha1)
|
|||||||
if (!obj_hash)
|
if (!obj_hash)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
first = i = hashtable_index(sha1);
|
first = i = hash_obj(sha1, obj_hash_size);
|
||||||
while ((obj = obj_hash[i]) != NULL) {
|
while ((obj = obj_hash[i]) != NULL) {
|
||||||
if (!hashcmp(sha1, obj->sha1))
|
if (!hashcmp(sha1, obj->sha1))
|
||||||
break;
|
break;
|
||||||
@ -101,6 +95,10 @@ struct object *lookup_object(const unsigned char *sha1)
|
|||||||
static void grow_object_hash(void)
|
static void grow_object_hash(void)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
/*
|
||||||
|
* Note that this size must always be power-of-2 to match hash_obj
|
||||||
|
* above.
|
||||||
|
*/
|
||||||
int new_hash_size = obj_hash_size < 32 ? 32 : 2 * obj_hash_size;
|
int new_hash_size = obj_hash_size < 32 ? 32 : 2 * obj_hash_size;
|
||||||
struct object **new_hash;
|
struct object **new_hash;
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user