Avoid method dispatch

This commit is contained in:
Ian Ker-Seymer
2024-10-30 13:54:01 -03:00
committed by Michael Go
parent a406603e9f
commit aa45356133
4 changed files with 16 additions and 12 deletions
+2
View File
@@ -28,3 +28,5 @@ group :test do
gem 'liquid-c', github: 'Shopify/liquid-c', ref: 'main' gem 'liquid-c', github: 'Shopify/liquid-c', ref: 'main'
end end
end end
gem "strscan", ">= 3.1.1"
+4 -1
View File
@@ -73,7 +73,7 @@ end
namespace :benchmark do namespace :benchmark do
desc "Run the liquid benchmark with lax parsing" desc "Run the liquid benchmark with lax parsing"
task :run do task :lax do
ruby "./performance/benchmark.rb lax" ruby "./performance/benchmark.rb lax"
end end
@@ -82,6 +82,9 @@ namespace :benchmark do
ruby "./performance/benchmark.rb strict" ruby "./performance/benchmark.rb strict"
end end
desc "Run the liquid benchmark with both lax and strict parsing"
task run: [:lax, :strict]
desc "Run unit benchmarks" desc "Run unit benchmarks"
task :unit do task :unit do
Dir["./performance/unit/*_benchmark.rb"].each do |file| Dir["./performance/unit/*_benchmark.rb"].each do |file|
+5 -6
View File
@@ -17,6 +17,7 @@ module Liquid
def initialize(source, line_numbers = false, line_number: nil, for_liquid_tag: false) def initialize(source, line_numbers = false, line_number: nil, for_liquid_tag: false)
@line_number = line_number || (line_numbers ? 1 : nil) @line_number = line_number || (line_numbers ? 1 : nil)
@for_liquid_tag = for_liquid_tag @for_liquid_tag = for_liquid_tag
@source = source
@ss = StringScanner.new(source) @ss = StringScanner.new(source)
end end
@@ -78,7 +79,7 @@ module Liquid
end end
@ss.pos -= 2 @ss.pos -= 2
@ss.string.byteslice(start, @ss.pos - start) @source.byteslice(start, @ss.pos - start)
end end
def next_variable_token def next_variable_token
@@ -88,9 +89,7 @@ module Liquid
byte_a = @ss.scan_byte byte_a = @ss.scan_byte
until @ss.eos? until @ss.eos?
while @ss.eos? == false && byte_a != CLOSE_CURLEY && byte_a != OPEN_CURLEY byte_a = @ss.scan_byte while @ss.eos? == false && byte_a != CLOSE_CURLEY && byte_a != OPEN_CURLEY
byte_a = @ss.scan_byte
end
break if @ss.eos? break if @ss.eos?
@@ -108,7 +107,7 @@ module Liquid
end end
end end
return "{{" "{{"
end end
def next_tag_token(start = nil) def next_tag_token(start = nil)
@@ -116,7 +115,7 @@ module Liquid
@ss.scan_until(TAG_END) @ss.scan_until(TAG_END)
@ss.string.byteslice(start, @ss.pos - start) @source.byteslice(start, @ss.pos - start)
end end
end end
end end
+4 -4
View File
@@ -8,14 +8,14 @@ Liquid::Template.error_mode = ARGV.first.to_sym if ARGV.first
profiler = ThemeRunner.new profiler = ThemeRunner.new
Benchmark.ips do |x| Benchmark.ips do |x|
x.time = 10 x.time = 20
x.warmup = 5 x.warmup = 10
puts puts
puts "Running benchmark for #{x.time} seconds (with #{x.warmup} seconds warmup)." puts "Running benchmark for #{x.time} seconds (with #{x.warmup} seconds warmup)."
puts puts
x.report("parse:") { profiler.compile } x.report("parse:") { profiler.compile }
x.report("render:") { profiler.render } # x.report("render:") { profiler.render }
x.report("parse & render:") { profiler.run } # x.report("parse & render:") { profiler.run }
end end