From 95e9fa5010ac53f35853e8cf06d9b077583febd9 Mon Sep 17 00:00:00 2001 From: Watson Date: Sun, 26 Sep 2021 01:51:56 +0900 Subject: [PATCH] Use `String#=~` and `Regexp.last_match` instead to retrieve the markup content MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If the first value is only used obtained with String#scan, it will increase the performance if replace with `String#=~` and `Regexp.last_match`. ### Environment - MacBook Air (M1, 2020) - macOS 12.0 beta 7 - Apple M1 - Ruby 3.0.2 ### Test code ```ruby require 'benchmark/ips' WhitespaceControl = '-' VariableStart = /\{\{/ VariableEnd = /\}\}/ ContentOfVariable = /\A#{VariableStart}#{WhitespaceControl}?(.*?)#{WhitespaceControl}?#{VariableEnd}\z/om token = "{{item.product.featured_image | product_img_url: 'thumb' }}" Benchmark.ips do |x| x.report("String#scan") { token.scan(ContentOfVariable) {|content| break } } x.report("String#match") { m = token.match(ContentOfVariable); m[1] } x.report("String#=~") { token =~ ContentOfVariable; Regexp.last_match(1) } x.compare! end ``` ### Result ``` Warming up -------------------------------------- String#scan 135.724k i/100ms String#match 117.397k i/100ms String#=~ 151.637k i/100ms Calculating ------------------------------------- String#scan 1.351M (± 0.8%) i/s - 6.786M in 5.021955s String#match 1.169M (± 1.3%) i/s - 5.870M in 5.020429s String#=~ 1.520M (± 0.9%) i/s - 7.733M in 5.087427s Comparison: String#=~: 1520250.9 i/s String#scan: 1351399.0 i/s - 1.12x (± 0.00) slower String#match: 1169384.1 i/s - 1.30x (± 0.00) slower ``` --- lib/liquid/block_body.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/liquid/block_body.rb b/lib/liquid/block_body.rb index 76ab0a85..2921ce88 100644 --- a/lib/liquid/block_body.rb +++ b/lib/liquid/block_body.rb @@ -231,8 +231,8 @@ module Liquid end def create_variable(token, parse_context) - token.scan(ContentOfVariable) do |content| - markup = content.first + if token =~ ContentOfVariable + markup = Regexp.last_match(1) return Variable.new(markup, parse_context) end BlockBody.raise_missing_variable_terminator(token, parse_context)