Improve performance of search when the query is in the cache.

As discussed in the perf_test pull request.