Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix a performance issue with dependent params #2127

Merged
merged 1 commit into from
Oct 28, 2020

Conversation

dnesteryuk
Copy link
Member

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

@dnesteryuk dnesteryuk force-pushed the resolve_performace_issue_with_given branch from d0d4d11 to 1e6555f Compare October 27, 2020 08:04
@grape-bot
Copy link

grape-bot commented Oct 27, 2020

1 Warning
⚠️ There’re library changes, but not tests. That’s OK as long as you’re refactoring existing code.

Generated by 🚫 danger

Copy link
Member

@dblock dblock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes sense. Let's beef up UPGRADING to explain in which case it might not work?

UPGRADING.md Outdated
#### Dependent params

If you use [dependent params](https://github.com/ruby-grape/grape#dependent-parameters) with
`Grape::Extensions::Hash::ParamBuilder`, make sure a parameter to be dependent on is set as a Symbol
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Explain what won't work? In this case a String? Also add a period at the end of the sentence or maybe a before/after section?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also add a period at the end of the sentence or maybe a before/after section?

I am not sure what you mean here 😊 Could you explain?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a Symbol. (it's a full sentence, just missing a period)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding before/after, you're saying make sure it's a Symbol. What happens if it's not? Explain that.

@dnesteryuk dnesteryuk force-pushed the resolve_performace_issue_with_given branch 2 times, most recently from 3f52bb7 to ffcb7dd Compare October 28, 2020 07:42
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
@dnesteryuk dnesteryuk force-pushed the resolve_performace_issue_with_given branch from ffcb7dd to affd474 Compare October 28, 2020 07:43
@dnesteryuk
Copy link
Member Author

@dblock please, have a look again. thanks.


If you use [dependent params](https://github.com/ruby-grape/grape#dependent-parameters) with
`Grape::Extensions::Hash::ParamBuilder`, make sure a parameter to be dependent on is set as a Symbol.
If a String is given, a parameter that other parameters depend on won't be found even if it is present.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So is this a bug? Should we also fix this (in another PR)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Performance degradation (~6x) when using given to model a dependent relation
3 participants