From aa45356133d395089785d20a369024e319ce592f Mon Sep 17 00:00:00 2001 From: Ian Ker-Seymer Date: Thu, 24 Oct 2024 22:45:12 -0400 Subject: [PATCH] Avoid method dispatch --- Gemfile | 2 ++ Rakefile | 5 ++++- lib/liquid/tokenizer.rb | 13 ++++++------- performance/benchmark.rb | 8 ++++---- 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/Gemfile b/Gemfile index 509316c1..b050ec6e 100644 --- a/Gemfile +++ b/Gemfile @@ -28,3 +28,5 @@ group :test do gem 'liquid-c', github: 'Shopify/liquid-c', ref: 'main' end end + +gem "strscan", ">= 3.1.1" diff --git a/Rakefile b/Rakefile index 82588eda..107be1f0 100755 --- a/Rakefile +++ b/Rakefile @@ -73,7 +73,7 @@ end namespace :benchmark do desc "Run the liquid benchmark with lax parsing" - task :run do + task :lax do ruby "./performance/benchmark.rb lax" end @@ -82,6 +82,9 @@ namespace :benchmark do ruby "./performance/benchmark.rb strict" end + desc "Run the liquid benchmark with both lax and strict parsing" + task run: [:lax, :strict] + desc "Run unit benchmarks" task :unit do Dir["./performance/unit/*_benchmark.rb"].each do |file| diff --git a/lib/liquid/tokenizer.rb b/lib/liquid/tokenizer.rb index 1991102f..1aa4808a 100644 --- a/lib/liquid/tokenizer.rb +++ b/lib/liquid/tokenizer.rb @@ -17,7 +17,8 @@ module Liquid def initialize(source, line_numbers = false, line_number: nil, for_liquid_tag: false) @line_number = line_number || (line_numbers ? 1 : nil) @for_liquid_tag = for_liquid_tag - @ss = StringScanner.new(source) + @source = source + @ss = StringScanner.new(source) end def shift @@ -78,7 +79,7 @@ module Liquid end @ss.pos -= 2 - @ss.string.byteslice(start, @ss.pos - start) + @source.byteslice(start, @ss.pos - start) end def next_variable_token @@ -88,9 +89,7 @@ module Liquid byte_a = @ss.scan_byte until @ss.eos? - while @ss.eos? == false && byte_a != CLOSE_CURLEY && byte_a != OPEN_CURLEY - byte_a = @ss.scan_byte - end + byte_a = @ss.scan_byte while @ss.eos? == false && byte_a != CLOSE_CURLEY && byte_a != OPEN_CURLEY break if @ss.eos? @@ -108,7 +107,7 @@ module Liquid end end - return "{{" + "{{" end def next_tag_token(start = nil) @@ -116,7 +115,7 @@ module Liquid @ss.scan_until(TAG_END) - @ss.string.byteslice(start, @ss.pos - start) + @source.byteslice(start, @ss.pos - start) end end end diff --git a/performance/benchmark.rb b/performance/benchmark.rb index da31aa0c..f1c5ebb8 100644 --- a/performance/benchmark.rb +++ b/performance/benchmark.rb @@ -8,14 +8,14 @@ Liquid::Template.error_mode = ARGV.first.to_sym if ARGV.first profiler = ThemeRunner.new Benchmark.ips do |x| - x.time = 10 - x.warmup = 5 + x.time = 20 + x.warmup = 10 puts puts "Running benchmark for #{x.time} seconds (with #{x.warmup} seconds warmup)." puts x.report("parse:") { profiler.compile } - x.report("render:") { profiler.render } - x.report("parse & render:") { profiler.run } + # x.report("render:") { profiler.render } + # x.report("parse & render:") { profiler.run } end