Skip to content

Commit

Permalink
Avoid extra string allocation in the methods generated by eval
Browse files Browse the repository at this point in the history
  • Loading branch information
kamipo committed Mar 10, 2020
1 parent d5be4f1 commit 1ef8c60
Show file tree
Hide file tree
Showing 11 changed files with 16 additions and 7 deletions.
8 changes: 4 additions & 4 deletions actionpack/lib/abstract_controller/helpers.rb
Expand Up @@ -66,10 +66,10 @@ def helper_method(*methods)

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
Expand Down
3 changes: 2 additions & 1 deletion actionpack/lib/action_dispatch/http/request.rb
Expand Up @@ -49,8 +49,9 @@ class Request
# 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
Expand Down
1 change: 1 addition & 0 deletions actionpack/lib/action_dispatch/journey/route.rb
Expand Up @@ -13,6 +13,7 @@ module VerbMatchers
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
Expand Down
Expand Up @@ -145,6 +145,7 @@ def polymorphic_path(record_or_hash_or_array, options = {})

%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
Expand Down
2 changes: 1 addition & 1 deletion actionview/lib/action_view/helpers/form_helper.rb
Expand Up @@ -1905,7 +1905,7 @@ def initialize(object_name, object, template, options)
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))
Expand Down
1 change: 1 addition & 0 deletions actionview/lib/action_view/layouts.rb
Expand Up @@ -321,6 +321,7 @@ def _write_layout_method # :nodoc:
end

class_eval <<-RUBY, __FILE__, __LINE__ + 1
# frozen_string_literal: true
def _layout(lookup_context, formats)
if _conditional_layout?
#{layout_definition}
Expand Down
2 changes: 1 addition & 1 deletion activemodel/lib/active_model/attribute_methods.rb
Expand Up @@ -495,7 +495,7 @@ module AttrNames # :nodoc:
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}"
Expand Down
1 change: 1 addition & 0 deletions activemodel/lib/active_model/attributes.rb
Expand Up @@ -47,6 +47,7 @@ def define_method_attribute=(name)
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)
Expand Down
1 change: 1 addition & 0 deletions activerecord/lib/active_record/attribute_methods/read.rb
Expand Up @@ -12,6 +12,7 @@ def define_method_attribute(name)
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) }
Expand Down
1 change: 1 addition & 0 deletions activerecord/lib/active_record/attribute_methods/write.rb
Expand Up @@ -16,6 +16,7 @@ def define_method_attribute=(name)
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)
Expand Down
2 changes: 2 additions & 0 deletions activestorage/lib/active_storage/attached/model.rb
Expand Up @@ -44,6 +44,7 @@ def has_one_attached(name, dependent: :purge_later, service: nil)
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
Expand Down Expand Up @@ -113,6 +114,7 @@ def has_many_attached(name, dependent: :purge_later, service: nil)
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
Expand Down

0 comments on commit 1ef8c60

Please sign in to comment.