Blame src/sha1_lookup.c

Packit Service 20376f
/*
Packit Service 20376f
 * Copyright (C) the libgit2 contributors. All rights reserved.
Packit Service 20376f
 *
Packit Service 20376f
 * This file is part of libgit2, distributed under the GNU GPL v2 with
Packit Service 20376f
 * a Linking Exception. For full terms see the included COPYING file.
Packit Service 20376f
 */
Packit Service 20376f
Packit Service 20376f
#include <stdio.h>
Packit Service 20376f
Packit Service 20376f
#include "sha1_lookup.h"
Packit Service 20376f
#include "common.h"
Packit Service 20376f
#include "oid.h"
Packit Service 20376f
Packit Service 20376f
/*
Packit Service 20376f
 * Conventional binary search loop looks like this:
Packit Service 20376f
 *
Packit Service 20376f
 *	unsigned lo, hi;
Packit Service 20376f
 *		do {
Packit Service 20376f
 *				unsigned mi = (lo + hi) / 2;
Packit Service 20376f
 *				int cmp = "entry pointed at by mi" minus "target";
Packit Service 20376f
 *				if (!cmp)
Packit Service 20376f
 *						return (mi is the wanted one)
Packit Service 20376f
 *				if (cmp > 0)
Packit Service 20376f
 *						hi = mi; "mi is larger than target"
Packit Service 20376f
 *				else
Packit Service 20376f
 *						lo = mi+1; "mi is smaller than target"
Packit Service 20376f
 *		} while (lo < hi);
Packit Service 20376f
 *
Packit Service 20376f
 * The invariants are:
Packit Service 20376f
 *
Packit Service 20376f
 * - When entering the loop, lo points at a slot that is never
Packit Service 20376f
 *	above the target (it could be at the target), hi points at a
Packit Service 20376f
 *	slot that is guaranteed to be above the target (it can never
Packit Service 20376f
 *	be at the target).
Packit Service 20376f
 *
Packit Service 20376f
 * - We find a point 'mi' between lo and hi (mi could be the same
Packit Service 20376f
 *	as lo, but never can be as same as hi), and check if it hits
Packit Service 20376f
 *	the target. There are three cases:
Packit Service 20376f
 *
Packit Service 20376f
 *	- if it is a hit, we are happy.
Packit Service 20376f
 *
Packit Service 20376f
 *	- if it is strictly higher than the target, we set it to hi,
Packit Service 20376f
 *		and repeat the search.
Packit Service 20376f
 *
Packit Service 20376f
 *	- if it is strictly lower than the target, we update lo to
Packit Service 20376f
 *		one slot after it, because we allow lo to be at the target.
Packit Service 20376f
 *
Packit Service 20376f
 *	If the loop exits, there is no matching entry.
Packit Service 20376f
 *
Packit Service 20376f
 * When choosing 'mi', we do not have to take the "middle" but
Packit Service 20376f
 * anywhere in between lo and hi, as long as lo <= mi < hi is
Packit Service 20376f
 * satisfied. When we somehow know that the distance between the
Packit Service 20376f
 * target and lo is much shorter than the target and hi, we could
Packit Service 20376f
 * pick mi that is much closer to lo than the midway.
Packit Service 20376f
 *
Packit Service 20376f
 * Now, we can take advantage of the fact that SHA-1 is a good hash
Packit Service 20376f
 * function, and as long as there are enough entries in the table, we
Packit Service 20376f
 * can expect uniform distribution. An entry that begins with for
Packit Service 20376f
 * example "deadbeef..." is much likely to appear much later than in
Packit Service 20376f
 * the midway of the table. It can reasonably be expected to be near
Packit Service 20376f
 * 87% (222/256) from the top of the table.
Packit Service 20376f
 *
Packit Service 20376f
 * However, we do not want to pick "mi" too precisely. If the entry at
Packit Service 20376f
 * the 87% in the above example turns out to be higher than the target
Packit Service 20376f
 * we are looking for, we would end up narrowing the search space down
Packit Service 20376f
 * only by 13%, instead of 50% we would get if we did a simple binary
Packit Service 20376f
 * search. So we would want to hedge our bets by being less aggressive.
Packit Service 20376f
 *
Packit Service 20376f
 * The table at "table" holds at least "nr" entries of "elem_size"
Packit Service 20376f
 * bytes each. Each entry has the SHA-1 key at "key_offset". The
Packit Service 20376f
 * table is sorted by the SHA-1 key of the entries. The caller wants
Packit Service 20376f
 * to find the entry with "key", and knows that the entry at "lo" is
Packit Service 20376f
 * not higher than the entry it is looking for, and that the entry at
Packit Service 20376f
 * "hi" is higher than the entry it is looking for.
Packit Service 20376f
 */
