Skip to content

Commit

Permalink
Reduce Array allocations
Browse files Browse the repository at this point in the history
  • Loading branch information
amatsuda committed Sep 18, 2019
1 parent 5c07e1a commit 0fa8c0a
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions activesupport/lib/active_support/parameter_filter.rb
Expand Up @@ -59,24 +59,30 @@ class CompiledFilter # :nodoc:
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
Expand All @@ -85,7 +91,7 @@ def self.compile(filters, mask:)

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
Expand Down

0 comments on commit 0fa8c0a

Please sign in to comment.