-
Notifications
You must be signed in to change notification settings - Fork 152
pass options to delegators when they accept it #336
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
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 similar comment
This change reduces hash allocations. Here is a simplified example what was happening. def do_not_accept_keywords(**) end 100.times { do_not_accept_keywords(hash: true) } After measuring via memory profiler, we got this: Total allocated: 69600 bytes (300 objects) allocated memory by location ----------------------------------- 69600 profile/sample.rb:15 It points to a line where the method gets called. So, every call created a separate hash. It was happening for `Grape::Entity::Delegator::PlainObject`. If options get extracted, the picture is better. def do_not_accept_keywords(**) end opts = { hash: true } 100.times { do_not_accept_keywords(opts) } Allocation: Total allocated: 46632 bytes (201 objects) allocated memory by location ----------------------------------- 46400 profile/sample.rb:15 232 profile/sample.rb:13 However, there is no object allocation if nothing is passed to the method. options. Total allocated: 0 bytes (0 objects) Btw, if a method "swallows" arguments, but nothing is passed, there is still allocation. def do_not_accept_keywords(**) end 100.times { do_not_accept_keywords } Result: Total allocated: 23200 bytes (100 objects) allocated memory by location ----------------------------------- 23200 profile/sample.rb:15 The splat operator brings its cost. So, now Grape Entity checks whether the delegetor accepts options before passing them. I measured this change against our production: **Before:** Total allocated: 1512362 bytes (12328 objects) allocated memory by location ----------------------------------- 605520 /usr/local/bundle/gems/grape-entity-0.8.0/lib/grape_entity/entity.rb:537 **After:** Total allocated: 908402 bytes (9733 objects) allocated memory by location ----------------------------------- 18000 /app/grape-entity/lib/grape_entity/entity.rb:553 0.57 MB of RAM were saved while serving the identical request.
Generated by 🚫 danger |
1 similar comment
Generated by 🚫 danger |
thanks @dnesteryuk … especially for the explanation but … can it have implications on upgrade? |
@LeFnord I don't see how it might impact existing code in projects which use grape entity 🤔 |
LeFnord
approved these changes
Jul 15, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This change reduces hash allocations.
Here is a simplified example what was happening.
After measuring via memory profiler, we got this:
It points to a line where the method gets called. So, every call created a separate hash. It was happening for
Grape::Entity::Delegator::PlainObject
.If options get extracted, the picture is better.
Allocation:
However, there is no object allocation if nothing is passed to the method.
Btw, if a method "swallows" arguments, but nothing is passed, there is still allocation.
Result:
The splat operator brings its cost.
So, now Grape Entity checks whether the delegetor accepts options before passing them.
I measured this change against our production:
Before:
After:
0.57 MB of RAM were saved while serving the identical request.