optimize key_pairs in recordDistances