Optimize range iteration

For loops support a range syntax for iterating a fixed number of times,
which looks like this.

```liquid
{% for i in range (1..100) %}
        ...
{% endfor %}
```

Previously, we converted these ranges to arrays using `to_a`, which
initialized an array containing each number in the range. Since all we
use these ranges for is iteration, this is far less efficient than
using a range iterator.

Doing this means that iterating over ranges now takes O(1) rather than O(n)
memory. See the PR for more benchmarks.

* Remove to_a cast on ranges
* Add ReversableRange iterator
* Add custom range-specific slicing logic
This commit is contained in:
Samuel
2019-07-09 10:57:39 -04:00
parent b3b63a683f
commit fd5e3e87c7
9 changed files with 208 additions and 9 deletions
+3 -3
View File
@@ -349,9 +349,9 @@ class ContextUnitTest < Minitest::Test
def test_ranges
@context.merge("test" => '5')
assert_equal (1..5), @context['(1..5)']
assert_equal (1..5), @context['(1..test)']
assert_equal (5..5), @context['(test..test)']
assert_equal ReversableRange.new(1, 5), @context['(1..5)']
assert_equal ReversableRange.new(1, 5), @context['(1..test)']
assert_equal ReversableRange.new(5, 5), @context['(test..test)']
end
def test_cents_through_drop_nestedly