Improve performance of concatenation

When invoked with a large number of intermediate results, concatenation
was performing badly. This patch should fix such behavior.