You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
`ActiveSupport::ParameterFilter::CompiledFilter` was originally
extracted in 79e91cc as a performance
optimization. Its purpose was to avoid redundantly checking
`@filters.empty?`. However, redundant checks can be avoided by using
separate `ParameterFilter` methods, rather than allocating a separate
`CompiledFilter` object. Therefore, this commit reintegrates
`CompiledFilter` back into `ParameterFilter`.
Additionally, lazy compilation of filters predates the extraction of
`ParameterFilter` from `ActionDispatch::Http::FilterParameters` in
e466354. However, since
`ActionDispatch::Http::FilterParameters` lazily allocates a
`ParameterFilter` instance, there is no benefit to `ParameterFilter`
also lazily compiling filters. Therefore, this commit switches from
lazy compilation to eager compilation.
Together, these changes yield a small performance increase:
**Benchmark script**
```ruby
# frozen_string_literal: true
require "benchmark/ips"
require "benchmark/memory"
require "active_support"
require "active_support/parameter_filter"
ootb = [:passw, :secret, :token, :_key, :crypt, :salt, :certificate, :otp, :ssn]
mixed = [:passw, "secret", /token/, :crypt, "salt", /certificate/, "user.otp", /user\.ssn/, proc {}]
params = {
"user" => {
"name" => :name,
"email" => :email,
"password" => :password,
"ssn" => :ssn,
"locations" => [
{ "city" => :city, "country" => :country },
{ "city" => :city, "country" => :country },
],
}
}
Benchmark.ips do |x|
x.report("ootb") do
ActiveSupport::ParameterFilter.new(ootb).filter(params)
end
x.report("mixed") do
ActiveSupport::ParameterFilter.new(mixed).filter(params)
end
end
```
**Before**
```
Warming up --------------------------------------
ootb 2.032k i/100ms
mixed 1.521k i/100ms
Calculating -------------------------------------
ootb 20.315k (± 1.3%) i/s - 101.600k in 5.001939s
mixed 15.142k (± 1.2%) i/s - 76.050k in 5.023077s
```
**After**
```
Warming up --------------------------------------
ootb 2.163k i/100ms
mixed 1.604k i/100ms
Calculating -------------------------------------
ootb 21.478k (± 1.2%) i/s - 108.150k in 5.036188s
mixed 16.052k (± 0.8%) i/s - 81.804k in 5.096656s
```
0 commit comments