1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Reduce Array allocations

This commit is contained in:
Akira Matsuda 2019-09-18 14:56:28 +09:00
parent 5c07e1a3f4
commit 0fa8c0a1d9

View file

@ -59,24 +59,30 @@ module ActiveSupport
def self.compile(filters, mask:)
return lambda { |params| params.dup } if filters.empty?
strings, regexps, blocks = [], [], []
strings, regexps, blocks, deep_regexps, deep_strings = [], [], [], nil, nil
filters.each do |item|
case item
when Proc
blocks << item
when Regexp
regexps << item
if item.to_s.include?("\\.")
(deep_regexps ||= []) << item
else
regexps << item
end
else
strings << Regexp.escape(item.to_s)
s = Regexp.escape(item.to_s)
if s.include?("\\.")
(deep_strings ||= []) << s
else
strings << s
end
end
end
deep_regexps = regexps.extract! { |r| r.to_s.include?("\\.") }
deep_strings = strings.extract! { |s| s.include?("\\.") }
regexps << Regexp.new(strings.join("|"), true) unless strings.empty?
deep_regexps << Regexp.new(deep_strings.join("|"), true) unless deep_strings.empty?
(deep_regexps ||= []) << Regexp.new(deep_strings.join("|"), true) if deep_strings&.any?
new regexps, deep_regexps, blocks, mask: mask
end
@ -85,7 +91,7 @@ module ActiveSupport
def initialize(regexps, deep_regexps, blocks, mask:)
@regexps = regexps
@deep_regexps = deep_regexps.any? ? deep_regexps : nil
@deep_regexps = deep_regexps&.any? ? deep_regexps : nil
@blocks = blocks
@mask = mask
end