Avoid explictly freezing literals strings when possible

Ref: https://github.com/jeremyevans/erubi/pull/33

If the template is compiled with `frozen_string_literals: true`,
then explicitly freezing string is slightly wasteful as it will be
compiled as `opt_str_freeze` instead of a simple `putobject`.

The former has to check wether `String#freeze` was redefined every
time, which while fast is useless extra work.
This commit is contained in:
Jean Boussier 2022-07-28 11:40:55 +02:00
parent 887dc9af6c
commit 476aeda794
3 changed files with 23 additions and 18 deletions

View File

@ -53,7 +53,7 @@ PATH
actionview (7.1.0.alpha) actionview (7.1.0.alpha)
activesupport (= 7.1.0.alpha) activesupport (= 7.1.0.alpha)
builder (~> 3.1) builder (~> 3.1)
erubi (~> 1.4) erubi (~> 1.11)
rails-dom-testing (~> 2.0) rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.1, >= 1.2.0) rails-html-sanitizer (~> 1.1, >= 1.2.0)
activejob (7.1.0.alpha) activejob (7.1.0.alpha)
@ -205,7 +205,7 @@ GEM
http_parser.rb (>= 0.6.0) http_parser.rb (>= 0.6.0)
em-socksify (0.3.2) em-socksify (0.3.2)
eventmachine (>= 1.0.0.beta.4) eventmachine (>= 1.0.0.beta.4)
erubi (1.10.0) erubi (1.11.0)
et-orbi (1.2.6) et-orbi (1.2.6)
tzinfo tzinfo
event_emitter (0.2.6) event_emitter (0.2.6)

View File

@ -36,7 +36,7 @@ Gem::Specification.new do |s|
s.add_dependency "activesupport", version s.add_dependency "activesupport", version
s.add_dependency "builder", "~> 3.1" s.add_dependency "builder", "~> 3.1"
s.add_dependency "erubi", "~> 1.4" s.add_dependency "erubi", "~> 1.11"
s.add_dependency "rails-html-sanitizer", "~> 1.1", ">= 1.2.0" s.add_dependency "rails-html-sanitizer", "~> 1.1", ">= 1.2.0"
s.add_dependency "rails-dom-testing", "~> 2.0" s.add_dependency "rails-dom-testing", "~> 2.0"

View File

@ -18,6 +18,9 @@ module ActionView
properties[:preamble] ||= "" properties[:preamble] ||= ""
properties[:postamble] ||= "#{properties[:bufvar]}.to_s" properties[:postamble] ||= "#{properties[:bufvar]}.to_s"
# Tell Eruby that whether template will be compiled with `frozen_string_literal: true`
properties[:freeze_template_literals] = !Template.frozen_string_literal
properties[:escapefunc] = "" properties[:escapefunc] = ""
super super
@ -30,11 +33,11 @@ module ActionView
if text == "\n" if text == "\n"
@newline_pending += 1 @newline_pending += 1
else else
src << bufvar << ".safe_append='" with_buffer do
src << "\n" * @newline_pending if @newline_pending > 0 src << ".safe_append='"
src << text.gsub(/['\\]/, '\\\\\&') src << "\n" * @newline_pending if @newline_pending > 0
src << "'.freeze;" src << text.gsub(/['\\]/, '\\\\\&') << @text_end
end
@newline_pending = 0 @newline_pending = 0
end end
end end
@ -44,16 +47,18 @@ module ActionView
def add_expression(indicator, code) def add_expression(indicator, code)
flush_newline_if_pending(src) flush_newline_if_pending(src)
if (indicator == "==") || @escape with_buffer do
src << bufvar << ".safe_expr_append=" if (indicator == "==") || @escape
else src << ".safe_expr_append="
src << bufvar << ".append=" else
end src << ".append="
end
if BLOCK_EXPR.match?(code) if BLOCK_EXPR.match?(code)
src << " " << code src << " " << code
else else
src << "(" << code << ");" src << "(" << code << ")"
end
end end
end end
@ -69,7 +74,7 @@ module ActionView
def flush_newline_if_pending(src) def flush_newline_if_pending(src)
if @newline_pending > 0 if @newline_pending > 0
src << bufvar << ".safe_append='#{"\n" * @newline_pending}'.freeze;" with_buffer { src << ".safe_append='#{"\n" * @newline_pending}" << @text_end }
@newline_pending = 0 @newline_pending = 0
end end
end end