Commit 5d6b712
authored
pass options to delegators when they accept it (#336)
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.1 parent ab3238d commit 5d6b712
5 files changed
Lines changed: 14 additions & 4 deletions
File tree
- lib/grape_entity
- delegator
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| 11 | + | |
11 | 12 | | |
12 | 13 | | |
13 | 14 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
7 | | - | |
| 7 | + | |
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
7 | | - | |
| 7 | + | |
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
7 | | - | |
| 7 | + | |
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
130 | 130 | | |
131 | 131 | | |
132 | 132 | | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
133 | 137 | | |
134 | 138 | | |
135 | 139 | | |
| |||
479 | 483 | | |
480 | 484 | | |
481 | 485 | | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
482 | 489 | | |
483 | 490 | | |
484 | 491 | | |
| |||
531 | 538 | | |
532 | 539 | | |
533 | 540 | | |
| 541 | + | |
| 542 | + | |
534 | 543 | | |
535 | | - | |
| 544 | + | |
536 | 545 | | |
537 | 546 | | |
538 | 547 | | |
| |||
0 commit comments