Improve reduce() performance with generic iterators