-
Notifications
You must be signed in to change notification settings - Fork 440
Optimize memory allocation when rendering partials #591
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
Open
moberegger
wants to merge
5
commits into
rails:main
Choose a base branch
from
moberegger:moberegger/optimize_options_merges
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+22
−13
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
86b3946
Optimize options merging
moberegger 866d289
Call _set_value directly
moberegger c6b9bff
Put template_lookup_options to avoid possible breaking change
moberegger ba3a627
Save on more merges
moberegger 9ca4d05
Save memory allocation when calling render
moberegger 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 |
---|---|---|
|
@@ -118,7 +118,8 @@ def array!(collection = [], *args) | |
options = args.first | ||
|
||
if args.one? && _partial_options?(options) | ||
partial! options.merge(collection: collection) | ||
options[:collection] = collection | ||
partial! options | ||
else | ||
super | ||
end | ||
|
@@ -137,14 +138,14 @@ def set!(name, object = BLANK, *args) | |
private | ||
|
||
def _render_partial_with_options(options) | ||
options.reverse_merge! locals: options.except(:partial, :as, :collection, :cached) | ||
options.reverse_merge! ::JbuilderTemplate.template_lookup_options | ||
options[:locals] ||= options.except(:partial, :as, :collection, :cached) | ||
options[:handlers] ||= ::JbuilderTemplate.template_lookup_options[:handlers] | ||
as = options[:as] | ||
|
||
if as && options.key?(:collection) && CollectionRenderer.supported? | ||
collection = options.delete(:collection) || [] | ||
partial = options.delete(:partial) | ||
options[:locals].merge!(json: self) | ||
options[:locals][:json] = self | ||
collection = EnumerableCompat.new(collection) if collection.respond_to?(:count) && !collection.respond_to?(:size) | ||
|
||
if options.has_key?(:layout) | ||
|
@@ -173,9 +174,10 @@ def _render_partial_with_options(options) | |
locals = options.delete(:locals) | ||
array! collection do |member| | ||
member_locals = locals.clone | ||
member_locals.merge! collection: collection | ||
member_locals.merge! as => member | ||
_render_partial options.merge(locals: member_locals) | ||
member_locals[:collection] = collection | ||
member_locals[as] = member | ||
options[:locals] = member_locals | ||
_render_partial options | ||
end | ||
else | ||
array! | ||
|
@@ -186,8 +188,8 @@ def _render_partial_with_options(options) | |
end | ||
|
||
def _render_partial(options) | ||
options[:locals].merge! json: self | ||
@context.render options | ||
options[:locals][:json] = self | ||
@context.render options, nil | ||
end | ||
|
||
def _cache_fragment_for(key, options, &block) | ||
|
@@ -242,13 +244,19 @@ def _set_inline_partial(name, object, options) | |
value = if object.nil? | ||
[] | ||
elsif _is_collection?(object) | ||
_scope{ _render_partial_with_options options.merge(collection: object) } | ||
_scope do | ||
options[:collection] = object | ||
_render_partial_with_options options | ||
end | ||
else | ||
locals = ::Hash[options[:as], object] | ||
_scope{ _render_partial_with_options options.merge(locals: locals) } | ||
_scope do | ||
options[:locals] = locals | ||
_render_partial_with_options options | ||
end | ||
end | ||
|
||
set! name, value | ||
_set_value name, value | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can set the value directly here instead of going back in through This saves a bit in processing and also avoids an extra memory allocation for |
||
end | ||
|
||
def _render_explicit_partial(name_or_options, locals = {}) | ||
|
@@ -259,7 +267,8 @@ def _render_explicit_partial(name_or_options, locals = {}) | |
else | ||
# partial! 'name', locals: {foo: 'bar'} | ||
if locals.one? && (locals.keys.first == :locals) | ||
options = locals.merge(partial: name_or_options) | ||
locals[:partial] = name_or_options | ||
options = locals | ||
else | ||
options = { partial: name_or_options, locals: locals } | ||
end | ||
|
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.
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.
The
render
helper in rails will default the second parameter to{}
. By providingnil
here we save on that extra memory allocation.That second parameter is intended to be the options you provide to the partial if the first param is the partial name (ex:
render 'foo', options
). Since the partial name is included in theoptions
, that second parameter isn't actually used.