Make loop discovery O(M+N) instead of O(M*N) stat() operations.