Skip to content

Commit

Permalink
peel_ref: check object type before loading
Browse files Browse the repository at this point in the history
The point of peel_ref is to dereference tags; if the base
object is not a tag, then we can return early without even
loading the object into memory.

This patch accomplishes that by checking sha1_object_info
for the type. For a packed object, we can get away with just
looking in the pack index. For a loose object, we only need
to inflate the first couple of header bytes.

This is a bit of a gamble; if we do find a tag object, then
we will end up loading the content anyway, and the extra
lookup will have been wasteful. However, if it is not a tag
object, then we save loading the object entirely. Depending
on the ratio of non-tags to tags in the input, this can be a
minor win or minor loss.

However, it does give us one potential major win: if a ref
points to a large blob (e.g., via an unannotated tag), then
we can avoid looking at it entirely.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Jeff King authored and Junio C Hamano committed Oct 5, 2012
1 parent e6dbffa commit 6c4a060
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions refs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1225,8 +1225,15 @@ int peel_ref(const char *refname, unsigned char *sha1)
}

fallback:
o = parse_object(base);
if (o && o->type == OBJ_TAG) {
o = lookup_unknown_object(base);
if (o->type == OBJ_NONE) {
int type = sha1_object_info(base, NULL);
if (type < 0)
return -1;
o->type = type;
}

if (o->type == OBJ_TAG) {
o = deref_tag_noverify(o);
if (o) {
hashcpy(sha1, o->sha1);
Expand Down

0 comments on commit 6c4a060

Please sign in to comment.