Files
liquid/lib
Aaron Patterson 8bf0e7dfae Reduce allocations in truncatewords
I don't know if this is a bottleneck IRL, but while doing some
performance testing I noticed that this method allocates a bunch of
objects and I decided to fix it.

Here is the benchmark I used:

```ruby
require_relative 'theme_runner'

Liquid::Template.error_mode = ARGV.first.to_sym if ARGV.first
profiler = ThemeRunner.new

def count_alloc
  before = GC.stat(:total_allocated_objects)
  yield
  GC.stat(:total_allocated_objects) - before
end

profiler.render # heat
p count_alloc { profiler.render }
p count_alloc { profiler.render }
p count_alloc { profiler.render }
```

Before this change:

```
[aaron@tc-lan-adapter ~/g/liquid (master)]$ ruby -I lib performance/run_once.rb
15753
15750
15752
```

After this change:

```
[aaron@tc-lan-adapter ~/g/liquid (master)]$ ruby -I lib performance/run_once.rb
14015
14010
14011
```

About an 11% reduction in allocations for this test.  I also added some
tests around the current behavior of the method so we can be more sure
the replacement behaves the same way
2020-10-09 16:01:12 -07:00
..