Skip to content

Commit b1e3333

Browse files
ttaylorrgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ed9f414 commit b1e3333

File tree

6 files changed

+37
-20
lines changed

6 files changed

+37
-20
lines changed

midx.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "refs.h"
2222
#include "revision.h"
2323
#include "list-objects.h"
24+
#include "pack-revindex.h"
2425

2526
#define MIDX_SIGNATURE 0x4d494458 /* "MIDX" */
2627
#define MIDX_VERSION 1
@@ -177,6 +178,8 @@ struct multi_pack_index *load_multi_pack_index(const char *object_dir, int local
177178

178179
m->num_packs = get_be32(m->data + MIDX_BYTE_NUM_PACKS);
179180

181+
m->preferred_pack_idx = -1;
182+
180183
cf = init_chunkfile(NULL);
181184

182185
if (read_table_of_contents(cf, m->data, midx_size,
@@ -460,6 +463,23 @@ int midx_contains_pack(struct multi_pack_index *m, const char *idx_or_pack_name)
460463
return midx_locate_pack(m, idx_or_pack_name, NULL);
461464
}
462465

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;
481+
}
482+
463483
int prepare_multi_pack_index_one(struct repository *r, const char *object_dir, int local)
464484
{
465485
struct multi_pack_index *m;

midx.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ struct multi_pack_index {
2929
unsigned char num_chunks;
3030
uint32_t num_packs;
3131
uint32_t num_objects;
32+
int preferred_pack_idx;
3233

3334
int local;
3435

@@ -74,6 +75,7 @@ int midx_contains_pack(struct multi_pack_index *m,
7475
const char *idx_or_pack_name);
7576
int midx_locate_pack(struct multi_pack_index *m, const char *idx_or_pack_name,
7677
uint32_t *pos);
78+
int midx_preferred_pack(struct multi_pack_index *m, uint32_t *pack_int_id);
7779
int prepare_multi_pack_index_one(struct repository *r, const char *object_dir, int local);
7880

7981
/*

pack-bitmap.c

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ static int open_midx_bitmap_1(struct bitmap_index *bitmap_git,
338338
struct stat st;
339339
char *bitmap_name = midx_bitmap_filename(midx);
340340
int fd = git_open(bitmap_name);
341-
uint32_t i;
341+
uint32_t i, preferred_pack;
342342
struct packed_git *preferred;
343343

344344
if (fd < 0) {
@@ -393,7 +393,12 @@ static int open_midx_bitmap_1(struct bitmap_index *bitmap_git,
393393
}
394394
}
395395

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];
397402
if (!is_pack_valid(preferred)) {
398403
warning(_("preferred pack (%s) is invalid"),
399404
preferred->pack_name);
@@ -1926,14 +1931,6 @@ static int try_partial_reuse(struct bitmapped_pack *pack,
19261931
return 0;
19271932
}
19281933

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-
}
1936-
19371934
static void reuse_partial_packfile_from_bitmap_1(struct bitmap_index *bitmap_git,
19381935
struct bitmapped_pack *pack,
19391936
struct bitmap *reuse)

pack-bitmap.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ int test_bitmap_hashes(struct repository *r);
7777

7878
struct bitmap_index *prepare_bitmap_walk(struct rev_info *revs,
7979
int filter_provided_objects);
80-
uint32_t midx_preferred_pack(struct bitmap_index *bitmap_git);
8180
void reuse_partial_packfile_from_bitmap(struct bitmap_index *bitmap_git,
8281
struct bitmapped_pack **packs_out,
8382
size_t *packs_nr_out,

pack-revindex.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,9 @@ int midx_to_pack_pos(struct multi_pack_index *m, uint32_t at, uint32_t *pos)
542542
* implicitly is preferred (and includes all its objects, since ties are
543543
* broken first by pack identifier).
544544
*/
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+
546548

547549
found = bsearch(&key, m->revindex_data, m->num_objects,
548550
sizeof(*m->revindex_data), midx_pack_order_cmp);

t/helper/test-read-midx.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "pack-bitmap.h"
77
#include "packfile.h"
88
#include "setup.h"
9+
#include "gettext.h"
910

1011
static int read_midx_file(const char *object_dir, int show_objects)
1112
{
@@ -79,24 +80,20 @@ static int read_midx_checksum(const char *object_dir)
7980
static int read_midx_preferred_pack(const char *object_dir)
8081
{
8182
struct multi_pack_index *midx = NULL;
82-
struct bitmap_index *bitmap = NULL;
83+
uint32_t preferred_pack;
8384

8485
setup_git_directory();
8586

8687
midx = load_multi_pack_index(object_dir, 1);
8788
if (!midx)
8889
return 1;
8990

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"));
9593
return 1;
9694
}
9795

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]);
10097
return 0;
10198
}
10299

0 commit comments

Comments
 (0)