optimize loop performance