Git fork

midx: implement `midx_preferred_pack()`

When performing a binary search over the objects in a MIDX's bitmap
(i.e. in pseudo-pack order), the reader reconstructs the pseudo-pack
ordering using a combination of (a) the preferred pack, (b) the pack's
lexical position in the MIDX based on pack names, and (c) the object
offset within the pack.

In order to perform this binary search, the reader must know the
identity of the preferred pack. This could be stored in the MIDX, but
isn't for historical reasons, mostly because it can easily be inferred
at read-time by looking at the object in the first bit position and
finding out which pack it was selected from in the MIDX, like so:

nth_midxed_pack_int_id(m, pack_pos_to_midx(m, 0));

In midx_to_pack_pos() which performs this binary search, we look up the
identity of the preferred pack before each search. This is relatively
quick, since it involves two table-driven lookups (one in the MIDX's
revindex for `pack_pos_to_midx()`, and another in the MIDX's object
table for `nth_midxed_pack_int_id()`).

But since the preferred pack does not change after the MIDX is written,
it is safe to cache this value on the MIDX itself.

Write a helper to do just that, and rewrite all of the existing
call-sites that care about the identity of the preferred pack in terms
of this new helper.

This will prepare us for a subsequent patch where we will need to binary
search through the MIDX's pseudo-pack order multiple times.

Signed-off-by: Taylor Blau <me@ttaylorr.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

authored by

Taylor Blau and committed by
Junio C Hamano
b1e33330 ed9f4148

