Performance tweaks for cosine similarity.