mirror of
https://github.com/kemko/liquid.git
synced 2026-01-02 08:15:41 +03:00
Compare commits
1 Commits
parse_part
...
benchmark-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d7234cb346 |
@@ -30,11 +30,6 @@ module Liquid
|
|||||||
@attributes[key] = value
|
@attributes[key] = value
|
||||||
end
|
end
|
||||||
|
|
||||||
if partial_parsable_at_parse_time?
|
|
||||||
source = read_template_from_file_system_at_parse
|
|
||||||
@partial = Liquid::Template.parse(source, pass_options)
|
|
||||||
end
|
|
||||||
|
|
||||||
else
|
else
|
||||||
raise SyntaxError.new(options[:locale].t("errors.syntax.include".freeze))
|
raise SyntaxError.new(options[:locale].t("errors.syntax.include".freeze))
|
||||||
end
|
end
|
||||||
@@ -71,21 +66,17 @@ module Liquid
|
|||||||
template_name = context[@template_name]
|
template_name = context[@template_name]
|
||||||
|
|
||||||
if cached = cached_partials[template_name]
|
if cached = cached_partials[template_name]
|
||||||
cached
|
return cached
|
||||||
else
|
|
||||||
if @partial && context.registers[:file_system].nil?
|
|
||||||
partial = @partial
|
|
||||||
else
|
|
||||||
partial = Liquid::Template.parse(read_template_from_file_system(context), pass_options)
|
|
||||||
end
|
|
||||||
cached_partials[template_name] = partial
|
|
||||||
context.registers[:cached_partials] = cached_partials
|
|
||||||
partial
|
|
||||||
end
|
end
|
||||||
|
source = read_template_from_file_system(context)
|
||||||
|
partial = Liquid::Template.parse(source, pass_options)
|
||||||
|
cached_partials[template_name] = partial
|
||||||
|
context.registers[:cached_partials] = cached_partials
|
||||||
|
partial
|
||||||
end
|
end
|
||||||
|
|
||||||
def read_template_from_file_system(context)
|
def read_template_from_file_system(context)
|
||||||
file_system = context.registers[:file_system] || parsed_file_system || Liquid::Template.file_system
|
file_system = context.registers[:file_system] || Liquid::Template.file_system
|
||||||
|
|
||||||
# make read_template_file call backwards-compatible.
|
# make read_template_file call backwards-compatible.
|
||||||
case file_system.method(:read_template_file).arity
|
case file_system.method(:read_template_file).arity
|
||||||
@@ -98,23 +89,6 @@ module Liquid
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def read_template_from_file_system_at_parse
|
|
||||||
parsed_file_system.read_template_file(parsed_template_name)
|
|
||||||
end
|
|
||||||
|
|
||||||
def parsed_file_system
|
|
||||||
options[:file_system]
|
|
||||||
end
|
|
||||||
|
|
||||||
def partial_parsable_at_parse_time?
|
|
||||||
template_name_is_string_constant = parsed_template_name.is_a?(String)
|
|
||||||
options[:file_system] && template_name_is_string_constant
|
|
||||||
end
|
|
||||||
|
|
||||||
def parsed_template_name
|
|
||||||
Expression.parse(@template_name)
|
|
||||||
end
|
|
||||||
|
|
||||||
def pass_options
|
def pass_options
|
||||||
dont_pass = @options[:include_options_blacklist]
|
dont_pass = @options[:include_options_blacklist]
|
||||||
return {locale: @options[:locale]} if dont_pass == true
|
return {locale: @options[:locale]} if dont_pass == true
|
||||||
|
|||||||
@@ -1,17 +1,14 @@
|
|||||||
require 'benchmark/ips'
|
require 'benchmark'
|
||||||
require File.dirname(__FILE__) + '/theme_runner'
|
require File.dirname(__FILE__) + '/theme_runner'
|
||||||
|
|
||||||
Liquid::Template.error_mode = ARGV.first.to_sym if ARGV.first
|
Liquid::Template.error_mode = ARGV.first.to_sym if ARGV.first
|
||||||
profiler = ThemeRunner.new
|
profiler = ThemeRunner.new
|
||||||
|
|
||||||
Benchmark.ips do |x|
|
N = 100
|
||||||
x.time = 60
|
Benchmark.bmbm do |x|
|
||||||
x.warmup = 5
|
x.report("parse:") { N.times { profiler.parse } }
|
||||||
|
x.report("marshal load:") { N.times { profiler.marshal_load } }
|
||||||
puts
|
x.report("render:") { N.times { profiler.render } }
|
||||||
puts "Running benchmark for #{x.time} seconds (with #{x.warmup} seconds warmup)."
|
x.report("marshal load & render:") { N.times { profiler.load_and_render } }
|
||||||
puts
|
x.report("parse & render:") { N.times { profiler.parse_and_render } }
|
||||||
|
|
||||||
x.report("parse:") { profiler.compile }
|
|
||||||
x.report("parse & run:") { profiler.run }
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -3,13 +3,13 @@ require File.dirname(__FILE__) + '/theme_runner'
|
|||||||
|
|
||||||
Liquid::Template.error_mode = ARGV.first.to_sym if ARGV.first
|
Liquid::Template.error_mode = ARGV.first.to_sym if ARGV.first
|
||||||
profiler = ThemeRunner.new
|
profiler = ThemeRunner.new
|
||||||
profiler.run
|
profiler.parse_and_render
|
||||||
|
|
||||||
[:cpu, :object].each do |profile_type|
|
[:cpu, :object].each do |profile_type|
|
||||||
puts "Profiling in #{profile_type.to_s} mode..."
|
puts "Profiling in #{profile_type.to_s} mode..."
|
||||||
results = StackProf.run(mode: profile_type) do
|
results = StackProf.run(mode: profile_type) do
|
||||||
100.times do
|
100.times do
|
||||||
profiler.run
|
profiler.parse_and_render
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
StackProf::Report.new(results).print_text(false, 20)
|
StackProf::Report.new(results).print_text(false, 20)
|
||||||
|
|||||||
@@ -32,45 +32,59 @@ class ThemeRunner
|
|||||||
|
|
||||||
[File.read(test), (File.file?(theme_path) ? File.read(theme_path) : nil), test]
|
[File.read(test), (File.file?(theme_path) ? File.read(theme_path) : nil), test]
|
||||||
end.compact
|
end.compact
|
||||||
end
|
@parsed = @tests.map do |liquid, layout, template_name|
|
||||||
|
[Liquid::Template.parse(liquid), Liquid::Template.parse(layout), template_name]
|
||||||
def compile
|
end
|
||||||
# Dup assigns because will make some changes to them
|
@marshaled = @parsed.map do |liquid, layout, template_name|
|
||||||
|
[Marshal.dump(liquid), Marshal.dump(layout), template_name]
|
||||||
@tests.each do |liquid, layout, template_name|
|
|
||||||
|
|
||||||
tmpl = Liquid::Template.new
|
|
||||||
tmpl.parse(liquid)
|
|
||||||
tmpl = Liquid::Template.new
|
|
||||||
tmpl.parse(layout)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def run
|
def parse
|
||||||
|
@tests.each do |liquid, layout, template_name|
|
||||||
|
Liquid::Template.parse(liquid)
|
||||||
|
Liquid::Template.parse(layout)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def marshal_load
|
||||||
|
@marshaled.each do |liquid, layout, template_name|
|
||||||
|
Marshal.load(liquid)
|
||||||
|
Marshal.load(layout)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def render
|
||||||
|
@parsed.each do |liquid, layout, template_name|
|
||||||
|
render_once(liquid, layout, template_name)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def load_and_render
|
||||||
|
@marshaled.each do |liquid, layout, template_name|
|
||||||
|
render_once(Marshal.load(liquid), Marshal.load(layout), template_name)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def parse_and_render
|
||||||
|
@tests.each do |liquid, layout, template_name|
|
||||||
|
render_once(Liquid::Template.parse(liquid), Liquid::Template.parse(layout), template_name)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def render_once(template, layout, template_name)
|
||||||
# Dup assigns because will make some changes to them
|
# Dup assigns because will make some changes to them
|
||||||
assigns = Database.tables.dup
|
assigns = Database.tables.dup
|
||||||
|
|
||||||
@tests.each do |liquid, layout, template_name|
|
assigns['page_title'] = 'Page title'
|
||||||
|
assigns['template'] = File.basename(template_name, File.extname(template_name))
|
||||||
|
template.registers[:file_system] = ThemeRunner::FileSystem.new(File.dirname(template_name))
|
||||||
|
|
||||||
# Compute page_tempalte outside of profiler run, uninteresting to profiler
|
content_for_layout = template.render!(assigns)
|
||||||
page_template = File.basename(template_name, File.extname(template_name))
|
|
||||||
compile_and_render(liquid, layout, assigns, page_template, template_name)
|
|
||||||
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
def compile_and_render(template, layout, assigns, page_template, template_file)
|
|
||||||
tmpl = Liquid::Template.new
|
|
||||||
tmpl.assigns['page_title'] = 'Page title'
|
|
||||||
tmpl.assigns['template'] = page_template
|
|
||||||
tmpl.registers[:file_system] = ThemeRunner::FileSystem.new(File.dirname(template_file))
|
|
||||||
|
|
||||||
content_for_layout = tmpl.parse(template).render!(assigns)
|
|
||||||
|
|
||||||
if layout
|
if layout
|
||||||
assigns['content_for_layout'] = content_for_layout
|
assigns['content_for_layout'] = content_for_layout
|
||||||
tmpl.parse(layout).render!(assigns)
|
layout.render!(assigns)
|
||||||
else
|
else
|
||||||
content_for_layout
|
content_for_layout
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -33,13 +33,6 @@ class TestFileSystem
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# FileSystems at parse time don't have a context
|
|
||||||
class ParseFileSystem
|
|
||||||
def read_template_file(template_path)
|
|
||||||
'from ParseFileSystem'
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
class OtherFileSystem
|
class OtherFileSystem
|
||||||
def read_template_file(template_path, context)
|
def read_template_file(template_path, context)
|
||||||
'from OtherFileSystem'
|
'from OtherFileSystem'
|
||||||
@@ -84,20 +77,6 @@ class IncludeTagTest < Minitest::Test
|
|||||||
Template.parse("{% include 'pick_a_source' %}").render!({}, :registers => {:file_system => OtherFileSystem.new})
|
Template.parse("{% include 'pick_a_source' %}").render!({}, :registers => {:file_system => OtherFileSystem.new})
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_include_tag_can_use_file_system_at_parse_so_it_can_be_parsed_before_rendered
|
|
||||||
assert_equal 'from ParseFileSystem',
|
|
||||||
Template.parse("{% include 'pick_a_source' %}",file_system: ParseFileSystem.new).render!({})
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_include_tag_looks_at_file_system_passed_in_registers_over_parse_options
|
|
||||||
assert_equal 'from OtherFileSystem',
|
|
||||||
Template.parse("{% include 'pick_a_source' %}",file_system: ParseFileSystem.new).render!({}, :registers => {:file_system => OtherFileSystem.new})
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_include_tag_use_parse_option_file_system_even_if_partial_can_be_preparsed
|
|
||||||
assert_equal 'from ParseFileSystem',
|
|
||||||
Template.parse("{% include template %}",file_system: ParseFileSystem.new).render!({})
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_include_tag_with
|
def test_include_tag_with
|
||||||
assert_template_result "Product: Draft 151cm ",
|
assert_template_result "Product: Draft 151cm ",
|
||||||
|
|||||||
Reference in New Issue
Block a user