From c65864cdcaa801cac320922f1227218c21a0da4c Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Fri, 29 May 2020 10:20:13 +0900 Subject: [PATCH] Prefer no allocation `start/end_with?` over `String#[] ==` --- actionview/lib/action_view/dependency_tracker.rb | 2 +- .../lib/action_view/renderer/abstract_renderer.rb | 2 +- actionview/lib/action_view/template.rb | 2 +- activemodel/README.rdoc | 2 +- activemodel/lib/active_model/validations.rb | 4 ++-- .../connection_adapters/postgresql/oid/legacy_point.rb | 2 +- .../connection_adapters/postgresql/oid/point.rb | 2 +- .../connection_adapters/postgresql/oid/range.rb | 10 +++++----- activesupport/lib/active_support/array_inquirer.rb | 6 ++++-- activesupport/lib/active_support/string_inquirer.rb | 6 ++++-- railties/lib/rails/source_annotation_extractor.rb | 2 +- 11 files changed, 22 insertions(+), 18 deletions(-) diff --git a/actionview/lib/action_view/dependency_tracker.rb b/actionview/lib/action_view/dependency_tracker.rb index db347f8396..bcdee64681 100644 --- a/actionview/lib/action_view/dependency_tracker.rb +++ b/actionview/lib/action_view/dependency_tracker.rb @@ -170,7 +170,7 @@ module ActionView def explicit_dependencies dependencies = source.scan(EXPLICIT_DEPENDENCY).flatten.uniq - wildcards, explicits = dependencies.partition { |dependency| dependency[-1] == "*" } + wildcards, explicits = dependencies.partition { |dependency| dependency.end_with?("*") } (explicits + resolve_directories(wildcards)).uniq end diff --git a/actionview/lib/action_view/renderer/abstract_renderer.rb b/actionview/lib/action_view/renderer/abstract_renderer.rb index 22043d064d..82aeef3922 100644 --- a/actionview/lib/action_view/renderer/abstract_renderer.rb +++ b/actionview/lib/action_view/renderer/abstract_renderer.rb @@ -46,7 +46,7 @@ module ActionView as.to_sym else begin - base = path[-1] == "/" ? "" : File.basename(path) + base = path.end_with?("/") ? "" : File.basename(path) raise_invalid_identifier(path) unless base =~ /\A_?(.*?)(?:\.\w+)*\z/ $1.to_sym end diff --git a/actionview/lib/action_view/template.rb b/actionview/lib/action_view/template.rb index d2d24299fe..88c0f90911 100644 --- a/actionview/lib/action_view/template.rb +++ b/actionview/lib/action_view/template.rb @@ -138,7 +138,7 @@ module ActionView @virtual_path = virtual_path @variable = if @virtual_path - base = @virtual_path[-1] == "/" ? "" : ::File.basename(@virtual_path) + base = @virtual_path.end_with?("/") ? "" : ::File.basename(@virtual_path) base =~ /\A_?(.*?)(?:\.\w+)*\z/ $1.to_sym end diff --git a/activemodel/README.rdoc b/activemodel/README.rdoc index bf910846c4..8fd7ed7ed8 100644 --- a/activemodel/README.rdoc +++ b/activemodel/README.rdoc @@ -200,7 +200,7 @@ behavior out of the box: attr_accessor :first_name, :last_name validates_each :first_name, :last_name do |record, attr, value| - record.errors.add attr, 'starts with z.' if value.to_s[0] == ?z + record.errors.add attr, "starts with z." if value.start_with?("z") end end diff --git a/activemodel/lib/active_model/validations.rb b/activemodel/lib/active_model/validations.rb index 4a6b464131..73525a94fc 100644 --- a/activemodel/lib/active_model/validations.rb +++ b/activemodel/lib/active_model/validations.rb @@ -15,7 +15,7 @@ module ActiveModel # attr_accessor :first_name, :last_name # # validates_each :first_name, :last_name do |record, attr, value| - # record.errors.add attr, 'starts with z.' if value.to_s[0] == ?z + # record.errors.add attr, "starts with z." if value.start_with?("z") # end # end # @@ -61,7 +61,7 @@ module ActiveModel # attr_accessor :first_name, :last_name # # validates_each :first_name, :last_name, allow_blank: true do |record, attr, value| - # record.errors.add attr, 'starts with z.' if value.to_s[0] == ?z + # record.errors.add attr, "starts with z." if value.start_with?("z") # end # end # diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/oid/legacy_point.rb b/activerecord/lib/active_record/connection_adapters/postgresql/oid/legacy_point.rb index 5504d4b1c9..687f75956b 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/oid/legacy_point.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/oid/legacy_point.rb @@ -14,7 +14,7 @@ module ActiveRecord def cast(value) case value when ::String - if value[0] == "(" && value[-1] == ")" + if value.start_with?("(") && value.end_with?(")") value = value[1...-1] end cast(value.split(",")) diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/oid/point.rb b/activerecord/lib/active_record/connection_adapters/postgresql/oid/point.rb index e5474bce1c..f53b3fa602 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/oid/point.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/oid/point.rb @@ -18,7 +18,7 @@ module ActiveRecord when ::String return if value.blank? - if value[0] == "(" && value[-1] == ")" + if value.start_with?("(") && value.end_with?(")") value = value[1...-1] end x, y = value.split(",") diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/oid/range.rb b/activerecord/lib/active_record/connection_adapters/postgresql/oid/range.rb index d19f1f9cf8..c668aa0d13 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/oid/range.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/oid/range.rb @@ -67,12 +67,12 @@ module ActiveRecord end def extract_bounds(value) - from, to = value[1..-2].split(",") + from, to = value[1..-2].split(",", 2) { - from: (value[1] == "," || from == "-infinity") ? infinity(negative: true) : from, - to: (value[-2] == "," || to == "infinity") ? infinity : to, - exclude_start: (value[0] == "("), - exclude_end: (value[-1] == ")") + from: (from == "" || from == "-infinity") ? infinity(negative: true) : from, + to: (to == "" || to == "infinity") ? infinity : to, + exclude_start: value.start_with?("("), + exclude_end: value.end_with?(")") } end diff --git a/activesupport/lib/active_support/array_inquirer.rb b/activesupport/lib/active_support/array_inquirer.rb index b2b9e9c0b7..0cbba0b6df 100644 --- a/activesupport/lib/active_support/array_inquirer.rb +++ b/activesupport/lib/active_support/array_inquirer.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require "active_support/core_ext/symbol/starts_ends_with" + module ActiveSupport # Wrapping an array in an +ArrayInquirer+ gives a friendlier way to check # its string-like contents: @@ -34,11 +36,11 @@ module ActiveSupport private def respond_to_missing?(name, include_private = false) - (name[-1] == "?") || super + name.end_with?("?") || super end def method_missing(name, *args) - if name[-1] == "?" + if name.end_with?("?") any?(name[0..-2]) else super diff --git a/activesupport/lib/active_support/string_inquirer.rb b/activesupport/lib/active_support/string_inquirer.rb index e5091e127a..5ff3f4bfe7 100644 --- a/activesupport/lib/active_support/string_inquirer.rb +++ b/activesupport/lib/active_support/string_inquirer.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require "active_support/core_ext/symbol/starts_ends_with" + module ActiveSupport # Wrapping a string in this class gives you a prettier way to test # for equality. The value returned by Rails.env is wrapped @@ -19,11 +21,11 @@ module ActiveSupport class StringInquirer < String private def respond_to_missing?(method_name, include_private = false) - (method_name[-1] == "?") || super + method_name.end_with?("?") || super end def method_missing(method_name, *arguments) - if method_name[-1] == "?" + if method_name.end_with?("?") self == method_name[0..-2] else super diff --git a/railties/lib/rails/source_annotation_extractor.rb b/railties/lib/rails/source_annotation_extractor.rb index 77a99036ec..f2ebcada7d 100644 --- a/railties/lib/rails/source_annotation_extractor.rb +++ b/railties/lib/rails/source_annotation_extractor.rb @@ -109,7 +109,7 @@ module Rails results = {} Dir.glob("#{dir}/*") do |item| - next if File.basename(item)[0] == ?. + next if File.basename(item).start_with?(".") if File.directory?(item) results.update(find_in(item))