Turn on performance based cops

Use attr_reader/attr_writer instead of methods

method is 12% slower

Use flat_map over map.flatten(1)

flatten is 66% slower

Use hash[]= instead of hash.merge! with single arguments

merge! is 166% slower

See https://github.com/rails/rails/pull/32337 for more conversation
上级 a4398e41
......@@ -179,3 +179,12 @@ Style/Semicolon:
# Prefer Foo.method over Foo::method
Style/ColonMethodCall:
Enabled: true
Style/TrivialAccessors:
Enabled: true
Performance/FlatMap:
Enabled: true
Performance/RedundantMerge:
Enabled: true
......@@ -40,9 +40,7 @@ def transform! #:nodoc:
end
private
def message
@message
end
attr_reader :message
def html_part
@html_part ||= message.html_part
......
......@@ -793,9 +793,7 @@ def deep_dup
protected
attr_reader :parameters
def permitted=(new_permitted)
@permitted = new_permitted
end
attr_writer :permitted
def fields_for_style?
@parameters.all? { |k, v| k =~ /\A-?\d+\z/ && (v.is_a?(Hash) || v.is_a?(Parameters)) }
......
......@@ -272,7 +272,7 @@ def encode_credentials(options)
credentials.merge!(options)
path_info = @request.env["PATH_INFO"].to_s
uri = options[:uri] || path_info
credentials.merge!(uri: uri)
credentials[:uri] = uri
@request.env["ORIGINAL_FULLPATH"] = path_info
ActionController::HttpAuthentication::Digest.encode_credentials(method, credentials, password, options[:password_is_ha1])
end
......
......@@ -82,9 +82,7 @@ def append_info_to_payload(payload)
@last_payload = payload
end
def last_payload
@last_payload
end
attr_reader :last_payload
end
end
......
......@@ -1322,7 +1322,7 @@ def assert_resource_methods(expected, resource, action_method, method)
def assert_resource_allowed_routes(controller, options, shallow_options, allowed, not_allowed, path = controller)
shallow_path = "#{path}/#{shallow_options[:id]}"
format = options[:format] && ".#{options[:format]}"
options.merge!(controller: controller)
options[:controller] = controller
shallow_options.merge!(options)
assert_whether_allowed(allowed, not_allowed, options, "index", "#{path}#{format}", :get)
......@@ -1336,7 +1336,7 @@ def assert_resource_allowed_routes(controller, options, shallow_options, allowed
def assert_singleton_resource_allowed_routes(controller, options, allowed, not_allowed, path = controller.singularize)
format = options[:format] && ".#{options[:format]}"
options.merge!(controller: controller)
options[:controller] = controller
assert_whether_allowed(allowed, not_allowed, options, "new", "#{path}/new#{format}", :get)
assert_whether_allowed(allowed, not_allowed, options, "create", "#{path}#{format}", :post)
......
......@@ -105,16 +105,20 @@ def make_headers(hash)
end
test "#merge! headers with mutation" do
# rubocop:disable Performance/RedundantMerge
@headers.merge!("Host" => "http://example.test",
"Content-Type" => "text/html")
# rubocop:enable Performance/RedundantMerge
assert_equal({ "HTTP_HOST" => "http://example.test",
"CONTENT_TYPE" => "text/html",
"HTTP_REFERER" => "/some/page" }, @headers.env)
end
test "#merge! env with mutation" do
# rubocop:disable Performance/RedundantMerge
@headers.merge!("HTTP_HOST" => "http://first.com",
"CONTENT_TYPE" => "text/html")
# rubocop:enable Performance/RedundantMerge
assert_equal({ "HTTP_HOST" => "http://first.com",
"CONTENT_TYPE" => "text/html",
"HTTP_REFERER" => "/some/page" }, @headers.env)
......@@ -156,7 +160,7 @@ def make_headers(hash)
env = { "HTTP_REFERER" => "/" }
headers = make_headers(env)
headers["Referer"] = "http://example.com/"
headers.merge! "CONTENT_TYPE" => "text/plain"
headers["CONTENT_TYPE"] = "text/plain"
assert_equal({ "HTTP_REFERER" => "http://example.com/",
"CONTENT_TYPE" => "text/plain" }, env)
end
......
......@@ -318,7 +318,8 @@ def url_for(object)
@url_for_options = object
if object.is_a?(Hash) && object[:use_route].blank? && object[:controller].blank?
object.merge!(controller: "main", action: "index")
object[:controller] = "main"
object[:action] = "index"
end
super
......
......@@ -168,7 +168,8 @@ def url_for(object)
@url_for_options = object
if object.is_a?(Hash) && object[:use_route].blank? && object[:controller].blank?
object.merge!(controller: "main", action: "index")
object[:controller] = "main"
object[:action] = "index"
end
super
......
......@@ -14,37 +14,23 @@ def initialize
@status = "initialized"
end
def name
@name
end
attr_reader :name, :color, :size, :status
def name=(val)
name_will_change!
@name = val
end
def color
@color
end
def color=(val)
color_will_change! unless val == @color
@color = val
end
def size
@size
end
def size=(val)
attribute_will_change!(:size) unless val == @size
@size = val
end
def status
@status
end
def status=(val)
status_will_change! unless val == @status
@status = val
......
......@@ -57,9 +57,7 @@ def to_hash
private
def uri
@uri
end
attr_reader :uri
def uri_parser
@uri_parser ||= URI::Parser.new
......
......@@ -36,7 +36,7 @@ def indexes(table_name)
end
indexes.last[-2] << row[:Column_name]
indexes.last[-1][:lengths].merge!(row[:Column_name] => row[:Sub_part].to_i) if row[:Sub_part]
indexes.last[-1][:lengths][row[:Column_name]] = row[:Sub_part].to_i if row[:Sub_part]
indexes.last[-1][:orders].merge!(row[:Column_name] => :desc) if row[:Collation] == "D"
end
end
......
......@@ -117,7 +117,7 @@ def connect
end
def configure_connection
@connection.query_options.merge!(as: :array)
@connection.query_options[:as] = :array
super
end
......
......@@ -68,9 +68,7 @@ def structure_load(filename, extra_flags)
private
def configuration
@configuration
end
attr_reader :configuration
def configuration_without_database
configuration.merge("database" => nil)
......
......@@ -90,9 +90,7 @@ def structure_load(filename, extra_flags)
private
def configuration
@configuration
end
attr_reader :configuration
def encoding
configuration["encoding"] || DEFAULT_ENCODING
......
......@@ -60,13 +60,7 @@ def structure_load(filename, extra_flags)
private
def configuration
@configuration
end
def root
@root
end
attr_reader :configuration, :root
def run_cmd(cmd, args, out)
fail run_cmd_error(cmd, args) unless Kernel.system(cmd, *args, out: out)
......
......@@ -187,7 +187,7 @@ def self.sanitize_sql(args)
end
relation = Relation.new(klass)
relation.merge!(where: ["foo = ?", "bar"])
relation.merge!(where: ["foo = ?", "bar"]) # rubocop:disable Performance/RedundantMerge
assert_equal Relation::WhereClause.new(["foo = bar"]), relation.where_clause
end
......
......@@ -152,7 +152,7 @@ def teardown
end
def test_ignores_configurations_without_databases
@configurations["development"].merge!("database" => nil)
@configurations["development"]["database"] = nil
with_stubbed_configurations_establish_connection do
assert_not_called(ActiveRecord::Tasks::DatabaseTasks, :create) do
......@@ -162,7 +162,7 @@ def test_ignores_configurations_without_databases
end
def test_ignores_remote_databases
@configurations["development"].merge!("host" => "my.server.tld")
@configurations["development"]["host"] = "my.server.tld"
with_stubbed_configurations_establish_connection do
assert_not_called(ActiveRecord::Tasks::DatabaseTasks, :create) do
......@@ -172,7 +172,7 @@ def test_ignores_remote_databases
end
def test_warning_for_remote_databases
@configurations["development"].merge!("host" => "my.server.tld")
@configurations["development"]["host"] = "my.server.tld"
with_stubbed_configurations_establish_connection do
ActiveRecord::Tasks::DatabaseTasks.create_all
......@@ -183,7 +183,7 @@ def test_warning_for_remote_databases
end
def test_creates_configurations_with_local_ip
@configurations["development"].merge!("host" => "127.0.0.1")
@configurations["development"]["host"] = "127.0.0.1"
with_stubbed_configurations_establish_connection do
assert_called(ActiveRecord::Tasks::DatabaseTasks, :create) do
......@@ -193,7 +193,7 @@ def test_creates_configurations_with_local_ip
end
def test_creates_configurations_with_local_host
@configurations["development"].merge!("host" => "localhost")
@configurations["development"]["host"] = "localhost"
with_stubbed_configurations_establish_connection do
assert_called(ActiveRecord::Tasks::DatabaseTasks, :create) do
......@@ -203,7 +203,7 @@ def test_creates_configurations_with_local_host
end
def test_creates_configurations_with_blank_hosts
@configurations["development"].merge!("host" => nil)
@configurations["development"]["host"] = nil
with_stubbed_configurations_establish_connection do
assert_called(ActiveRecord::Tasks::DatabaseTasks, :create) do
......@@ -463,7 +463,7 @@ def teardown
end
def test_ignores_configurations_without_databases
@configurations[:development].merge!("database" => nil)
@configurations[:development]["database"] = nil
ActiveRecord::Base.stub(:configurations, @configurations) do
assert_not_called(ActiveRecord::Tasks::DatabaseTasks, :drop) do
......@@ -473,7 +473,7 @@ def test_ignores_configurations_without_databases
end
def test_ignores_remote_databases
@configurations[:development].merge!("host" => "my.server.tld")
@configurations[:development]["host"] = "my.server.tld"
ActiveRecord::Base.stub(:configurations, @configurations) do
assert_not_called(ActiveRecord::Tasks::DatabaseTasks, :drop) do
......@@ -483,7 +483,7 @@ def test_ignores_remote_databases
end
def test_warning_for_remote_databases
@configurations[:development].merge!("host" => "my.server.tld")
@configurations[:development]["host"] = "my.server.tld"
ActiveRecord::Base.stub(:configurations, @configurations) do
ActiveRecord::Tasks::DatabaseTasks.drop_all
......@@ -494,7 +494,7 @@ def test_warning_for_remote_databases
end
def test_drops_configurations_with_local_ip
@configurations[:development].merge!("host" => "127.0.0.1")
@configurations[:development]["host"] = "127.0.0.1"
ActiveRecord::Base.stub(:configurations, @configurations) do
assert_called(ActiveRecord::Tasks::DatabaseTasks, :drop) do
......@@ -504,7 +504,7 @@ def test_drops_configurations_with_local_ip
end
def test_drops_configurations_with_local_host
@configurations[:development].merge!("host" => "localhost")
@configurations[:development]["host"] = "localhost"
ActiveRecord::Base.stub(:configurations, @configurations) do
assert_called(ActiveRecord::Tasks::DatabaseTasks, :drop) do
......@@ -514,7 +514,7 @@ def test_drops_configurations_with_local_host
end
def test_drops_configurations_with_blank_hosts
@configurations[:development].merge!("host" => nil)
@configurations[:development]["host"] = nil
ActiveRecord::Base.stub(:configurations, @configurations) do
assert_called(ActiveRecord::Tasks::DatabaseTasks, :drop) do
......
......@@ -497,9 +497,7 @@ def skip?(arg)
arg.halted || !@user_conditions.all? { |c| c.call(arg.target, arg.value) }
end
def nested
@nested
end
attr_reader :nested
def final?
!@call_template
......@@ -578,7 +576,7 @@ def prepend(*callbacks)
end
protected
def chain; @chain; end
attr_reader :chain
private
......
......@@ -210,9 +210,7 @@ def new_cipher
OpenSSL::Cipher.new(@cipher)
end
def verifier
@verifier
end
attr_reader :verifier
def aead_mode?
@aead_mode ||= new_cipher.authenticated?
......
......@@ -26,25 +26,21 @@ def <<(o)
def pop; @queue.pop; end
end
@after_fork_hooks = []
@@after_fork_hooks = []
def self.after_fork_hook(&blk)
@after_fork_hooks << blk
@@after_fork_hooks << blk
end
def self.after_fork_hooks
@after_fork_hooks
end
cattr_reader :after_fork_hooks
@run_cleanup_hooks = []
@@run_cleanup_hooks = []
def self.run_cleanup_hook(&blk)
@run_cleanup_hooks << blk
@@run_cleanup_hooks << blk
end
def self.run_cleanup_hooks
@run_cleanup_hooks
end
cattr_reader :run_cleanup_hooks
def initialize(queue_size)
@queue_size = queue_size
......
......@@ -265,7 +265,7 @@ def clear #:nodoc:
private
def load_country_zones(code)
country = TZInfo::Country.get(code)
country.zone_identifiers.map do |tz_id|
country.zone_identifiers.flat_map do |tz_id|
if MAPPING.value?(tz_id)
MAPPING.inject([]) do |memo, (key, value)|
memo << self[key] if value == tz_id
......@@ -274,7 +274,7 @@ def load_country_zones(code)
else
create(tz_id, nil, TZInfo::Timezone.new(tz_id))
end
end.flatten(1).sort!
end.sort!
end
def zones_map
......
......@@ -103,9 +103,7 @@ class InitializationTest < ActiveSupport::TestCase
private
def build(**kwargs)
ActiveSupport::Cache::RedisCacheStore.new(driver: DRIVER, **kwargs).tap do |cache|
cache.redis
end
ActiveSupport::Cache::RedisCacheStore.new(driver: DRIVER, **kwargs).tap(&:redis)
end
end
......
......@@ -373,9 +373,7 @@ def config #:nodoc:
@config ||= Application::Configuration.new(self.class.find_root(self.class.called_from))
end
def config=(configuration) #:nodoc:
@config = configuration
end
attr_writer :config
# Returns secrets added to config/secrets.yml.
#
......@@ -413,9 +411,7 @@ def secrets
end
end
def secrets=(secrets) #:nodoc:
@secrets = secrets
end
attr_writer :secrets
# The secret_key_base is used as the input secret to the application's key generator, which in turn
# is used to create all MessageVerifiers/MessageEncryptors, including the ones that sign and encrypt cookies.
......
......@@ -146,9 +146,7 @@ def debug_exception_response_format
@debug_exception_response_format || :default
end
def debug_exception_response_format=(value)
@debug_exception_response_format = value
end
attr_writer :debug_exception_response_format
def paths
@paths ||= begin
......
......@@ -79,13 +79,7 @@ def +(other) # :nodoc:
end
protected
def operations
@operations
end
def delete_operations
@delete_operations
end
attr_reader :operations, :delete_operations
end
class Generators #:nodoc:
......
......@@ -126,7 +126,7 @@ def api_only!
)
if ARGV.first == "mailer"
options[:rails].merge!(template_engine: :erb)
options[:rails][:template_engine] = :erb
end
end
......
......@@ -298,7 +298,7 @@ def execute_command(executor, command, options = {}) # :doc:
sudo = options[:sudo] && !Gem.win_platform? ? "sudo " : ""
config = { verbose: false }
config.merge!(capture: options[:capture]) if options[:capture]
config[:capture] = options[:capture] if options[:capture]
in_root { run("#{sudo}#{extify(executor)} #{command} RAILS_ENV=#{env}", config) }
end
......
......@@ -9,12 +9,12 @@ def loader
@loader ||= Class.new do
extend Rails::AppLoader
def self.exec_arguments
@exec_arguments
end
class << self
attr_accessor :exec_arguments
def self.exec(*args)
@exec_arguments = args
def exec(*args)
self.exec_arguments = args
end
end
end
end
......
......@@ -151,7 +151,8 @@ def app
def build_app(console)
mocked_console = Class.new do
attr_reader :sandbox, :console
attr_accessor :sandbox
attr_reader :console
def initialize(console)
@console = console
......@@ -161,10 +162,6 @@ def config
self
end
def sandbox=(arg)
@sandbox = arg
end
def load_console
end
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册