pack-check: Sort entries by pack offset before unpacking them.
Because of the way objects are sorted in a pack, unpacking them in disk order is much more efficient than random access. Tests on the Wine repository show a gain in pack validation time of about 35%. Signed-off-by: Alexandre Julliard <julliard@winehq.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Alexandre Julliard committed
Jun 3, 2007 at 20:21 UTC
3af51928ab7cefa35048e3a5a9e78a127749d405
1 file changed
+35
-12
pack-check.c
+35
-12
@@ -1,6 +1,23 @@
1
#include "cache.h"
2
#include "pack.h"
3
4
+struct idx_entry
5
+{
6
+ const unsigned char *sha1;
7
+ off_t offset;
8
+};
9
+
10
+static int compare_entries(const void *e1, const void *e2)
11
+{
12
+ const struct idx_entry *entry1 = e1;
13
+ const struct idx_entry *entry2 = e2;
14
+ if (entry1->offset < entry2->offset)
15
+ return -1;
16
+ if (entry1->offset > entry2->offset)
17
+ return 1;
18
+ return 0;
19
+}
20
+
21
static int verify_packfile(struct packed_git *p,
22
struct pack_window **w_curs)
23
{
@@ -11,6 +28,7 @@ static int verify_packfile(struct packed_git *p,
28
off_t offset = 0, pack_sig = p->pack_size - 20;
29
uint32_t nr_objects, i;
30
int err;
31
+ struct idx_entry *entries;
32
33
/* Note that the pack header checks are actually performed by
34
* use_pack when it first opens the pack file. If anything
@@ -41,33 +59,38 @@ static int verify_packfile(struct packed_git *p,
59
* we do not do scan-streaming check on the pack file.
60
*/
61
nr_objects = p->num_objects;
62
+ entries = xmalloc(nr_objects * sizeof(*entries));
63
+ /* first sort entries by pack offset, since unpacking them is more efficient that way */
64
+ for (i = 0; i < nr_objects; i++) {
65
+ entries[i].sha1 = nth_packed_object_sha1(p, i);
66
+ if (!entries[i].sha1)
67
+ die("internal error pack-check nth-packed-object");
68
+ entries[i].offset = find_pack_entry_one(entries[i].sha1, p);
69
+ if (!entries[i].offset)
70
+ die("internal error pack-check find-pack-entry-one");
71
+ }
72
+ qsort(entries, nr_objects, sizeof(*entries), compare_entries);
73
+
74
for (i = 0, err = 0; i < nr_objects; i++) {
45
- const unsigned char *sha1;
75
void *data;
76
enum object_type type;
77
unsigned long size;
49
- off_t offset;
78
51
- sha1 = nth_packed_object_sha1(p, i);
52
- if (!sha1)
53
- die("internal error pack-check nth-packed-object");
54
- offset = find_pack_entry_one(sha1, p);
55
- if (!offset)
56
- die("internal error pack-check find-pack-entry-one");
57
- data = unpack_entry(p, offset, &type, &size);
79
+ data = unpack_entry(p, entries[i].offset, &type, &size);
80
if (!data) {
81
err = error("cannot unpack %s from %s",
60
- sha1_to_hex(sha1), p->pack_name);
82
+ sha1_to_hex(entries[i].sha1), p->pack_name);
83
continue;
84
}
63
- if (check_sha1_signature(sha1, data, size, typename(type))) {
85
+ if (check_sha1_signature(entries[i].sha1, data, size, typename(type))) {
86
err = error("packed %s from %s is corrupt",
65
- sha1_to_hex(sha1), p->pack_name);
87
+ sha1_to_hex(entries[i].sha1), p->pack_name);
88
free(data);
89
continue;
90
}
91
free(data);
92
}
93
+ free(entries);
94
95
return err;
96
}