Shopify/liquid — pull request #319 by dylanahsmith (2014-03-18 17:13:12+00:00)
Title: Append to output string in render rather than returning a string.

@fw42 & @arthurnn for review

## Problem

I noticed from `bundle exec rake profile:run` that a significant amount of CPU time was spent doing `output.join` at the end of Liquid::Block#render_all.

```
248 (13.3%) / 248 (13.3%) | 153 | output.join
```

## Solution

I think part of the problem is that we are joining at multiple levels to render the inner blocks, then join the inner block strings in outer blocks.

Instead, we can just append to a single output string, and prevent some additional strings and array objects from being created. The benchmarks show that this is a significant improvement to render time.

However, this does require a change to custom liquid tags to append to an output string rather than return a rendered string. We should probably make a release before this gets merged, then make the next release a major version bump, which can allow us to do additional refactoring and performance improvements.

## Benchmark

before:

```
/Users/dylansmith/.rbenv/versions/2.1.0-github/bin/ruby ./performance/benchmark.rb lax
Rehearsal ------------------------------------------------
parse: 4.430000 0.060000 4.490000 ( 4.496172)
parse & run: 9.180000 0.050000 9.230000 ( 9.222556)
-------------------------------------- total: 13.720000sec

user system total real
parse: 4.440000 0.040000 4.480000 ( 4.480196)
parse & run: 9.380000 0.050000 9.430000 ( 9.425905)
```

after:

```
dylansmith:~/src/liquid(render-concat-output-string)$ be rake benchmark:run
/Users/dylansmith/.rbenv/versions/2.1.0-github/bin/ruby ./performance/benchmark.rb lax
Rehearsal ------------------------------------------------
parse: 4.330000 0.050000 4.380000 ( 4.372873)
parse & run: 8.310000 0.030000 8.340000 ( 8.341584)
-------------------------------------- total: 12.720000sec

user system total real
parse: 4.250000 0.030000 4.280000 ( 4.274610)
parse & run: 8.240000 0.040000 8.280000 ( 8.280087)
```