fix a performance problem with head-cycle detection
fix a performance problem with head-cycle detection