-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fdd8c71
commit a6eef1c
Showing
9 changed files
with
318 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# frozen_string_literal: true | ||
|
||
module Liquid | ||
class Loom | ||
class << self | ||
def optimize(template) | ||
new(template).optimize | ||
end | ||
end | ||
|
||
def initialize template | ||
@root = template.root | ||
end | ||
|
||
def optimize | ||
merge_if_blocks | ||
end | ||
|
||
def merge_if_blocks | ||
nodelist_list = [@root.nodelist] | ||
|
||
while nodelist_list.any? | ||
next_nodelist_list = [] | ||
|
||
nodelist_list.each do |nodelist| | ||
i = 0 | ||
while i < nodelist.length | ||
node = nodelist[i] | ||
chain_if_blocks(nodelist, node, i) if node.is_a?(If) | ||
i += 1 | ||
end | ||
end | ||
|
||
nodelist_list = next_nodelist_list | ||
end | ||
end | ||
|
||
private | ||
|
||
def chain_if_blocks(nodelist, first_if_node, first_if_index) | ||
used_variables = Set.new | ||
|
||
# only check the top level Condition (ignore children conditions for now) | ||
first_if_node.blocks.each do |condition| | ||
used_variables << condition.left | ||
used_variables << condition.right if condition.right | ||
end | ||
|
||
if_blocks = [] | ||
|
||
nodelist[first_if_index + 1..-1].each do |node| | ||
break unless node.is_a?(If) | ||
|
||
# check if the variables used in the current block are used in the previous block | ||
first_if_node.blocks.each do |condition| | ||
if used_variables.include?(condition.left) || (condition.right && used_variables.include?(condition.right)) | ||
break | ||
end | ||
end | ||
|
||
if_blocks << node | ||
end | ||
|
||
nodelist.delete_if { |node| if_blocks.include?(node) } | ||
|
||
if_blocks.each do |if_block| | ||
first_if_node.blocks << if_block.blocks.first | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# frozen_string_literal: true | ||
|
||
require "benchmark/ips" | ||
require 'liquid' | ||
|
||
RubyVM::YJIT.enable | ||
|
||
TEMPLATE = <<~LIQUID | ||
{% if false %} | ||
{% for i in (1..1000000) %} | ||
{{ "Hello world!" }} | ||
{% endfor %} | ||
{% endif %} | ||
{% assign result = 1 %} | ||
{% if foo == 1 %}{% assign result = 1 %}{% endif %}{% if foo == 2 %}{% assign result = 2 %}{% endif %}{% if foo == 3 %}{% assign result = 3 %}{% endif %} | ||
Result: {{ result }} | ||
{% for i in (1..1000000) %} | ||
{% endfor %} | ||
{% for i in (1..1000000) %} | ||
{% endfor %} | ||
{% for i in (1..1000000) %} | ||
{% endfor %} | ||
{% for i in (1..1000000) %} | ||
{% endfor %} | ||
LIQUID | ||
|
||
baseline_template = Liquid::Template.parse(TEMPLATE, eager_optimize: false) | ||
optimized_template = Liquid::Template.parse(TEMPLATE, eager_optimize: true) | ||
|
||
[nil, 1, 2, 3].each do |foo| | ||
baseline_output = baseline_template.render('foo' => foo) | ||
optimized_output = optimized_template.render('foo' => foo) | ||
|
||
if baseline_output != optimized_output | ||
puts "WARNING! Baseline and optimized templates render differently for foo=#{foo}" | ||
puts "Baseline: #{baseline_output}" | ||
puts "Optimized: #{optimized_output}" | ||
|
||
raise | ||
end | ||
end | ||
|
||
def render(template, foo) | ||
template.render('foo' => foo) | ||
end | ||
|
||
Benchmark.ips do |x| | ||
x.config(time: 20, warmup: 3) | ||
|
||
x.report("baseline") do | ||
[nil, 1, 2, 3].each do |foo| | ||
render(baseline_template, foo) | ||
end | ||
end | ||
|
||
x.report("optimized") do | ||
[nil, 1, 2, 3].each do |foo| | ||
render(optimized_template, foo) | ||
end | ||
end | ||
|
||
x.compare! | ||
end |
Oops, something went wrong.