pack-bitmap.c: avoid redundant calls to try_partial_reuse

try_partial_reuse() is used to mark any bits in the beginning of a
bitmap whose objects can be reused verbatim from the pack they came
from.

Currently this function returns void, and signals nothing to the caller
when bits could not be reused. But multi-pack bitmaps would benefit from
having such a signal, because they may try to pass objects which are in
bounds, but from a pack other than the preferred one.

Any extra calls are noops because of a conditional in
reuse_partial_packfile_from_bitmap(), but those loop iterations can be
avoided by letting try_partial_reuse() indicate when it can't accept any
more bits for reuse, and then listening to that signal.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Taylor Blau <me@ttaylorr.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
maint
Taylor Blau 2021-08-31 16:52:19 -04:00 committed by Junio C Hamano
parent 711260fd60
commit a5f9f24aa0
1 changed files with 29 additions and 11 deletions

View File

@ -1140,22 +1140,26 @@ cleanup:
return NULL; return NULL;
} }


static void try_partial_reuse(struct bitmap_index *bitmap_git, /*
size_t pos, * -1 means "stop trying further objects"; 0 means we may or may not have
struct bitmap *reuse, * reused, but you can keep feeding bits.
struct pack_window **w_curs) */
static int try_partial_reuse(struct bitmap_index *bitmap_git,
size_t pos,
struct bitmap *reuse,
struct pack_window **w_curs)
{ {
off_t offset, header; off_t offset, header;
enum object_type type; enum object_type type;
unsigned long size; unsigned long size;


if (pos >= bitmap_num_objects(bitmap_git)) if (pos >= bitmap_num_objects(bitmap_git))
return; /* not actually in the pack or MIDX */ return -1; /* not actually in the pack or MIDX */


offset = header = pack_pos_to_offset(bitmap_git->pack, pos); offset = header = pack_pos_to_offset(bitmap_git->pack, pos);
type = unpack_object_header(bitmap_git->pack, w_curs, &offset, &size); type = unpack_object_header(bitmap_git->pack, w_curs, &offset, &size);
if (type < 0) if (type < 0)
return; /* broken packfile, punt */ return -1; /* broken packfile, punt */


if (type == OBJ_REF_DELTA || type == OBJ_OFS_DELTA) { if (type == OBJ_REF_DELTA || type == OBJ_OFS_DELTA) {
off_t base_offset; off_t base_offset;
@ -1172,9 +1176,9 @@ static void try_partial_reuse(struct bitmap_index *bitmap_git,
base_offset = get_delta_base(bitmap_git->pack, w_curs, base_offset = get_delta_base(bitmap_git->pack, w_curs,
&offset, type, header); &offset, type, header);
if (!base_offset) if (!base_offset)
return; return 0;
if (offset_to_pack_pos(bitmap_git->pack, base_offset, &base_pos) < 0) if (offset_to_pack_pos(bitmap_git->pack, base_offset, &base_pos) < 0)
return; return 0;


/* /*
* We assume delta dependencies always point backwards. This * We assume delta dependencies always point backwards. This
@ -1186,7 +1190,7 @@ static void try_partial_reuse(struct bitmap_index *bitmap_git,
* odd parameters. * odd parameters.
*/ */
if (base_pos >= pos) if (base_pos >= pos)
return; return 0;


/* /*
* And finally, if we're not sending the base as part of our * And finally, if we're not sending the base as part of our
@ -1197,13 +1201,14 @@ static void try_partial_reuse(struct bitmap_index *bitmap_git,
* object_entry code path handle it. * object_entry code path handle it.
*/ */
if (!bitmap_get(reuse, base_pos)) if (!bitmap_get(reuse, base_pos))
return; return 0;
} }


/* /*
* If we got here, then the object is OK to reuse. Mark it. * If we got here, then the object is OK to reuse. Mark it.
*/ */
bitmap_set(reuse, pos); bitmap_set(reuse, pos);
return 0;
} }


int reuse_partial_packfile_from_bitmap(struct bitmap_index *bitmap_git, int reuse_partial_packfile_from_bitmap(struct bitmap_index *bitmap_git,
@ -1239,10 +1244,23 @@ int reuse_partial_packfile_from_bitmap(struct bitmap_index *bitmap_git,
break; break;


offset += ewah_bit_ctz64(word >> offset); offset += ewah_bit_ctz64(word >> offset);
try_partial_reuse(bitmap_git, pos + offset, reuse, &w_curs); if (try_partial_reuse(bitmap_git, pos + offset, reuse,
&w_curs) < 0) {
/*
* try_partial_reuse indicated we couldn't reuse
* any bits, so there is no point in trying more
* bits in the current word, or any other words
* in result.
*
* Jump out of both loops to avoid future
* unnecessary calls to try_partial_reuse.
*/
goto done;
}
} }
} }


done:
unuse_pack(&w_curs); unuse_pack(&w_curs);


*entries = bitmap_popcount(reuse); *entries = bitmap_popcount(reuse);