packed_ref_iterator_begin(): make optimization more general
authorMichael Haggerty <mhagger@alum.mit.edu>
Wed, 24 Jan 2018 11:14:14 +0000 (12:14 +0100)
committerJunio C Hamano <gitster@pobox.com>
Wed, 24 Jan 2018 20:55:26 +0000 (12:55 -0800)
We can return an empty iterator not only if the `packed-refs` file is
missing, but also if it is empty or if there are no references whose
names succeed `prefix`. Optimize away those cases as well by moving
the call to `find_reference_location()` higher in the function and
checking whether the determined start position is the same as
`snapshot->eof`. (This is possible now because the previous commit
made `find_reference_location()` robust against empty snapshots.)

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
refs/packed-backend.c
index fffface4283f4be94012a9f40ad0977044b5030f..c2efe912cc834e36ac85561816e3dd34e217c5b8 100644 (file)
@@ -927,7 +927,12 @@ static struct ref_iterator *packed_ref_iterator_begin(
         */
        snapshot = get_snapshot(refs);
 
-       if (!snapshot->buf)
+       if (prefix && *prefix)
+               start = find_reference_location(snapshot, prefix, 0);
+       else
+               start = snapshot->start;
+
+       if (start == snapshot->eof)
                return empty_ref_iterator_begin();
 
        iter = xcalloc(1, sizeof(*iter));
@@ -937,11 +942,6 @@ static struct ref_iterator *packed_ref_iterator_begin(
        iter->snapshot = snapshot;
        acquire_snapshot(snapshot);
 
-       if (prefix && *prefix)
-               start = find_reference_location(snapshot, prefix, 0);
-       else
-               start = snapshot->start;
-
        iter->pos = start;
        iter->eof = snapshot->eof;
        strbuf_init(&iter->refname_buf, 0);