Suggestions needed: speed up optimization

This will allocate an array on the heap each time it is called, which is bad for performance.