Skip to content

Commit 9d98bbf

Browse files
peffgitster
authored andcommitted
pack-revindex: store entries directly in packed_git
A pack_revindex struct has two elements: the revindex entries themselves, and a pointer to the packed_git. We need both to do lookups, because only the latter knows things like the number of objects in the pack. Now that packed_git contains the pack_revindex struct it's just as easy to pass around the packed_git itself, and we do not need the extra back-pointer. We can instead just store the entries directly in the pack. All functions which took a pack_revindex now just take a packed_git. We still lazy-load in find_pack_revindex, so most callers are unaffected. The exception is the bitmap code, which computes the revindex and caches the pointer when we load the bitmaps. We can continue to load, drop the extra cache pointer, and just access bitmap_git.pack.revindex directly. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f401533 commit 9d98bbf

File tree

4 files changed

+32
-41
lines changed

4 files changed

+32
-41
lines changed

cache.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1118,7 +1118,7 @@ extern struct packed_git {
11181118
pack_keep:1,
11191119
do_not_close:1;
11201120
unsigned char sha1[20];
1121-
struct pack_revindex reverse_index;
1121+
struct revindex_entry *revindex;
11221122
/* something like ".git/objects/pack/xxxxx.pack" */
11231123
char pack_name[FLEX_ARRAY]; /* more */
11241124
} *packed_git;

pack-bitmap.c

