Use a hashtable for objects instead of a sorted list
In a simple test, this brings down the CPU time from 47 sec to 22 sec. Signed-off-by: Johannes Schindelin <Johannes.Schindelin@gmx.de> Signed-off-by: Junio C Hamano <junkio@cox.net>
Johannes Schindelin committed
Feb 12, 2006 at 02:57 UTC
070879ca93a7d358086f4c8aff4553493dcb9210
4 files changed
+49
-34
fsck-objects.c
+4
-1
@@ -61,9 +61,12 @@ static void check_connectivity(void)
61
int i;
62
63
/* Look up all the requirements, warn about missing objects.. */
64
- for (i = 0; i < nr_objs; i++) {
64
+ for (i = 0; i < obj_allocs; i++) {
65
struct object *obj = objs[i];
66
67
+ if (!obj)
68
+ continue;
69
+
70
if (!obj->parsed) {
71
if (!standalone && has_sha1_file(obj->sha1))
72
; /* it is in pack */
name-rev.c
+4
-3
@@ -232,9 +232,10 @@ int main(int argc, char **argv)
232
} else if (all) {
233
int i;
234
235
- for (i = 0; i < nr_objs; i++)
236
- printf("%s %s\n", sha1_to_hex(objs[i]->sha1),
237
- get_rev_name(objs[i]));
235
+ for (i = 0; i < obj_allocs; i++)
236
+ if (objs[i])
237
+ printf("%s %s\n", sha1_to_hex(objs[i]->sha1),
238
+ get_rev_name(objs[i]));
239
} else
240
for ( ; revs; revs = revs->next)
241
printf("%s %s\n", revs->name, get_rev_name(revs->item));
object.c
+40
-29
@@ -6,30 +6,32 @@
6
#include "tag.h"
7
8
struct object **objs;
9
-int nr_objs;
10
-static int obj_allocs;
9
+static int nr_objs;
10
+int obj_allocs;
11
12
int track_object_refs = 1;
13
14
+static int hashtable_index(const unsigned char *sha1)
15
+{
16
+ unsigned int i = *(unsigned int *)sha1;
17
+ return (int)(i % obj_allocs);
18
+}
19
+
20
static int find_object(const unsigned char *sha1)
21
{
16
- int first = 0, last = nr_objs;
17
-
18
- while (first < last) {
19
- int next = (first + last) / 2;
20
- struct object *obj = objs[next];
21
- int cmp;
22
-
23
- cmp = memcmp(sha1, obj->sha1, 20);
24
- if (!cmp)
25
- return next;
26
- if (cmp < 0) {
27
- last = next;
28
- continue;
29
- }
30
- first = next+1;
31
- }
32
- return -first-1;
22
+ int i = hashtable_index(sha1);
23
+
24
+ if (!objs)
25
+ return -1;
26
+
27
+ while (objs[i]) {
28
+ if (memcmp(sha1, objs[i]->sha1, 20) == 0)
29
+ return i;
30
+ i++;
31
+ if (i == obj_allocs)
32
+ i = 0;
33
+ }
34
+ return -1 - i;
35
}
36
37
struct object *lookup_object(const unsigned char *sha1)
@@ -42,7 +44,7 @@ struct object *lookup_object(const unsigned char *sha1)
44
45
void created_object(const unsigned char *sha1, struct object *obj)
46
{
45
- int pos = find_object(sha1);
47
+ int pos;
48
49
obj->parsed = 0;
50
memcpy(obj->sha1, sha1, 20);
@@ -50,18 +52,27 @@ void created_object(const unsigned char *sha1, struct object *obj)
52
obj->refs = NULL;
53
obj->used = 0;
54
53
- if (pos >= 0)
54
- die("Inserting %s twice\n", sha1_to_hex(sha1));
55
- pos = -pos-1;
56
-
57
- if (obj_allocs == nr_objs) {
58
- obj_allocs = alloc_nr(obj_allocs);
55
+ if (obj_allocs - 1 <= nr_objs * 2) {
56
+ int i, count = obj_allocs;
57
+ obj_allocs = (obj_allocs < 32 ? 32 : 2 * obj_allocs);
58
objs = xrealloc(objs, obj_allocs * sizeof(struct object *));
59
+ memset(objs + count, 0, (obj_allocs - count)
60
+ * sizeof(struct object *));
61
+ for (i = 0; i < count; i++)
62
+ if (objs[i]) {
63
+ int j = find_object(objs[i]->sha1);
64
+ if (j != i) {
65
+ j = -1 - j;
66
+ objs[j] = objs[i];
67
+ objs[i] = NULL;
68
+ }
69
+ }
70
}
71
62
- /* Insert it into the right place */
63
- memmove(objs + pos + 1, objs + pos, (nr_objs - pos) *
64
- sizeof(struct object *));
72
+ pos = find_object(sha1);
73
+ if (pos >= 0)
74
+ die("Inserting %s twice\n", sha1_to_hex(sha1));
75
+ pos = -pos-1;
76
77
objs[pos] = obj;
78
nr_objs++;
object.h
+1
-1
@@ -23,7 +23,7 @@ struct object {
23
};
24
25
extern int track_object_refs;
26
-extern int nr_objs;
26
+extern int obj_allocs;
27
extern struct object **objs;
28
29
/** Internal only **/