2 #include "sha1-lookup.h"
5 * Conventional binary search loop looks like this:
9 * unsigned mi = (lo + hi) / 2;
10 * int cmp = "entry pointed at by mi" minus "target";
12 * return (mi is the wanted one)
14 * hi = mi; "mi is larger than target"
16 * lo = mi+1; "mi is smaller than target"
21 * - When entering the loop, lo points at a slot that is never
22 * above the target (it could be at the target), hi points at a
23 * slot that is guaranteed to be above the target (it can never
26 * - We find a point 'mi' between lo and hi (mi could be the same
27 * as lo, but never can be as same as hi), and check if it hits
28 * the target. There are three cases:
30 * - if it is a hit, we are happy.
32 * - if it is strictly higher than the target, we set it to hi,
33 * and repeat the search.
35 * - if it is strictly lower than the target, we update lo to
36 * one slot after it, because we allow lo to be at the target.
38 * If the loop exits, there is no matching entry.
40 * When choosing 'mi', we do not have to take the "middle" but
41 * anywhere in between lo and hi, as long as lo <= mi < hi is
42 * satisfied. When we somehow know that the distance between the
43 * target and lo is much shorter than the target and hi, we could
44 * pick mi that is much closer to lo than the midway.
46 * Now, we can take advantage of the fact that SHA-1 is a good hash
47 * function, and as long as there are enough entries in the table, we
48 * can expect uniform distribution. An entry that begins with for
49 * example "deadbeef..." is much likely to appear much later than in
50 * the midway of the table. It can reasonably be expected to be near
51 * 87% (222/256) from the top of the table.
53 * The table at "table" holds at least "nr" entries of "elem_size"
54 * bytes each. Each entry has the SHA-1 key at "key_offset". The
55 * table is sorted by the SHA-1 key of the entries. The caller wants
56 * to find the entry with "key", and knows that the entry at "lo" is
57 * not higher than the entry it is looking for, and that the entry at
58 * "hi" is higher than the entry it is looking for.
60 int sha1_entry_pos(const void *table,
63 unsigned lo, unsigned hi, unsigned nr,
64 const unsigned char *key)
66 const unsigned char *base = table;
67 const unsigned char *hi_key, *lo_key;
69 static int debug_lookup = -1;
72 debug_lookup = !!getenv("GIT_DEBUG_LOOKUP");
80 hi_key = base + elem_size * hi + key_offset;
81 lo_key = base + elem_size * lo + key_offset;
86 unsigned ofs, mi, range;
87 unsigned lov, hiv, kyv;
88 const unsigned char *mi_key;
92 for (ofs = ofs_0; ofs < 20; ofs++)
93 if (lo_key[ofs] != hi_key[ofs])
97 * byte 0 thru (ofs-1) are the same between
98 * lo and hi; ofs is the first byte that is
103 hiv = (hiv << 8) | hi_key[ofs_0+1];
112 lov = (lov << 8) | lo_key[ofs_0+1];
113 kyv = (kyv << 8) | key[ofs_0+1];
122 if (kyv == lov && lov < hiv - 1)
124 else if (kyv == hiv - 1 && lov < kyv)
127 mi = (range - 1) * (kyv - lov) / (hiv - lov) + lo;
130 printf("lo %u hi %u rg %u mi %u ", lo, hi, range, mi);
131 printf("ofs %u lov %x, hiv %x, kyv %x\n",
132 ofs_0, lov, hiv, kyv);
134 if (!(lo <= mi && mi < hi))
135 die("assertion failure lo %u mi %u hi %u %s",
136 lo, mi, hi, sha1_to_hex(key));
138 mi_key = base + elem_size * mi + key_offset;
139 cmp = memcmp(mi_key + ofs_0, key + ofs_0, 20 - ofs_0);
148 lo_key = mi_key + elem_size;