Replace linear array search with binary search.

For large keysets this should be much more efficient, because we're
reducing an O(n**2) algorithm down to O(n*logn)