Skip to content

Commit dab6093

Browse files
ttaylorrgitster
authored andcommitted
pack-bitmap: pass bitmapped_pack struct to pack-reuse functions
When trying to assemble a pack with bitmaps using `--use-bitmap-index`, `pack-objects` asks the pack-bitmap machinery for a bitmap which indicates the set of objects we can "reuse" verbatim from on-disk. This set is roughly comprised of: a prefix of objects in the bitmapped pack (or preferred pack, in the case of a multi-pack reachability bitmap), plus any other objects not included in the prefix, excluding any deltas whose base we are not sending in the resulting pack. The pack-bitmap machinery is responsible for computing this bitmap, and does so with the following functions: - reuse_partial_packfile_from_bitmap() - try_partial_reuse() In the existing implementation, the first function is responsible for (a) marking the prefix of objects in the reusable pack, and then (b) calling try_partial_reuse() on any remaining objects to ensure that they are also reusable (and removing them from the bitmapped set if they are not). Likewise, the `try_partial_reuse()` function is responsible for checking whether an isolated object (that is, an object from the bitmapped pack/preferred pack not contained in the prefix from earlier) may be reused, i.e. that it isn't a delta of an object that we are not sending in the resulting pack. These functions are based on two core assumptions, which we will unwind in this and the following commits: 1. There is only a single pack from the bitmap which is eligible for verbatim pack-reuse. For single-pack bitmaps, this is trivially the bitmapped pack. For multi-pack bitmaps, this is (currently) the MIDX's preferred pack. 2. The pack eligible for reuse has its first object in bit position 0, and all objects from that pack follow in pack-order from that first bit position. In order to perform verbatim pack reuse over multiple packs, we must unwind these two assumptions. Most notably, in order to reuse bits from a given packfile, we need to know the first bit position occupied by an object form that packfile. To propagate this information around, pass a `struct bitmapped_pack *` anywhere we previously passed a `struct packed_git *`, since the former contains the bitmap position we're interested in (as well as a pointer to the latter). As an additional step, factor out a sub-routine from the main `reuse_partial_packfile_from_bitmap()` function, called `reuse_partial_packfile_from_bitmap_1()`. This new function will be responsible for figuring out which objects may be reused from a single pack, and the existing function will dispatch multiple calls to its new helper function for each reusable pack. Consequently, `reuse_partial_packfile_from_bitmap()` will now maintain an array of reusable packs instead of a single such pack. We currently expect that array to have only a single element, so this awkward state is short-lived. It will serve as useful scaffolding in subsequent commits as we begin to work towards enabling multi-pack reuse. Signed-off-by: Taylor Blau <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 307d75b commit dab6093

File tree

1 file changed

+87
-31
lines changed

1 file changed

+87
-31
lines changed

pack-bitmap.c

