Skip to content

Commit 3f52bb7

Browse files
committed
fix a performance issue with dependent params
Fixes #2100 The reason was in `ActiveSupport::HashWithIndifferentAccess`, it is super expensive. When users use a `Grape::Extensions::ActiveSupport::HashWithIndifferentAccess::ParamBuilder` or `Grape::Extensions::Hashie::Mash::ParamBuilder` parameter builder there is no change. However, users who use `Grape::Extensions::Hash::ParamBuilder` must make sure a parameter to be dependent on must be a symbol. given :matrix do # block here end Benchmark after this fix: Warming up -------------------------------------- Given 1.000 i/100ms Simple 1.000 i/100ms Calculating ------------------------------------- Given 0.804 (± 0.0%) i/s - 49.000 in 61.186831s Simple 0.855 (± 0.0%) i/s - 52.000 in 60.926097s Comparison: Simple: 0.9 i/s Given: 0.8 i/s - 1.06x slower
1 parent 1e97967 commit 3f52bb7

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#### Fixes
88

99
* Your contribution here.
10+
* [#2127](https://github.com/ruby-grape/grape/pull/2127): Fix a performance issue with dependent params - [@dnesteryuk](https://github.com/dnesteryuk).
1011
* [#2126](https://github.com/ruby-grape/grape/pull/2126): Fix warnings about redefined attribute accessors in `AttributeTranslator` - [@samsonjs](https://github.com/samsonjs).
1112
* [#2121](https://github.com/ruby-grape/grape/pull/2121): Fix 2.7 deprecation warning in validator_factory - [@Legogris](https://github.com/Legogris).
1213
* [#2115](https://github.com/ruby-grape/grape/pull/2115): Fix declared_params regression with multiple allowed types - [@stanhu](https://github.com/stanhu).

UPGRADING.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,28 @@
11
Upgrading Grape
22
===============
33

4+
### Upgrading to >= 1.5.1
5+
6+
#### Dependent params
7+
8+
If you use [dependent params](https://github.com/ruby-grape/grape#dependent-parameters) with
9+
`Grape::Extensions::Hash::ParamBuilder`, make sure a parameter to be dependent on is set as a Symbol.
10+
If a String is given, a parameter that other parameters depend on won't be found even if it is present.
11+
12+
**Correct**:
13+
```ruby
14+
given :matrix do
15+
# dependent params
16+
end
17+
```
18+
19+
**Wrong**:
20+
```ruby
21+
given 'matrix' do
22+
# dependent params
23+
end
24+
```
25+
426
### Upgrading to >= 1.5.0
527

628
Prior to 1.3.3, the `declared` helper would always return the complete params structure if `include_missing=true` was set. In 1.3.3 a regression was introduced such that a missing Hash with or without nested parameters would always resolve to `{}`.

lib/grape/validations/params_scope.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,9 @@ def meets_dependency?(params, request_params)
6161
end
6262

6363
return params.any? { |param| meets_dependency?(param, request_params) } if params.is_a?(Array)
64-
return false unless params.respond_to?(:with_indifferent_access)
65-
params = params.with_indifferent_access
64+
65+
# params might be anything what looks like a hash, so it must implement a `key?` method
66+
return false unless params.respond_to?(:key?)
6667

6768
@dependent_on.each do |dependency|
6869
if dependency.is_a?(Hash)

0 commit comments

Comments
 (0)