Currently the only caller of peel_ref is show-ref, which is using this function to show the peeled tag information if it is available from an existing packed-refs file. The call happens during the for_each_ref callback function, so we have the proper struct ref_list already on the call stack but it is not easily available to return the peeled information to the caller. We now save the current struct ref_list item before calling back into the callback function so that future calls to peel_ref from within the callback function can quickly access the current ref. Doing so will save us an lstat() per ref processed as we no longer have to check the filesystem to see if the ref exists as a loose file or is packed. This current ref caching also saves a linear scan of the cached packed refs list. As a micro-optimization we test the address of the passed ref name against the current_ref->name before we go into the much more costly strcmp(). Nearly any caller of peel_ref will be passing us the same string do_for_each_ref passed them, which is current_ref->name. Signed-off-by: Shawn O. Pearce <spearce@xxxxxxxxxxx> --- refs.c | 25 ++++++++++++++++++++----- 1 files changed, 20 insertions(+), 5 deletions(-) diff --git a/refs.c b/refs.c index fb33da1..c979fb1 100644 --- a/refs.c +++ b/refs.c @@ -157,6 +157,7 @@ static struct cached_refs { struct ref_list *loose; struct ref_list *packed; } cached_refs; +static struct ref_list *current_ref; static void free_ref_list(struct ref_list *list) { @@ -476,6 +477,7 @@ static int do_one_ref(const char *base, each_ref_fn fn, int trim, error("%s does not point to a valid object!", entry->name); return 0; } + current_ref = entry; return fn(entry->name + trim, entry->sha1, entry->flag, cb_data); } @@ -485,6 +487,16 @@ int peel_ref(const char *ref, unsigned char *sha1) unsigned char base[20]; struct object *o; + if (current_ref && (current_ref->name == ref + || !strcmp(current_ref->name, ref))) { + if (current_ref->flag & REF_KNOWS_PEELED) { + hashcpy(sha1, current_ref->peeled); + return 0; + } + hashcpy(base, current_ref->sha1); + goto fallback; + } + if (!resolve_ref(ref, base, 1, &flag)) return -1; @@ -504,7 +516,7 @@ int peel_ref(const char *ref, unsigned char *sha1) } } - /* fallback - callers should not call this for unpacked refs */ +fallback: o = parse_object(base); if (o && o->type == OBJ_TAG) { o = deref_tag(o, ref, 0); @@ -519,7 +531,7 @@ int peel_ref(const char *ref, unsigned char *sha1) static int do_for_each_ref(const char *base, each_ref_fn fn, int trim, void *cb_data) { - int retval; + int retval = 0; struct ref_list *packed = get_packed_refs(); struct ref_list *loose = get_loose_refs(); @@ -539,15 +551,18 @@ static int do_for_each_ref(const char *base, each_ref_fn fn, int trim, } retval = do_one_ref(base, fn, trim, cb_data, entry); if (retval) - return retval; + goto end_each; } for (packed = packed ? packed : loose; packed; packed = packed->next) { retval = do_one_ref(base, fn, trim, cb_data, packed); if (retval) - return retval; + goto end_each; } - return 0; + +end_each: + current_ref = NULL; + return retval; } int head_ref(each_ref_fn fn, void *cb_data) -- 1.5.4.3.295.g6b554 - To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html