Lines changed: 87 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1836,7 +1836,7 @@ struct bitmap_index *prepare_bitmap_walk(struct rev_info *revs,
18361836
* -1 means "stop trying further objects"; 0 means we may or may not have
18371837
* reused, but you can keep feeding bits.
18381838
*/
1839-
static int try_partial_reuse(struct packed_git *pack,
1839+
static int try_partial_reuse(struct bitmapped_pack *pack,
18401840
size_t pos,
18411841
struct bitmap *reuse,
18421842
struct pack_window **w_curs)
@@ -1868,11 +1868,11 @@ static int try_partial_reuse(struct packed_git *pack,
18681868
* preferred pack precede all bits from other packs.
18691869
*/
18701870

1871-
if (pos >= pack->num_objects)
1871+
if (pos >= pack->p->num_objects)
18721872
return -1; /* not actually in the pack or MIDX preferred pack */
18731873

1874-
offset = delta_obj_offset = pack_pos_to_offset(pack, pos);
1875-
type = unpack_object_header(pack, w_curs, &offset, &size);
1874+
offset = delta_obj_offset = pack_pos_to_offset(pack->p, pos);
1875+
type = unpack_object_header(pack->p, w_curs, &offset, &size);
18761876
if (type < 0)
18771877
return -1; /* broken packfile, punt */
18781878

@@ -1888,11 +1888,11 @@ static int try_partial_reuse(struct packed_git *pack,
18881888
* and the normal slow path will complain about it in
18891889
* more detail.
18901890
*/
1891-
base_offset = get_delta_base(pack, w_curs, &offset, type,
1891+
base_offset = get_delta_base(pack->p, w_curs, &offset, type,
18921892
delta_obj_offset);
18931893
if (!base_offset)
18941894
return 0;
1895-
if (offset_to_pack_pos(pack, base_offset, &base_pos) < 0)
1895+
if (offset_to_pack_pos(pack->p, base_offset, &base_pos) < 0)
18961896
return 0;
18971897

18981898
/*
@@ -1915,14 +1915,14 @@ static int try_partial_reuse(struct packed_git *pack,
19151915
* to REF_DELTA on the fly. Better to just let the normal
19161916
* object_entry code path handle it.
19171917
*/
1918-
if (!bitmap_get(reuse, base_pos))
1918+
if (!bitmap_get(reuse, pack->bitmap_pos + base_pos))
19191919
return 0;
19201920
}
19211921

19221922
/*
19231923
* If we got here, then the object is OK to reuse. Mark it.
19241924
*/
1925-
bitmap_set(reuse, pos);
1925+
bitmap_set(reuse, pack->bitmap_pos + pos);
19261926
return 0;
19271927
}
19281928

@@ -1934,29 +1934,13 @@ uint32_t midx_preferred_pack(struct bitmap_index *bitmap_git)
19341934
return nth_midxed_pack_int_id(m, pack_pos_to_midx(bitmap_git->midx, 0));
19351935
}
19361936

1937-
int reuse_partial_packfile_from_bitmap(struct bitmap_index *bitmap_git,
1938-
struct packed_git **packfile_out,
1939-
uint32_t *entries,
1940-
struct bitmap **reuse_out)
1937+
static void reuse_partial_packfile_from_bitmap_1(struct bitmap_index *bitmap_git,
1938+
struct bitmapped_pack *pack,
1939+
struct bitmap *reuse)
19411940
{
1942-
struct repository *r = the_repository;
1943-
struct packed_git *pack;
19441941
struct bitmap *result = bitmap_git->result;
1945-
struct bitmap *reuse;
19461942
struct pack_window *w_curs = NULL;
19471943
size_t i = 0;
1948-
uint32_t offset;
1949-
uint32_t objects_nr;
1950-
1951-
assert(result);
1952-
1953-
load_reverse_index(r, bitmap_git);
1954-
1955-
if (bitmap_is_midx(bitmap_git))
1956-
pack = bitmap_git->midx->packs[midx_preferred_pack(bitmap_git)];
1957-
else
1958-
pack = bitmap_git->pack;
1959-
objects_nr = pack->num_objects;
19601944

19611945
while (i < result->word_alloc && result->words[i] == (eword_t)~0)
19621946
i++;
@@ -1969,15 +1953,15 @@ int reuse_partial_packfile_from_bitmap(struct bitmap_index *bitmap_git,
19691953
* we use it instead of another pack. In single-pack bitmaps, the choice
19701954
* is made for us.
19711955
*/
1972-
if (i > objects_nr / BITS_IN_EWORD)
1973-
i = objects_nr / BITS_IN_EWORD;
1956+
if (i > pack->p->num_objects / BITS_IN_EWORD)
1957+
i = pack->p->num_objects / BITS_IN_EWORD;
19741958

1975-
reuse = bitmap_word_alloc(i);
19761959
memset(reuse->words, 0xFF, i * sizeof(eword_t));
19771960

19781961
for (; i < result->word_alloc; ++i) {
19791962
eword_t word = result->words[i];
19801963
size_t pos = (i * BITS_IN_EWORD);
1964+
size_t offset;
19811965

19821966
for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
19831967
if ((word >> offset) == 0)
@@ -2002,6 +1986,78 @@ int reuse_partial_packfile_from_bitmap(struct bitmap_index *bitmap_git,
20021986

20031987
done:
20041988
unuse_pack(&w_curs);
1989+
}
1990+
1991+
static int bitmapped_pack_cmp(const void *va, const void *vb)
1992+
{
1993+
const struct bitmapped_pack *a = va;
1994+
const struct bitmapped_pack *b = vb;
1995+
1996+
if (a->bitmap_pos < b->bitmap_pos)
1997+
return -1;
1998+
if (a->bitmap_pos > b->bitmap_pos)
1999+
return 1;
2000+
return 0;
2001+
}
2002+
2003+
int reuse_partial_packfile_from_bitmap(struct bitmap_index *bitmap_git,
2004+
struct packed_git **packfile_out,
2005+
uint32_t *entries,
2006+
struct bitmap **reuse_out)
2007+
{
2008+
struct repository *r = the_repository;
2009+
struct bitmapped_pack *packs = NULL;
2010+
struct bitmap *result = bitmap_git->result;
2011+
struct bitmap *reuse;
2012+
size_t i;
2013+
size_t packs_nr = 0, packs_alloc = 0;
2014+
size_t word_alloc;
2015+
uint32_t objects_nr = 0;
2016+
2017+
assert(result);
2018+
2019+
load_reverse_index(r, bitmap_git);
2020+
2021+
if (bitmap_is_midx(bitmap_git)) {
2022+
for (i = 0; i < bitmap_git->midx->num_packs; i++) {
2023+
struct bitmapped_pack pack;
2024+
if (nth_bitmapped_pack(r, bitmap_git->midx, &pack, i) < 0) {
2025+
warning(_("unable to load pack: '%s', disabling pack-reuse"),
2026+
bitmap_git->midx->pack_names[i]);
2027+
free(packs);
2028+
return -1;
2029+
}
2030+
if (!pack.bitmap_nr)
2031+
continue; /* no objects from this pack */
2032+
if (pack.bitmap_pos)
2033+
continue; /* not preferred pack */
2034+
2035+
ALLOC_GROW(packs, packs_nr + 1, packs_alloc);
2036+
memcpy(&packs[packs_nr++], &pack, sizeof(pack));
2037+
2038+
objects_nr += pack.p->num_objects;
2039+
}
2040+
2041+
QSORT(packs, packs_nr, bitmapped_pack_cmp);
2042+
} else {
2043+
ALLOC_GROW(packs, packs_nr + 1, packs_alloc);
2044+
2045+
packs[packs_nr].p = bitmap_git->pack;
2046+
packs[packs_nr].bitmap_pos = 0;
2047+
packs[packs_nr].bitmap_nr = bitmap_git->pack->num_objects;
2048+
2049+
objects_nr = packs[packs_nr++].p->num_objects;
2050+
}
2051+
2052+
word_alloc = objects_nr / BITS_IN_EWORD;
2053+
if (objects_nr % BITS_IN_EWORD)
2054+
word_alloc++;
2055+
reuse = bitmap_word_alloc(word_alloc);
2056+
2057+
if (packs_nr != 1)
2058+
BUG("pack reuse not yet implemented for multiple packs");
2059+
2060+
reuse_partial_packfile_from_bitmap_1(bitmap_git, packs, reuse);
20052061

20062062
*entries = bitmap_popcount(reuse);
20072063
if (!*entries) {
@@ -2014,7 +2070,7 @@ int reuse_partial_packfile_from_bitmap(struct bitmap_index *bitmap_git,
20142070
* need to be handled separately.
20152071
*/
20162072
bitmap_and_not(result, reuse);
2017-
*packfile_out = pack;
2073+
*packfile_out = packs[0].p;
20182074
*reuse_out = reuse;
20192075
return 0;
20202076
}

0 commit comments

Comments
 (0)