git object hash cleanups
This IMNSHO cleans up the object hashing. The hash expansion is separated out into a function of its own, the hash array (and size) names are made more obvious, and the code is generally made to look a bit more like the object-ref hashing. It also gets rid of "find_object()" returning an index (or negative position if no object is found), since that is made redundant by the simplified object rehashing. The basic operation is now "lookup_object()" which just returns the object itself. There's an almost unmeasurable speed increase, but more importantly, I think the end result is more readable. Signed-off-by: Linus Torvalds <torvalds@osdl.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
Linus Torvalds committed
Jun 30, 2006 at 11:20 UTC
0556a11a0df6b4119e01aa77dfb795561e62eb34
1 file changed
+53
-44
object.c
+53
-44
@@ -5,88 +5,97 @@
5
#include "commit.h"
6
#include "tag.h"
7
8
-static struct object **objs;
9
-static int nr_objs, obj_allocs;
8
+static struct object **obj_hash;
9
+static int nr_objs, obj_hash_size;
10
11
unsigned int get_max_object_index(void)
12
{
13
- return obj_allocs;
13
+ return obj_hash_size;
14
}
15
16
struct object *get_indexed_object(unsigned int idx)
17
{
18
- return objs[idx];
18
+ return obj_hash[idx];
19
}
20
21
const char *type_names[] = {
22
"none", "blob", "tree", "commit", "bad"
23
};
24
25
+static unsigned int hash_obj(struct object *obj, unsigned int n)
26
+{
27
+ unsigned int hash = *(unsigned int *)obj->sha1;
28
+ return hash % n;
29
+}
30
+
31
+static void insert_obj_hash(struct object *obj, struct object **hash, unsigned int size)
32
+{
33
+ int j = hash_obj(obj, size);
34
+
35
+ while (hash[j]) {
36
+ j++;
37
+ if (j >= size)
38
+ j = 0;
39
+ }
40
+ hash[j] = obj;
41
+}
42
+
43
static int hashtable_index(const unsigned char *sha1)
44
{
45
unsigned int i;
46
memcpy(&i, sha1, sizeof(unsigned int));
29
- return (int)(i % obj_allocs);
47
+ return (int)(i % obj_hash_size);
48
}
49
32
-static int find_object(const unsigned char *sha1)
50
+struct object *lookup_object(const unsigned char *sha1)
51
{
52
int i;
53
+ struct object *obj;
54
36
- if (!objs)
37
- return -1;
55
+ if (!obj_hash)
56
+ return NULL;
57
58
i = hashtable_index(sha1);
40
- while (objs[i]) {
41
- if (memcmp(sha1, objs[i]->sha1, 20) == 0)
42
- return i;
59
+ while ((obj = obj_hash[i]) != NULL) {
60
+ if (!memcmp(sha1, obj->sha1, 20))
61
+ break;
62
i++;
44
- if (i == obj_allocs)
63
+ if (i == obj_hash_size)
64
i = 0;
65
}
47
- return -1 - i;
66
+ return obj;
67
}
68
50
-struct object *lookup_object(const unsigned char *sha1)
69
+static void grow_object_hash(void)
70
{
52
- int pos = find_object(sha1);
53
- if (pos >= 0)
54
- return objs[pos];
55
- return NULL;
71
+ int i;
72
+ int new_hash_size = obj_hash_size < 32 ? 32 : 2 * obj_hash_size;
73
+ struct object **new_hash;
74
+
75
+ new_hash = calloc(new_hash_size, sizeof(struct object *));
76
+ for (i = 0; i < obj_hash_size; i++) {
77
+ struct object *obj = obj_hash[i];
78
+ if (!obj)
79
+ continue;
80
+ insert_obj_hash(obj, new_hash, new_hash_size);
81
+ }
82
+ free(obj_hash);
83
+ obj_hash = new_hash;
84
+ obj_hash_size = new_hash_size;
85
}
86
87
void created_object(const unsigned char *sha1, struct object *obj)
88
{
60
- int pos;
61
-
89
obj->parsed = 0;
63
- memcpy(obj->sha1, sha1, 20);
64
- obj->type = TYPE_NONE;
90
obj->used = 0;
91
+ obj->type = TYPE_NONE;
92
+ obj->flags = 0;
93
+ memcpy(obj->sha1, sha1, 20);
94
67
- if (obj_allocs - 1 <= nr_objs * 2) {
68
- int i, count = obj_allocs;
69
- obj_allocs = (obj_allocs < 32 ? 32 : 2 * obj_allocs);
70
- objs = xrealloc(objs, obj_allocs * sizeof(struct object *));
71
- memset(objs + count, 0, (obj_allocs - count)
72
- * sizeof(struct object *));
73
- for (i = 0; i < obj_allocs; i++)
74
- if (objs[i]) {
75
- int j = find_object(objs[i]->sha1);
76
- if (j != i) {
77
- j = -1 - j;
78
- objs[j] = objs[i];
79
- objs[i] = NULL;
80
- }
81
- }
82
- }
83
-
84
- pos = find_object(sha1);
85
- if (pos >= 0)
86
- die("Inserting %s twice\n", sha1_to_hex(sha1));
87
- pos = -pos-1;
95
+ if (obj_hash_size - 1 <= nr_objs * 2)
96
+ grow_object_hash();
97
89
- objs[pos] = obj;
98
+ insert_obj_hash(obj, obj_hash, obj_hash_size);
99
nr_objs++;
100
}
101