Skip to content

Commit 5f4af71

Browse files
authored
Merge pull request #591 from moberegger/moberegger/optimize_options_merges
Optimize memory allocation when rendering partials
2 parents 9ffacf7 + 6fd6c06 commit 5f4af71

File tree

1 file changed

+19
-31
lines changed

1 file changed

+19
-31
lines changed

lib/jbuilder/jbuilder_template.rb

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ def partial!(*args)
5555
if args.one? && _is_active_model?(args.first)
5656
_render_active_model_partial args.first
5757
else
58-
_render_explicit_partial(*args)
58+
options = args.extract_options!.dup
59+
options[:partial] = args.first if args.present?
60+
_render_partial_with_options options
5961
end
6062
end
6163

@@ -121,7 +123,9 @@ def array!(collection = [], *args)
121123
options = args.first
122124

123125
if args.one? && _partial_options?(options)
124-
partial! options.merge(collection: collection)
126+
options = options.dup
127+
options[:collection] = collection
128+
_render_partial_with_options options
125129
else
126130
super
127131
end
@@ -131,7 +135,7 @@ def set!(name, object = BLANK, *args)
131135
options = args.first
132136

133137
if args.one? && _partial_options?(options)
134-
_set_inline_partial name, object, options
138+
_set_inline_partial name, object, options.dup
135139
else
136140
super
137141
end
@@ -142,14 +146,14 @@ def set!(name, object = BLANK, *args)
142146
alias_method :method_missing, :set!
143147

144148
def _render_partial_with_options(options)
145-
options.reverse_merge! locals: options.except(:partial, :as, :collection, :cached)
146-
options.reverse_merge! ::JbuilderTemplate.template_lookup_options
149+
options[:locals] ||= options.except(:partial, :as, :collection, :cached)
150+
options[:handlers] ||= ::JbuilderTemplate.template_lookup_options[:handlers]
147151
as = options[:as]
148152

149153
if as && options.key?(:collection)
150154
collection = options.delete(:collection) || []
151155
partial = options.delete(:partial)
152-
options[:locals].merge!(json: self)
156+
options[:locals][:json] = self
153157
collection = EnumerableCompat.new(collection) if collection.respond_to?(:count) && !collection.respond_to?(:size)
154158

155159
if options.has_key?(:layout)
@@ -175,7 +179,7 @@ def _render_partial_with_options(options)
175179
end
176180

177181
def _render_partial(options)
178-
options[:locals].merge! json: self
182+
options[:locals][:json] = self
179183
@context.render options
180184
end
181185

@@ -231,34 +235,18 @@ def _set_inline_partial(name, object, options)
231235
value = if object.nil?
232236
[]
233237
elsif _is_collection?(object)
234-
_scope{ _render_partial_with_options options.merge(collection: object) }
235-
else
236-
locals = ::Hash[options[:as], object]
237-
_scope{ _render_partial_with_options options.merge(locals: locals) }
238-
end
239-
240-
set! name, value
241-
end
242-
243-
def _render_explicit_partial(name_or_options, locals = {})
244-
case name_or_options
245-
when ::Hash
246-
# partial! partial: 'name', foo: 'bar'
247-
options = name_or_options
238+
_scope do
239+
options[:collection] = object
240+
_render_partial_with_options options
241+
end
248242
else
249-
# partial! 'name', locals: {foo: 'bar'}
250-
if locals.one? && (locals.keys.first == :locals)
251-
options = locals.merge(partial: name_or_options)
252-
else
253-
options = { partial: name_or_options, locals: locals }
243+
_scope do
244+
options[:locals] = { options[:as] => object }
245+
_render_partial_with_options options
254246
end
255-
# partial! 'name', foo: 'bar'
256-
as = locals.delete(:as)
257-
options[:as] = as if as.present?
258-
options[:collection] = locals[:collection] if locals.key?(:collection)
259247
end
260248

261-
_render_partial_with_options options
249+
_set_value name, value
262250
end
263251

264252
def _render_active_model_partial(object)

0 commit comments

Comments
 (0)