Optimized `repeat` and its tests