Improve performance of segment generation