Add Symbol#blank? to skip respond_to?(:empty?)

Any classes that don't implement #blank? will fall back to Object's
implementation, which checks whether #empty? is defined before either
using #empty? or implicit truthiness. Since checking whether #empty? is
defined is expensive, some core classes (Array/Hash) alias #blank?
directly to #empty? to skip the respond_to? check.

This commit applies the alias optimization to Symbol. #blank? is called
on Symbols for many Active Record query methods (select, includes,
group, order, joins, etc.) so it seems reasonable to define the #blank?
alias on Symbol as well.
This commit is contained in:
Hartley McGuire 2023-10-24 23:20:30 -04:00
parent 4c5c904a21
commit 2dd3164c8c
No known key found for this signature in database
GPG Key ID: E823FC1403858A82

@ -100,6 +100,14 @@ class Hash
alias_method :blank?, :empty?
end
class Symbol
# A Symbol is blank if it's empty:
#
# :''.blank? # => true
# :symbol.blank? # => false
alias_method :blank?, :empty?
end
class String
BLANK_RE = /\A[[:space:]]*\z/
ENCODED_BLANKS = Concurrent::Map.new do |h, enc|