Avoid extra string allocation in the methods generated by eval

This commit is contained in:
Ryuta Kamizono 2020-02-27 18:26:50 +09:00
parent d5be4f1a46
commit 1ef8c60dfc
11 changed files with 16 additions and 7 deletions

View File

@ -66,10 +66,10 @@ module AbstractController
methods.each do |method|
_helpers.class_eval <<-ruby_eval, file, line
def #{method}(*args, &blk) # def current_user(*args, &blk)
controller.send(%(#{method}), *args, &blk) # controller.send(:current_user, *args, &blk)
end # end
ruby2_keywords(%(#{method})) if respond_to?(:ruby2_keywords, true)
def #{method}(*args, &block) # def current_user(*args, &block)
controller.send(:'#{method}', *args, &block) # controller.send(:'current_user', *args, &block)
end # end
ruby2_keywords(:'#{method}') if respond_to?(:ruby2_keywords, true)
ruby_eval
end
end

View File

@ -49,8 +49,9 @@ module ActionDispatch
# See https://github.com/rack/rack/commit/c173b188d81ee437b588c1e046a1c9f031dea550
ENV_METHODS.each do |env|
class_eval <<-METHOD, __FILE__, __LINE__ + 1
# frozen_string_literal: true
def #{env.sub(/^HTTP_/n, '').downcase} # def accept_charset
get_header "#{env}".freeze # get_header "HTTP_ACCEPT_CHARSET".freeze
get_header "#{env}" # get_header "HTTP_ACCEPT_CHARSET"
end # end
METHOD
end

View File

@ -13,6 +13,7 @@ module ActionDispatch
VERBS = %w{ DELETE GET HEAD OPTIONS LINK PATCH POST PUT TRACE UNLINK }
VERBS.each do |v|
class_eval <<-eoc, __FILE__, __LINE__ + 1
# frozen_string_literal: true
class #{v}
def self.verb; name.split("::").last; end
def self.call(req); req.#{v.downcase}?; end

View File

@ -145,6 +145,7 @@ module ActionDispatch
%w(edit new).each do |action|
module_eval <<-EOT, __FILE__, __LINE__ + 1
# frozen_string_literal: true
def #{action}_polymorphic_url(record_or_hash, options = {})
polymorphic_url_for_action("#{action}", record_or_hash, options)
end

View File

@ -1905,7 +1905,7 @@ module ActionView
class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
def #{selector}(method, options = {}) # def text_field(method, options = {})
@template.send( # @template.send(
#{selector.inspect}, # "text_field",
#{selector.inspect}, # :text_field,
@object_name, # @object_name,
method, # method,
objectify_options(options)) # objectify_options(options))

View File

@ -321,6 +321,7 @@ module ActionView
end
class_eval <<-RUBY, __FILE__, __LINE__ + 1
# frozen_string_literal: true
def _layout(lookup_context, formats)
if _conditional_layout?
#{layout_definition}

View File

@ -495,7 +495,7 @@ module ActiveModel
def self.define_attribute_accessor_method(mod, attr_name, writer: false)
method_name = "#{attr_name}#{'=' if writer}"
if attr_name.ascii_only? && DEF_SAFE_NAME.match?(attr_name)
yield method_name, "'#{attr_name}'.freeze"
yield method_name, "'#{attr_name}'"
else
safe_name = attr_name.unpack1("h*")
const_name = "ATTR_#{safe_name}"

View File

@ -47,6 +47,7 @@ module ActiveModel
generated_attribute_methods, name, writer: true,
) do |temp_method_name, attr_name_expr|
generated_attribute_methods.module_eval <<-RUBY, __FILE__, __LINE__ + 1
# frozen_string_literal: true
def #{temp_method_name}(value)
name = #{attr_name_expr}
write_attribute(name, value)

View File

@ -12,6 +12,7 @@ module ActiveRecord
generated_attribute_methods, name
) do |temp_method_name, attr_name_expr|
generated_attribute_methods.module_eval <<-RUBY, __FILE__, __LINE__ + 1
# frozen_string_literal: true
def #{temp_method_name}
name = #{attr_name_expr}
_read_attribute(name) { |n| missing_attribute(n, caller) }

View File

@ -16,6 +16,7 @@ module ActiveRecord
generated_attribute_methods, name, writer: true,
) do |temp_method_name, attr_name_expr|
generated_attribute_methods.module_eval <<-RUBY, __FILE__, __LINE__ + 1
# frozen_string_literal: true
def #{temp_method_name}(value)
name = #{attr_name_expr}
_write_attribute(name, value)

View File

@ -44,6 +44,7 @@ module ActiveStorage
validate_service_configuration(name, service)
generated_association_methods.class_eval <<-CODE, __FILE__, __LINE__ + 1
# frozen_string_literal: true
def #{name}
@active_storage_attached_#{name} ||= ActiveStorage::Attached::One.new("#{name}", self)
end
@ -113,6 +114,7 @@ module ActiveStorage
validate_service_configuration(name, service)
generated_association_methods.class_eval <<-CODE, __FILE__, __LINE__ + 1
# frozen_string_literal: true
def #{name}
@active_storage_attached_#{name} ||= ActiveStorage::Attached::Many.new("#{name}", self)
end