]>
git.proxmox.com Git - libgit2.git/blob - src/sha1_lookup.c
2 * This file is basically taken from git code.
3 * This file is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License, version 2,
5 * as published by the Free Software Foundation.
7 * In addition to the permissions in the GNU General Public License,
8 * the authors give you unlimited permission to link the compiled
9 * version of this file into combinations with other programs,
10 * and to distribute those combinations without any restriction
11 * coming from the use of this file. (The General Public License
12 * restrictions do apply in other respects; for example, they cover
13 * modification of the file, and distribution when not linked into
14 * a combined executable.)
16 * This file is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; see the file COPYING. If not, write to
23 * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
24 * Boston, MA 02110-1301, USA.
29 #include "sha1_lookup.h"
33 * Conventional binary search loop looks like this:
37 * unsigned mi = (lo + hi) / 2;
38 * int cmp = "entry pointed at by mi" minus "target";
40 * return (mi is the wanted one)
42 * hi = mi; "mi is larger than target"
44 * lo = mi+1; "mi is smaller than target"
49 * - When entering the loop, lo points at a slot that is never
50 * above the target (it could be at the target), hi points at a
51 * slot that is guaranteed to be above the target (it can never
54 * - We find a point 'mi' between lo and hi (mi could be the same
55 * as lo, but never can be as same as hi), and check if it hits
56 * the target. There are three cases:
58 * - if it is a hit, we are happy.
60 * - if it is strictly higher than the target, we set it to hi,
61 * and repeat the search.
63 * - if it is strictly lower than the target, we update lo to
64 * one slot after it, because we allow lo to be at the target.
66 * If the loop exits, there is no matching entry.
68 * When choosing 'mi', we do not have to take the "middle" but
69 * anywhere in between lo and hi, as long as lo <= mi < hi is
70 * satisfied. When we somehow know that the distance between the
71 * target and lo is much shorter than the target and hi, we could
72 * pick mi that is much closer to lo than the midway.
74 * Now, we can take advantage of the fact that SHA-1 is a good hash
75 * function, and as long as there are enough entries in the table, we
76 * can expect uniform distribution. An entry that begins with for
77 * example "deadbeef..." is much likely to appear much later than in
78 * the midway of the table. It can reasonably be expected to be near
79 * 87% (222/256) from the top of the table.
81 * However, we do not want to pick "mi" too precisely. If the entry at
82 * the 87% in the above example turns out to be higher than the target
83 * we are looking for, we would end up narrowing the search space down
84 * only by 13%, instead of 50% we would get if we did a simple binary
85 * search. So we would want to hedge our bets by being less aggressive.
87 * The table at "table" holds at least "nr" entries of "elem_size"
88 * bytes each. Each entry has the SHA-1 key at "key_offset". The
89 * table is sorted by the SHA-1 key of the entries. The caller wants
90 * to find the entry with "key", and knows that the entry at "lo" is
91 * not higher than the entry it is looking for, and that the entry at
92 * "hi" is higher than the entry it is looking for.
94 int sha1_entry_pos(const void *table
,
97 unsigned lo
, unsigned hi
, unsigned nr
,
98 const unsigned char *key
)
100 const unsigned char *base
= (const unsigned char*)table
;
101 const unsigned char *hi_key
, *lo_key
;
110 hi_key
= base
+ elem_size
* hi
+ key_offset
;
111 lo_key
= base
+ elem_size
* lo
+ key_offset
;
116 unsigned ofs
, mi
, range
;
117 unsigned lov
, hiv
, kyv
;
118 const unsigned char *mi_key
;
122 for (ofs
= ofs_0
; ofs
< 20; ofs
++)
123 if (lo_key
[ofs
] != hi_key
[ofs
])
127 * byte 0 thru (ofs-1) are the same between
128 * lo and hi; ofs is the first byte that is
133 hiv
= (hiv
<< 8) | hi_key
[ofs_0
+1];
142 lov
= (lov
<< 8) | lo_key
[ofs_0
+1];
143 kyv
= (kyv
<< 8) | key
[ofs_0
+1];
153 * Even if we know the target is much closer to 'hi'
154 * than 'lo', if we pick too precisely and overshoot
155 * (e.g. when we know 'mi' is closer to 'hi' than to
156 * 'lo', pick 'mi' that is higher than the target), we
157 * end up narrowing the search space by a smaller
158 * amount (i.e. the distance between 'mi' and 'hi')
159 * than what we would have (i.e. about half of 'lo'
160 * and 'hi'). Hedge our bets to pick 'mi' less
161 * aggressively, i.e. make 'mi' a bit closer to the
162 * middle than we would otherwise pick.
164 kyv
= (kyv
* 6 + lov
+ hiv
) / 8;
171 mi
= (range
- 1) * (kyv
- lov
) / (hiv
- lov
) + lo
;
173 #ifdef INDEX_DEBUG_LOOKUP
174 printf("lo %u hi %u rg %u mi %u ", lo
, hi
, range
, mi
);
175 printf("ofs %u lov %x, hiv %x, kyv %x\n",
176 ofs_0
, lov
, hiv
, kyv
);
179 if (!(lo
<= mi
&& mi
< hi
)) {
180 return git__throw(GIT_ERROR
, "Assertion failure. Binary search invariant is false");
183 mi_key
= base
+ elem_size
* mi
+ key_offset
;
184 cmp
= memcmp(mi_key
+ ofs_0
, key
+ ofs_0
, 20 - ofs_0
);
192 lo_key
= mi_key
+ elem_size
;