mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-29 15:15:19 -07:00
Performance improvement for Block parsing. ~ 10% speedup
This commit is contained in:
+8
-4
@@ -1,6 +1,10 @@
|
|||||||
module Liquid
|
module Liquid
|
||||||
|
|
||||||
class Block < Tag
|
class Block < Tag
|
||||||
|
IsTag = /^#{TagStart}/
|
||||||
|
IsVariable = /^#{VariableStart}/
|
||||||
|
FullToken = /^#{TagStart}\s*(\w+)\s*(.*)?#{TagEnd}$/
|
||||||
|
ContentOfVariable = /^#{VariableStart}(.*)#{VariableEnd}$/
|
||||||
|
|
||||||
def parse(tokens)
|
def parse(tokens)
|
||||||
@nodelist ||= []
|
@nodelist ||= []
|
||||||
@@ -9,8 +13,8 @@ module Liquid
|
|||||||
while token = tokens.shift
|
while token = tokens.shift
|
||||||
|
|
||||||
case token
|
case token
|
||||||
when /^#{TagStart}/
|
when IsTag
|
||||||
if token =~ /^#{TagStart}\s*(\w+)\s*(.*)?#{TagEnd}$/
|
if token =~ FullToken
|
||||||
|
|
||||||
# if we found the proper block delimitor just end parsing here and let the outer block
|
# if we found the proper block delimitor just end parsing here and let the outer block
|
||||||
# proceed
|
# proceed
|
||||||
@@ -30,7 +34,7 @@ module Liquid
|
|||||||
else
|
else
|
||||||
raise SyntaxError, "Tag '#{token}' was not properly terminated with regexp: #{TagEnd.inspect} "
|
raise SyntaxError, "Tag '#{token}' was not properly terminated with regexp: #{TagEnd.inspect} "
|
||||||
end
|
end
|
||||||
when /^#{VariableStart}/
|
when IsVariable
|
||||||
@nodelist << create_variable(token)
|
@nodelist << create_variable(token)
|
||||||
when ''
|
when ''
|
||||||
# pass
|
# pass
|
||||||
@@ -68,7 +72,7 @@ module Liquid
|
|||||||
end
|
end
|
||||||
|
|
||||||
def create_variable(token)
|
def create_variable(token)
|
||||||
token.scan(/^#{VariableStart}(.*)#{VariableEnd}$/) do |content|
|
token.scan(ContentOfVariable) do |content|
|
||||||
return Variable.new(content.first)
|
return Variable.new(content.first)
|
||||||
end
|
end
|
||||||
raise SyntaxError.new("Variable '#{token}' was not properly terminated with regexp: #{VariableEnd.inspect} ")
|
raise SyntaxError.new("Variable '#{token}' was not properly terminated with regexp: #{VariableEnd.inspect} ")
|
||||||
|
|||||||
@@ -81,7 +81,7 @@ end
|
|||||||
puts 'Success'
|
puts 'Success'
|
||||||
puts
|
puts
|
||||||
|
|
||||||
[RubyProf::FlatPrinter, RubyProf::GraphPrinter, RubyProf::GraphHtmlPrinter].each do |klass|
|
[RubyProf::FlatPrinter, RubyProf::GraphPrinter, RubyProf::GraphHtmlPrinter, RubyProf::CallTreePrinter].each do |klass|
|
||||||
filename = (ENV['TMP'] || '/tmp') + (klass.name.include?('Html') ? "/liquid.#{klass.name.downcase}.html" : "/liquid.#{klass.name.downcase}.txt")
|
filename = (ENV['TMP'] || '/tmp') + (klass.name.include?('Html') ? "/liquid.#{klass.name.downcase}.html" : "/liquid.#{klass.name.downcase}.txt")
|
||||||
filename.gsub!(/:+/, '_')
|
filename.gsub!(/:+/, '_')
|
||||||
File.open(filename, "w+") { |fp| klass.new(results).print(fp) }
|
File.open(filename, "w+") { |fp| klass.new(results).print(fp) }
|
||||||
|
|||||||
Reference in New Issue
Block a user