Skip to content

Commit b4e00f7

Browse files
jonathantanmygitster
authored andcommitted
packfile: refactor hash search with fanout table
Subsequent patches will introduce file formats that make use of a fanout array and a sorted table containing hashes, just like packfiles. Refactor the hash search in packfile.c into its own function, so that those patches can make use of it as well. Signed-off-by: Jonathan Tan <jonathantanmy@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 4669e7d commit b4e00f7

File tree

3 files changed

+54
-14
lines changed

3 files changed

+54
-14
lines changed

packfile.c

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1712,7 +1712,8 @@ off_t find_pack_entry_one(const unsigned char *sha1,
17121712
{
17131713
const uint32_t *level1_ofs = p->index_data;
17141714
const unsigned char *index = p->index_data;
1715-
unsigned hi, lo, stride;
1715+
unsigned stride;
1716+
uint32_t result;
17161717

17171718
if (!index) {
17181719
if (open_pack_index(p))
@@ -1725,26 +1726,15 @@ off_t find_pack_entry_one(const unsigned char *sha1,
17251726
index += 8;
17261727
}
17271728
index += 4 * 256;
1728-
hi = ntohl(level1_ofs[*sha1]);
1729-
lo = ((*sha1 == 0x0) ? 0 : ntohl(level1_ofs[*sha1 - 1]));
17301729
if (p->index_version > 1) {
17311730
stride = 20;
17321731
} else {
17331732
stride = 24;
17341733
index += 4;
17351734
}
17361735

1737-
while (lo < hi) {
1738-
unsigned mi = lo + (hi - lo) / 2;
1739-
int cmp = hashcmp(index + mi * stride, sha1);
1740-
1741-
if (!cmp)
1742-
return nth_packed_object_offset(p, mi);
1743-
if (cmp > 0)
1744-
hi = mi;
1745-
else
1746-
lo = mi+1;
1747-
}
1736+
if (bsearch_hash(sha1, level1_ofs, index, stride, &result))
1737+
return nth_packed_object_offset(p, result);
17481738
return 0;
17491739
}
17501740

sha1-lookup.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,31 @@ int sha1_pos(const unsigned char *sha1, void *table, size_t nr,
9999
} while (lo < hi);
100100
return -lo-1;
101101
}
102+
103+
int bsearch_hash(const unsigned char *sha1, const uint32_t *fanout_nbo,
104+
const unsigned char *table, size_t stride, uint32_t *result)
105+
{
106+
uint32_t hi, lo;
107+
108+
hi = ntohl(fanout_nbo[*sha1]);
109+
lo = ((*sha1 == 0x0) ? 0 : ntohl(fanout_nbo[*sha1 - 1]));
110+
111+
while (lo < hi) {
112+
unsigned mi = lo + (hi - lo) / 2;
113+
int cmp = hashcmp(table + mi * stride, sha1);
114+
115+
if (!cmp) {
116+
if (result)
117+
*result = mi;
118+
return 1;
119+
}
120+
if (cmp > 0)
121+
hi = mi;
122+
else
123+
lo = mi + 1;
124+
}
125+
126+
if (result)
127+
*result = lo;
128+
return 0;
129+
}

sha1-lookup.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,26 @@ extern int sha1_pos(const unsigned char *sha1,
77
void *table,
88
size_t nr,
99
sha1_access_fn fn);
10+
11+
/*
12+
* Searches for sha1 in table, using the given fanout table to determine the
13+
* interval to search, then using binary search. Returns 1 if found, 0 if not.
14+
*
15+
* Takes the following parameters:
16+
*
17+
* - sha1: the hash to search for
18+
* - fanout_nbo: a 256-element array of NETWORK-order 32-bit integers; the
19+
* integer at position i represents the number of elements in table whose
20+
* first byte is less than or equal to i
21+
* - table: a sorted list of hashes with optional extra information in between
22+
* - stride: distance between two consecutive elements in table (should be
23+
* GIT_MAX_RAWSZ or greater)
24+
* - result: if not NULL, this function stores the element index of the
25+
* position found (if the search is successful) or the index of the least
26+
* element that is greater than sha1 (if the search is not successful)
27+
*
28+
* This function does not verify the validity of the fanout table.
29+
*/
30+
int bsearch_hash(const unsigned char *sha1, const uint32_t *fanout_nbo,
31+
const unsigned char *table, size_t stride, uint32_t *result);
1032
#endif

0 commit comments

Comments
 (0)