Packit Service 20376f
int sha1_entry_pos(const void *table,
Packit Service 20376f
			size_t elem_size,
Packit Service 20376f
			size_t key_offset,
Packit Service 20376f
			unsigned lo, unsigned hi, unsigned nr,
Packit Service 20376f
			const unsigned char *key)
Packit Service 20376f
{
Packit Service 20376f
	const unsigned char *base = (const unsigned char*)table;
Packit Service 20376f
	const unsigned char *hi_key, *lo_key;
Packit Service 20376f
	unsigned ofs_0;
Packit Service 20376f
Packit Service 20376f
	if (!nr || lo >= hi)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	if (nr == hi)
Packit Service 20376f
		hi_key = NULL;
Packit Service 20376f
	else
Packit Service 20376f
		hi_key = base + elem_size * hi + key_offset;
Packit Service 20376f
	lo_key = base + elem_size * lo + key_offset;
Packit Service 20376f
Packit Service 20376f
	ofs_0 = 0;
Packit Service 20376f
	do {
Packit Service 20376f
		int cmp;
Packit Service 20376f
		unsigned ofs, mi, range;
Packit Service 20376f
		unsigned lov, hiv, kyv;
Packit Service 20376f
		const unsigned char *mi_key;
Packit Service 20376f
Packit Service 20376f
		range = hi - lo;
Packit Service 20376f
		if (hi_key) {
Packit Service 20376f
			for (ofs = ofs_0; ofs < 20; ofs++)
Packit Service 20376f
				if (lo_key[ofs] != hi_key[ofs])
Packit Service 20376f
					break;
Packit Service 20376f
			ofs_0 = ofs;
Packit Service 20376f
			/*
Packit Service 20376f
			 * byte 0 thru (ofs-1) are the same between
Packit Service 20376f
			 * lo and hi; ofs is the first byte that is
Packit Service 20376f
			 * different.
Packit Service 20376f
			 *
Packit Service 20376f
			 * If ofs==20, then no bytes are different,
Packit Service 20376f
			 * meaning we have entries with duplicate
Packit Service 20376f
			 * keys. We know that we are in a solid run
Packit Service 20376f
			 * of this entry (because the entries are
Packit Service 20376f
			 * sorted, and our lo and hi are the same,
Packit Service 20376f
			 * there can be nothing but this single key
Packit Service 20376f
			 * in between). So we can stop the search.
Packit Service 20376f
			 * Either one of these entries is it (and
Packit Service 20376f
			 * we do not care which), or we do not have
Packit Service 20376f
			 * it.
Packit Service 20376f
			 *
Packit Service 20376f
			 * Furthermore, we know that one of our
Packit Service 20376f
			 * endpoints must be the edge of the run of
Packit Service 20376f
			 * duplicates. For example, given this
Packit Service 20376f
			 * sequence:
Packit Service 20376f
			 *
Packit Service 20376f
			 *     idx 0 1 2 3 4 5
Packit Service 20376f
			 *     key A C C C C D
Packit Service 20376f
			 *
Packit Service 20376f
			 * If we are searching for "B", we might
Packit Service 20376f
			 * hit the duplicate run at lo=1, hi=3
Packit Service 20376f
			 * (e.g., by first mi=3, then mi=0). But we
Packit Service 20376f
			 * can never have lo > 1, because B < C.
Packit Service 20376f
			 * That is, if our key is less than the
Packit Service 20376f
			 * run, we know that "lo" is the edge, but
Packit Service 20376f
			 * we can say nothing of "hi". Similarly,
Packit Service 20376f
			 * if our key is greater than the run, we
Packit Service 20376f
			 * know that "hi" is the edge, but we can
Packit Service 20376f
			 * say nothing of "lo".
Packit Service 20376f
			 *
Packit Service 20376f
			 * Therefore if we do not find it, we also
Packit Service 20376f
			 * know where it would go if it did exist:
Packit Service 20376f
			 * just on the far side of the edge that we
Packit Service 20376f
			 * know about.
Packit Service 20376f
			 */
Packit Service 20376f
			if (ofs == 20) {
Packit Service 20376f
				mi = lo;
Packit Service 20376f
				mi_key = base + elem_size * mi + key_offset;
Packit Service 20376f
				cmp = memcmp(mi_key, key, 20);
Packit Service 20376f
				if (!cmp)
Packit Service 20376f
					return mi;
Packit Service 20376f
				if (cmp < 0)
Packit Service 20376f
					return -1 - hi;
Packit Service 20376f
				else
Packit Service 20376f
					return -1 - lo;
Packit Service 20376f
			}
Packit Service 20376f
Packit Service 20376f
			hiv = hi_key[ofs_0];
Packit Service 20376f
			if (ofs_0 < 19)
Packit Service 20376f
				hiv = (hiv << 8) | hi_key[ofs_0+1];
Packit Service 20376f
		} else {
Packit Service 20376f
			hiv = 256;
Packit Service 20376f
			if (ofs_0 < 19)
Packit Service 20376f
				hiv <<= 8;
Packit Service 20376f
		}
Packit Service 20376f
		lov = lo_key[ofs_0];
Packit Service 20376f
		kyv = key[ofs_0];
Packit Service 20376f
		if (ofs_0 < 19) {
Packit Service 20376f
			lov = (lov << 8) | lo_key[ofs_0+1];
Packit Service 20376f
			kyv = (kyv << 8) | key[ofs_0+1];
Packit Service 20376f
		}
Packit Service 20376f
		assert(lov < hiv);
Packit Service 20376f
Packit Service 20376f
		if (kyv < lov)
Packit Service 20376f
			return -1 - lo;
Packit Service 20376f
		if (hiv < kyv)
Packit Service 20376f
			return -1 - hi;
Packit Service 20376f
Packit Service 20376f
		/*
Packit Service 20376f
		 * Even if we know the target is much closer to 'hi'
Packit Service 20376f
		 * than 'lo', if we pick too precisely and overshoot
Packit Service 20376f
		 * (e.g. when we know 'mi' is closer to 'hi' than to
Packit Service 20376f
		 * 'lo', pick 'mi' that is higher than the target), we
Packit Service 20376f
		 * end up narrowing the search space by a smaller
Packit Service 20376f
		 * amount (i.e. the distance between 'mi' and 'hi')
Packit Service 20376f
		 * than what we would have (i.e. about half of 'lo'
Packit Service 20376f
		 * and 'hi'). Hedge our bets to pick 'mi' less
Packit Service 20376f
		 * aggressively, i.e. make 'mi' a bit closer to the
Packit Service 20376f
		 * middle than we would otherwise pick.
Packit Service 20376f
		 */
Packit Service 20376f
		kyv = (kyv * 6 + lov + hiv) / 8;
Packit Service 20376f
		if (lov < hiv - 1) {
Packit Service 20376f
			if (kyv == lov)
Packit Service 20376f
				kyv++;
Packit Service 20376f
			else if (kyv == hiv)
Packit Service 20376f
				kyv--;
Packit Service 20376f
		}
Packit Service 20376f
		mi = (range - 1) * (kyv - lov) / (hiv - lov) + lo;
Packit Service 20376f
Packit Service 20376f
#ifdef INDEX_DEBUG_LOOKUP
Packit Service 20376f
		printf("lo %u hi %u rg %u mi %u ", lo, hi, range, mi);
Packit Service 20376f
		printf("ofs %u lov %x, hiv %x, kyv %x\n",
Packit Service 20376f
				ofs_0, lov, hiv, kyv);
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
		if (!(lo <= mi && mi < hi)) {
Packit Service 20376f
			giterr_set(GITERR_INVALID, "assertion failure: binary search invariant is false");
Packit Service 20376f
			return -1;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		mi_key = base + elem_size * mi + key_offset;
Packit Service 20376f
		cmp = memcmp(mi_key + ofs_0, key + ofs_0, 20 - ofs_0);
Packit Service 20376f
		if (!cmp)
Packit Service 20376f
			return mi;
Packit Service 20376f
		if (cmp > 0) {
Packit Service 20376f
			hi = mi;
Packit Service 20376f
			hi_key = mi_key;
Packit Service 20376f
		} else {
Packit Service 20376f
			lo = mi + 1;
Packit Service 20376f
			lo_key = mi_key + elem_size;
Packit Service 20376f
		}
Packit Service 20376f
	} while (lo < hi);
Packit Service 20376f
	return -((int)lo)-1;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int sha1_position(const void *table,
Packit Service 20376f
			size_t stride,
Packit Service 20376f
			unsigned lo, unsigned hi,
Packit Service 20376f
			const unsigned char *key)
Packit Service 20376f
{
Packit Service 20376f
	const unsigned char *base = table;
Packit Service 20376f
Packit Service 20376f
	while (lo < hi) {
Packit Service 20376f
		unsigned mi = (lo + hi) / 2;
Packit Service 20376f
		int cmp = git_oid__hashcmp(base + mi * stride, key);
Packit Service 20376f
Packit Service 20376f
		if (!cmp)
Packit Service 20376f
			return mi;
Packit Service 20376f
Packit Service 20376f
		if (cmp > 0)
Packit Service 20376f
			hi = mi;
Packit Service 20376f
		else
Packit Service 20376f
			lo = mi+1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return -((int)lo)-1;
Packit Service 20376f
}