+37 -20
+20
midx.c
··· 21 21 #include "refs.h" 22 22 #include "revision.h" 23 23 #include "list-objects.h" 24 + #include "pack-revindex.h" 24 25 25 26 #define MIDX_SIGNATURE 0x4d494458 /* "MIDX" */ 26 27 #define MIDX_VERSION 1 ··· 176 177 m->num_chunks = m->data[MIDX_BYTE_NUM_CHUNKS]; 177 178 178 179 m->num_packs = get_be32(m->data + MIDX_BYTE_NUM_PACKS); 180 + 181 + m->preferred_pack_idx = -1; 179 182 180 183 cf = init_chunkfile(NULL); 181 184 ··· 458 461 int midx_contains_pack(struct multi_pack_index *m, const char *idx_or_pack_name) 459 462 { 460 463 return midx_locate_pack(m, idx_or_pack_name, NULL); 464 + } 465 + 466 + int midx_preferred_pack(struct multi_pack_index *m, uint32_t *pack_int_id) 467 + { 468 + if (m->preferred_pack_idx == -1) { 469 + if (load_midx_revindex(m) < 0) { 470 + m->preferred_pack_idx = -2; 471 + return -1; 472 + } 473 + 474 + m->preferred_pack_idx = 475 + nth_midxed_pack_int_id(m, pack_pos_to_midx(m, 0)); 476 + } else if (m->preferred_pack_idx == -2) 477 + return -1; /* no revindex */ 478 + 479 + *pack_int_id = m->preferred_pack_idx; 480 + return 0; 461 481 } 462 482 463 483 int prepare_multi_pack_index_one(struct repository *r, const char *object_dir, int local)
+2
midx.h
··· 29 29 unsigned char num_chunks; 30 30 uint32_t num_packs; 31 31 uint32_t num_objects; 32 + int preferred_pack_idx; 32 33 33 34 int local; 34 35 ··· 74 75 const char *idx_or_pack_name); 75 76 int midx_locate_pack(struct multi_pack_index *m, const char *idx_or_pack_name, 76 77 uint32_t *pos); 78 + int midx_preferred_pack(struct multi_pack_index *m, uint32_t *pack_int_id); 77 79 int prepare_multi_pack_index_one(struct repository *r, const char *object_dir, int local); 78 80 79 81 /*
+7 -10
pack-bitmap.c
··· 338 338 struct stat st; 339 339 char *bitmap_name = midx_bitmap_filename(midx); 340 340 int fd = git_open(bitmap_name); 341 - uint32_t i; 341 + uint32_t i, preferred_pack; 342 342 struct packed_git *preferred; 343 343 344 344 if (fd < 0) { ··· 393 393 } 394 394 } 395 395 396 - preferred = bitmap_git->midx->packs[midx_preferred_pack(bitmap_git)]; 396 + if (midx_preferred_pack(bitmap_git->midx, &preferred_pack) < 0) { 397 + warning(_("could not determine MIDX preferred pack")); 398 + goto cleanup; 399 + } 400 + 401 + preferred = bitmap_git->midx->packs[preferred_pack]; 397 402 if (!is_pack_valid(preferred)) { 398 403 warning(_("preferred pack (%s) is invalid"), 399 404 preferred->pack_name); ··· 1924 1929 */ 1925 1930 bitmap_set(reuse, pack->bitmap_pos + pos); 1926 1931 return 0; 1927 - } 1928 - 1929 - uint32_t midx_preferred_pack(struct bitmap_index *bitmap_git) 1930 - { 1931 - struct multi_pack_index *m = bitmap_git->midx; 1932 - if (!m) 1933 - BUG("midx_preferred_pack: requires non-empty MIDX"); 1934 - return nth_midxed_pack_int_id(m, pack_pos_to_midx(bitmap_git->midx, 0)); 1935 1932 } 1936 1933 1937 1934 static void reuse_partial_packfile_from_bitmap_1(struct bitmap_index *bitmap_git,
-1
pack-bitmap.h
··· 77 77 78 78 struct bitmap_index *prepare_bitmap_walk(struct rev_info *revs, 79 79 int filter_provided_objects); 80 - uint32_t midx_preferred_pack(struct bitmap_index *bitmap_git); 81 80 void reuse_partial_packfile_from_bitmap(struct bitmap_index *bitmap_git, 82 81 struct bitmapped_pack **packs_out, 83 82 size_t *packs_nr_out,
+3 -1
pack-revindex.c
··· 542 542 * implicitly is preferred (and includes all its objects, since ties are 543 543 * broken first by pack identifier). 544 544 */ 545 - key.preferred_pack = nth_midxed_pack_int_id(m, pack_pos_to_midx(m, 0)); 545 + if (midx_preferred_pack(key.midx, &key.preferred_pack) < 0) 546 + return error(_("could not determine preferred pack")); 547 + 546 548 547 549 found = bsearch(&key, m->revindex_data, m->num_objects, 548 550 sizeof(*m->revindex_data), midx_pack_order_cmp);
+5 -8
t/helper/test-read-midx.c
··· 6 6 #include "pack-bitmap.h" 7 7 #include "packfile.h" 8 8 #include "setup.h" 9 + #include "gettext.h" 9 10 10 11 static int read_midx_file(const char *object_dir, int show_objects) 11 12 { ··· 79 80 static int read_midx_preferred_pack(const char *object_dir) 80 81 { 81 82 struct multi_pack_index *midx = NULL; 82 - struct bitmap_index *bitmap = NULL; 83 + uint32_t preferred_pack; 83 84 84 85 setup_git_directory(); 85 86 ··· 87 88 if (!midx) 88 89 return 1; 89 90 90 - bitmap = prepare_bitmap_git(the_repository); 91 - if (!bitmap) 92 - return 1; 93 - if (!bitmap_is_midx(bitmap)) { 94 - free_bitmap_index(bitmap); 91 + if (midx_preferred_pack(midx, &preferred_pack) < 0) { 92 + warning(_("could not determine MIDX preferred pack")); 95 93 return 1; 96 94 } 97 95 98 - printf("%s\n", midx->pack_names[midx_preferred_pack(bitmap)]); 99 - free_bitmap_index(bitmap); 96 + printf("%s\n", midx->pack_names[preferred_pack]); 100 97 return 0; 101 98 } 102 99