forked from rails/jbuilder
-
Notifications
You must be signed in to change notification settings - Fork 0
Optimize internal extract! calls to save on memory allocation
#7
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
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -58,7 +58,7 @@ def set!(key, value = BLANK, *args, &block) | |
| else | ||
| # json.author @post.creator, :name, :email_address | ||
| # { "author": { "name": "David", "email_address": "[email protected]" } } | ||
| _merge_block(key){ extract! value, *args } | ||
| _merge_block(key){ _extract value, args } | ||
| end | ||
|
|
||
| _set_value key, result | ||
|
|
@@ -215,7 +215,7 @@ def array!(collection = [], *attributes, &block) | |
| elsif ::Kernel.block_given? | ||
| _map_collection(collection, &block) | ||
| elsif attributes.any? | ||
| _map_collection(collection) { |element| extract! element, *attributes } | ||
| _map_collection(collection) { |element| _extract element, attributes } | ||
| else | ||
| _format_keys(collection.to_a) | ||
| end | ||
|
|
@@ -241,18 +241,14 @@ def array!(collection = [], *attributes, &block) | |
| # | ||
| # json.(@person, :name, :age) | ||
| def extract!(object, *attributes) | ||
| if ::Hash === object | ||
| _extract_hash_values(object, attributes) | ||
| else | ||
| _extract_method_values(object, attributes) | ||
| end | ||
| _extract object, attributes | ||
| end | ||
|
|
||
| def call(object, *attributes, &block) | ||
| if ::Kernel.block_given? | ||
| array! object, &block | ||
| else | ||
| extract! object, *attributes | ||
| _extract object, attributes | ||
| end | ||
| end | ||
|
|
||
|
|
@@ -281,8 +277,16 @@ def target! | |
|
|
||
| private | ||
|
|
||
| def _extract(object, attributes) | ||
| if ::Hash === object | ||
| _extract_hash_values(object, attributes) | ||
| else | ||
| _extract_method_values(object, attributes) | ||
| end | ||
| end | ||
|
|
||
| def _extract_hash_values(object, attributes) | ||
| attributes.each{ |key| _set_value key, _format_keys(object.fetch(key)) } | ||
| attributes.each{ |key| _set_value key, _format_keys(object[key]) } | ||
| end | ||
|
|
||
| def _extract_method_values(object, attributes) | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also using
[]instead offetch. It's a bit faster.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the behaviour identical on missing keys?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question.
fetchactually throws if the key doesn't exist, vs returningnil. Not clear if this is the intended behaviour or just happenstance. The coding style for the project seems to preferfetch. I git blamed back 10+ years to see if there was any reasoning for this, but saw nothing of note before losing interest.Mixed feelings on this. The current implementation would result in 500 errors if/when this occurs, which is not something I would expect or desire from our tooling. But given my intent here was simply performance - not a behavioural change - I will switch this back to
fetch; the overall savings still fall within the margin of error (according to ips) and not worth the risk and drift from the upstream project.