Optimize compute_ranks() method