[PATCH] Avoid unnecessarily inflating and interpreting delta

This teaches packed_delta_info() that it only needs to look at
the type of the base object to figure out both type and size of
a deltified object.  This saves quite a many calls to inflate()
when dealing with a deep delta chain.

Signed-off-by: Junio C Hamano <junkio@cox.net>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
This commit is contained in:
Junio C Hamano
2005-06-30 17:13:07 -07:00
committed by Linus Torvalds
parent d0efc8a71d
commit c62266f37c
2 changed files with 42 additions and 36 deletions

View File

@ -16,7 +16,8 @@ int main(int argc, char **argv)
usage("git-cat-file [-t | -s | tagname] <sha1>"); usage("git-cat-file [-t | -s | tagname] <sha1>");
if (!strcmp("-t", argv[1]) || !strcmp("-s", argv[1])) { if (!strcmp("-t", argv[1]) || !strcmp("-s", argv[1])) {
if (!sha1_object_info(sha1, type, &size)) { if (!sha1_object_info(sha1, type,
argv[1][1] == 's' ? &size : NULL)) {
switch (argv[1][1]) { switch (argv[1][1]) {
case 't': case 't':
printf("%s\n", type); printf("%s\n", type);

View File

@ -624,41 +624,49 @@ static int packed_delta_info(unsigned char *base_sha1,
char *type, char *type,
unsigned long *sizep) unsigned long *sizep)
{ {
const unsigned char *data;
unsigned char delta_head[64];
unsigned long result_size, base_size, verify_base_size;
z_stream stream;
int st;
if (left < 20) if (left < 20)
die("truncated pack file"); die("truncated pack file");
if (sha1_object_info(base_sha1, type, &base_size))
/* We choose to only get the type of the base object and
* ignore potentially corrupt pack file that expects the delta
* based on a base with a wrong size. This saves tons of
* inflate() calls.
*/
if (sha1_object_info(base_sha1, type, NULL))
die("cannot get info for delta-pack base"); die("cannot get info for delta-pack base");
memset(&stream, 0, sizeof(stream)); if (sizep) {
const unsigned char *data;
unsigned char delta_head[64];
unsigned long result_size;
z_stream stream;
int st;
data = stream.next_in = base_sha1 + 20; memset(&stream, 0, sizeof(stream));
stream.avail_in = left - 20;
stream.next_out = delta_head;
stream.avail_out = sizeof(delta_head);
inflateInit(&stream); data = stream.next_in = base_sha1 + 20;
st = inflate(&stream, Z_FINISH); stream.avail_in = left - 20;
inflateEnd(&stream); stream.next_out = delta_head;
if ((st != Z_STREAM_END) && stream.total_out != sizeof(delta_head)) stream.avail_out = sizeof(delta_head);
die("delta data unpack-initial failed");
/* Examine the initial part of the delta to figure out inflateInit(&stream);
* the result size. Verify the base size while we are at it. st = inflate(&stream, Z_FINISH);
*/ inflateEnd(&stream);
data = delta_head; if ((st != Z_STREAM_END) &&
verify_base_size = get_delta_hdr_size(&data); stream.total_out != sizeof(delta_head))
if (verify_base_size != base_size) die("delta data unpack-initial failed");
die("delta base size mismatch");
/* Read the result size */ /* Examine the initial part of the delta to figure out
result_size = get_delta_hdr_size(&data); * the result size.
*sizep = result_size; */
data = delta_head;
get_delta_hdr_size(&data); /* ignore base size */
/* Read the result size */
result_size = get_delta_hdr_size(&data);
*sizep = result_size;
}
return 0; return 0;
} }
@ -726,7 +734,8 @@ static int packed_object_info(struct pack_entry *entry,
default: default:
die("corrupted pack file"); die("corrupted pack file");
} }
*sizep = size; if (sizep)
*sizep = size;
unuse_packed_git(p); unuse_packed_git(p);
return 0; return 0;
} }
@ -915,12 +924,7 @@ int sha1_object_info(const unsigned char *sha1, char *type, unsigned long *sizep
if (!find_pack_entry(sha1, &e)) if (!find_pack_entry(sha1, &e))
return error("unable to find %s", sha1_to_hex(sha1)); return error("unable to find %s", sha1_to_hex(sha1));
if (!packed_object_info(&e, type, sizep)) return packed_object_info(&e, type, sizep);
return 0;
/* sheesh */
map = unpack_entry(&e, type, sizep);
free(map);
return (map == NULL) ? 0 : -1;
} }
if (unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr)) < 0) if (unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr)) < 0)
status = error("unable to unpack %s header", status = error("unable to unpack %s header",
@ -929,7 +933,8 @@ int sha1_object_info(const unsigned char *sha1, char *type, unsigned long *sizep
status = error("unable to parse %s header", sha1_to_hex(sha1)); status = error("unable to parse %s header", sha1_to_hex(sha1));
else { else {
status = 0; status = 0;
*sizep = size; if (sizep)
*sizep = size;
} }
inflateEnd(&stream); inflateEnd(&stream);
munmap(map, mapsize); munmap(map, mapsize);