Skip to content

Commit 7d4bebf

Browse files
committed
Merge branch 'jt/binsearch-with-fanout' into HEAD
* jt/binsearch-with-fanout: packfile: refactor hash search with fanout table packfile: remove GIT_DEBUG_LOOKUP log statements
2 parents 0fd90da + b4e00f7 commit 7d4bebf

File tree

3 files changed

+54
-25
lines changed

3 files changed

+54
-25
lines changed

packfile.c

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1722,11 +1722,8 @@ off_t find_pack_entry_one(const unsigned char *sha1,
17221722
{
17231723
const uint32_t *level1_ofs = p->index_data;
17241724
const unsigned char *index = p->index_data;
1725-
unsigned hi, lo, stride;
1726-
static int debug_lookup = -1;
1727-
1728-
if (debug_lookup < 0)
1729-
debug_lookup = !!getenv("GIT_DEBUG_LOOKUP");
1725+
unsigned stride;
1726+
uint32_t result;
17301727

17311728
if (!index) {
17321729
if (open_pack_index(p))
@@ -1739,33 +1736,15 @@ off_t find_pack_entry_one(const unsigned char *sha1,
17391736
index += 8;
17401737
}
17411738
index += 4 * 256;
1742-
hi = ntohl(level1_ofs[*sha1]);
1743-
lo = ((*sha1 == 0x0) ? 0 : ntohl(level1_ofs[*sha1 - 1]));
17441739
if (p->index_version > 1) {
17451740
stride = 20;
17461741
} else {
17471742
stride = 24;
17481743
index += 4;
17491744
}
17501745

1751-
if (debug_lookup)
1752-
printf("%02x%02x%02x... lo %u hi %u nr %"PRIu32"\n",
1753-
sha1[0], sha1[1], sha1[2], lo, hi, p->num_objects);
1754-
1755-
while (lo < hi) {
1756-
unsigned mi = lo + (hi - lo) / 2;
1757-
int cmp = hashcmp(index + mi * stride, sha1);
1758-
1759-
if (debug_lookup)
1760-
printf("lo %u hi %u rg %u mi %u\n",
1761-
lo, hi, hi - lo, mi);
1762-
if (!cmp)
1763-
return nth_packed_object_offset(p, mi);
1764-
if (cmp > 0)
1765-
hi = mi;
1766-
else
1767-
lo = mi+1;
1768-
}
1746+
if (bsearch_hash(sha1, level1_ofs, index, stride, &result))
1747+
return nth_packed_object_offset(p, result);
17691748
return 0;
17701749
}
17711750

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)