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