+5-8
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@ static struct bitmap_index {
3333
/* Packfile to which this bitmap index belongs to */
3434
struct packed_git *pack;
3535

36-
/* reverse index for the packfile */
37-
struct pack_revindex *reverse_index;
38-
3936
/*
4037
* Mark the first `reuse_objects` in the packfile as reused:
4138
* they will be sent as-is without using them for repacking
@@ -293,7 +290,7 @@ static int load_pack_bitmap(void)
293290

294291
bitmap_git.bitmaps = kh_init_sha1();
295292
bitmap_git.ext_index.positions = kh_init_sha1_pos();
296-
bitmap_git.reverse_index = revindex_for_pack(bitmap_git.pack);
293+
load_pack_revindex(bitmap_git.pack);
297294

298295
if (!(bitmap_git.commits = read_bitmap_1(&bitmap_git)) ||
299296
!(bitmap_git.trees = read_bitmap_1(&bitmap_git)) ||
@@ -379,7 +376,7 @@ static inline int bitmap_position_packfile(const unsigned char *sha1)
379376
if (!offset)
380377
return -1;
381378

382-
return find_revindex_position(bitmap_git.reverse_index, offset);
379+
return find_revindex_position(bitmap_git.pack, offset);
383380
}
384381

385382
static int bitmap_position(const unsigned char *sha1)
@@ -631,7 +628,7 @@ static void show_objects_for_type(
631628
if (pos + offset < bitmap_git.reuse_objects)
632629
continue;
633630

634-
entry = &bitmap_git.reverse_index->revindex[pos + offset];
631+
entry = &bitmap_git.pack->revindex[pos + offset];
635632
sha1 = nth_packed_object_sha1(bitmap_git.pack, entry->nr);
636633

637634
if (bitmap_git.hashes)
@@ -805,7 +802,7 @@ int reuse_partial_packfile_from_bitmap(struct packed_git **packfile,
805802
return -1;
806803

807804
bitmap_git.reuse_objects = *entries = reuse_objects;
808-
*up_to = bitmap_git.reverse_index->revindex[reuse_objects].offset;
805+
*up_to = bitmap_git.pack->revindex[reuse_objects].offset;
809806
*packfile = bitmap_git.pack;
810807

811808
return 0;
@@ -1037,7 +1034,7 @@ int rebuild_existing_bitmaps(struct packing_data *mapping,
10371034
struct revindex_entry *entry;
10381035
struct object_entry *oe;
10391036

1040-
entry = &bitmap_git.reverse_index->revindex[i];
1037+
entry = &bitmap_git.pack->revindex[i];
10411038
sha1 = nth_packed_object_sha1(bitmap_git.pack, entry->nr);
10421039
oe = packlist_find(mapping, sha1, NULL);
10431040

pack-revindex.c

+22-25
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,13 @@ static void sort_revindex(struct revindex_entry *entries, unsigned n, off_t max)
115115
/*
116116
* Ordered list of offsets of objects in the pack.
117117
*/
118-
static void create_pack_revindex(struct pack_revindex *rix)
118+
static void create_pack_revindex(struct packed_git *p)
119119
{
120-
struct packed_git *p = rix->p;
121120
unsigned num_ent = p->num_objects;
122121
unsigned i;
123122
const char *index = p->index_data;
124123

125-
rix->revindex = xmalloc(sizeof(*rix->revindex) * (num_ent + 1));
124+
p->revindex = xmalloc(sizeof(*p->revindex) * (num_ent + 1));
126125
index += 4 * 256;
127126

128127
if (p->index_version > 1) {
@@ -132,46 +131,42 @@ static void create_pack_revindex(struct pack_revindex *rix)
132131
for (i = 0; i < num_ent; i++) {
133132
uint32_t off = ntohl(*off_32++);
134133
if (!(off & 0x80000000)) {
135-
rix->revindex[i].offset = off;
134+
p->revindex[i].offset = off;
136135
} else {
137-
rix->revindex[i].offset =
136+
p->revindex[i].offset =
138137
((uint64_t)ntohl(*off_64++)) << 32;
139-
rix->revindex[i].offset |=
138+
p->revindex[i].offset |=
140139
ntohl(*off_64++);
141140
}
142-
rix->revindex[i].nr = i;
141+
p->revindex[i].nr = i;
143142
}
144143
} else {
145144
for (i = 0; i < num_ent; i++) {
146145
uint32_t hl = *((uint32_t *)(index + 24 * i));
147-
rix->revindex[i].offset = ntohl(hl);
148-
rix->revindex[i].nr = i;
146+
p->revindex[i].offset = ntohl(hl);
147+
p->revindex[i].nr = i;
149148
}
150149
}
151150

152151
/* This knows the pack format -- the 20-byte trailer
153152
* follows immediately after the last object data.
154153
*/
155-
rix->revindex[num_ent].offset = p->pack_size - 20;
156-
rix->revindex[num_ent].nr = -1;
157-
sort_revindex(rix->revindex, num_ent, p->pack_size);
154+
p->revindex[num_ent].offset = p->pack_size - 20;
155+
p->revindex[num_ent].nr = -1;
156+
sort_revindex(p->revindex, num_ent, p->pack_size);
158157
}
159158

160-
struct pack_revindex *revindex_for_pack(struct packed_git *p)
159+
void load_pack_revindex(struct packed_git *p)
161160
{
162-
struct pack_revindex *rix = &p->reverse_index;
163-
if (!rix->revindex) {
164-
rix->p = p;
165-
create_pack_revindex(rix);
166-
}
167-
return rix;
161+
if (!p->revindex)
162+
create_pack_revindex(p);
168163
}
169164

170-
int find_revindex_position(struct pack_revindex *pridx, off_t ofs)
165+
int find_revindex_position(struct packed_git *p, off_t ofs)
171166
{
172167
int lo = 0;
173-
int hi = pridx->p->num_objects + 1;
174-
struct revindex_entry *revindex = pridx->revindex;
168+
int hi = p->num_objects + 1;
169+
struct revindex_entry *revindex = p->revindex;
175170

176171
do {
177172
unsigned mi = lo + (hi - lo) / 2;
@@ -189,11 +184,13 @@ int find_revindex_position(struct pack_revindex *pridx, off_t ofs)
189184

190185
struct revindex_entry *find_pack_revindex(struct packed_git *p, off_t ofs)
191186
{
192-
struct pack_revindex *pridx = revindex_for_pack(p);
193-
int pos = find_revindex_position(pridx, ofs);
187+
int pos;
188+
189+
load_pack_revindex(p);
190+
pos = find_revindex_position(p, ofs);
194191

195192
if (pos < 0)
196193
return NULL;
197194

198-
return pridx->revindex + pos;
195+
return p->revindex + pos;
199196
}

pack-revindex.h

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
#ifndef PACK_REVINDEX_H
22
#define PACK_REVINDEX_H
33

4+
struct packed_git;
5+
46
struct revindex_entry {
57
off_t offset;
68
unsigned int nr;
79
};
810

9-
struct pack_revindex {
10-
struct packed_git *p;
11-
struct revindex_entry *revindex;
12-
};
13-
14-
struct pack_revindex *revindex_for_pack(struct packed_git *p);
15-
int find_revindex_position(struct pack_revindex *pridx, off_t ofs);
11+
void load_pack_revindex(struct packed_git *p);
12+
int find_revindex_position(struct packed_git *p, off_t ofs);
1613

1714
struct revindex_entry *find_pack_revindex(struct packed_git *p, off_t ofs);
1815

0 commit comments

Comments
 (0)