Skip to content

Commit 51f813c

Browse files
committed
Merge branch 'ds/bsearch-hash'
Code to find the length to uniquely abbreviate object names based on packfile content, which is a relatively recent addtion, has been optimized to use the same fan-out table. * ds/bsearch-hash: sha1_name: use bsearch_pack() in unique_in_pack() sha1_name: use bsearch_pack() for abbreviations packfile: define and use bsearch_pack() sha1_name: convert struct min_abbrev_data to object_id
2 parents 57e4b1c + 902f5a2 commit 51f813c

File tree

3 files changed

+42
-57
lines changed

3 files changed

+42
-57
lines changed

packfile.c

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1654,6 +1654,29 @@ void *unpack_entry(struct packed_git *p, off_t obj_offset,
16541654
return data;
16551655
}
16561656

1657+
int bsearch_pack(const struct object_id *oid, const struct packed_git *p, uint32_t *result)
1658+
{
1659+
const unsigned char *index_fanout = p->index_data;
1660+
const unsigned char *index_lookup;
1661+
int index_lookup_width;
1662+
1663+
if (!index_fanout)
1664+
BUG("bsearch_pack called without a valid pack-index");
1665+
1666+
index_lookup = index_fanout + 4 * 256;
1667+
if (p->index_version == 1) {
1668+
index_lookup_width = 24;
1669+
index_lookup += 4;
1670+
} else {
1671+
index_lookup_width = 20;
1672+
index_fanout += 8;
1673+
index_lookup += 8;
1674+
}
1675+
1676+
return bsearch_hash(oid->hash, (const uint32_t*)index_fanout,
1677+
index_lookup, index_lookup_width, result);
1678+
}
1679+
16571680
const unsigned char *nth_packed_object_sha1(struct packed_git *p,
16581681
uint32_t n)
16591682
{
@@ -1720,30 +1743,17 @@ off_t nth_packed_object_offset(const struct packed_git *p, uint32_t n)
17201743
off_t find_pack_entry_one(const unsigned char *sha1,
17211744
struct packed_git *p)
17221745
{
1723-
const uint32_t *level1_ofs = p->index_data;
17241746
const unsigned char *index = p->index_data;
1725-
unsigned stride;
1747+
struct object_id oid;
17261748
uint32_t result;
17271749

17281750
if (!index) {
17291751
if (open_pack_index(p))
17301752
return 0;
1731-
level1_ofs = p->index_data;
1732-
index = p->index_data;
1733-
}
1734-
if (p->index_version > 1) {
1735-
level1_ofs += 2;
1736-
index += 8;
1737-
}
1738-
index += 4 * 256;
1739-
if (p->index_version > 1) {
1740-
stride = 20;
1741-
} else {
1742-
stride = 24;
1743-
index += 4;
17441753
}
17451754

1746-
if (bsearch_hash(sha1, level1_ofs, index, stride, &result))
1755+
hashcpy(oid.hash, sha1);
1756+
if (bsearch_pack(&oid, p, &result))
17471757
return nth_packed_object_offset(p, result);
17481758
return 0;
17491759
}

packfile.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ extern struct packed_git *add_packed_git(const char *path, size_t path_len, int
7878
*/
7979
extern void check_pack_index_ptr(const struct packed_git *p, const void *ptr);
8080

81+
/*
82+
* Perform binary search on a pack-index for a given oid. Packfile is expected to
83+
* have a valid pack-index.
84+
*
85+
* See 'bsearch_hash' for more information.
86+
*/
87+
int bsearch_pack(const struct object_id *oid, const struct packed_git *p, uint32_t *result);
88+
8189
/*
8290
* Return the SHA-1 of the nth object within the specified packfile.
8391
* Open the index if it is not already open. The return value points

sha1_name.c

Lines changed: 8 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -150,31 +150,14 @@ static int match_sha(unsigned len, const unsigned char *a, const unsigned char *
150150
static void unique_in_pack(struct packed_git *p,
151151
struct disambiguate_state *ds)
152152
{
153-
uint32_t num, last, i, first = 0;
153+
uint32_t num, i, first = 0;
154154
const struct object_id *current = NULL;
155155

156156
if (open_pack_index(p) || !p->num_objects)
157157
return;
158158

159159
num = p->num_objects;
160-
last = num;
161-
while (first < last) {
162-
uint32_t mid = first + (last - first) / 2;
163-
const unsigned char *current;
164-
int cmp;
165-
166-
current = nth_packed_object_sha1(p, mid);
167-
cmp = hashcmp(ds->bin_pfx.hash, current);
168-
if (!cmp) {
169-
first = mid;
170-
break;
171-
}
172-
if (cmp > 0) {
173-
first = mid+1;
174-
continue;
175-
}
176-
last = mid;
177-
}
160+
bsearch_pack(&ds->bin_pfx, p, &first);
178161

179162
/*
180163
* At this point, "first" is the location of the lowest object
@@ -480,7 +463,7 @@ struct min_abbrev_data {
480463
unsigned int init_len;
481464
unsigned int cur_len;
482465
char *hex;
483-
const unsigned char *hash;
466+
const struct object_id *oid;
484467
};
485468

486469
static inline char get_hex_char_from_oid(const struct object_id *oid,
@@ -512,32 +495,16 @@ static void find_abbrev_len_for_pack(struct packed_git *p,
512495
struct min_abbrev_data *mad)
513496
{
514497
int match = 0;
515-
uint32_t num, last, first = 0;
498+
uint32_t num, first = 0;
516499
struct object_id oid;
500+
const struct object_id *mad_oid;
517501

518502
if (open_pack_index(p) || !p->num_objects)
519503
return;
520504

521505
num = p->num_objects;
522-
last = num;
523-
while (first < last) {
524-
uint32_t mid = first + (last - first) / 2;
525-
const unsigned char *current;
526-
int cmp;
527-
528-
current = nth_packed_object_sha1(p, mid);
529-
cmp = hashcmp(mad->hash, current);
530-
if (!cmp) {
531-
match = 1;
532-
first = mid;
533-
break;
534-
}
535-
if (cmp > 0) {
536-
first = mid + 1;
537-
continue;
538-
}
539-
last = mid;
540-
}
506+
mad_oid = mad->oid;
507+
match = bsearch_pack(mad_oid, p, &first);
541508

542509
/*
543510
* first is now the position in the packfile where we would insert
@@ -603,7 +570,7 @@ int find_unique_abbrev_r(char *hex, const struct object_id *oid, int len)
603570
mad.init_len = len;
604571
mad.cur_len = len;
605572
mad.hex = hex;
606-
mad.hash = oid->hash;
573+
mad.oid = oid;
607574

608575
find_abbrev_len_packed(&mad);
609576

0 commit comments

Comments
 (0)