Optimizing doctrine listener to avoid 2*n*n computations.