[Sheepdog] [PATCH v3 2/2] use binary search in hval_to_sheep()

Christoph Hellwig hch at infradead.org
Mon May 14 18:48:09 CEST 2012


> +			int nr_entries, uint64_t id)
> +{
> +	int start, end, pos;
> +
> +	start = 0;
> +	end = nr_entries - 1;
> +
> +	if (id > entries[end].id)
> +		return end;
> +again:
> +	pos = (end - start) / 2 + start;
> +	if (entries[pos].id < id) {
> +		if (entries[pos + 1].id >= id)
> +			return pos;
> +		start = pos;
> +	} else
> +		end = pos;
> +	goto again;

This would be a lot more readable as a endless for loop, e.g.

	for (;;) {
		pos = (end - start) / 2 + start;
		if (entries[pos].id < id) {
			if (entries[pos + 1].id >= id)
				return pos;
			start = pos;
		} else {
			end = pos;
		}
	}




More information about the sheepdog mailing list