From d3568bc0ae1c8b93254c642e6b5d6b7232d6c472 Mon Sep 17 00:00:00 2001 From: Michael Oberegger Date: Fri, 30 May 2025 15:06:54 -0400 Subject: [PATCH 1/3] Optimize options merging --- lib/jbuilder/jbuilder_template.rb | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/lib/jbuilder/jbuilder_template.rb b/lib/jbuilder/jbuilder_template.rb index 1326c9d..f514a93 100644 --- a/lib/jbuilder/jbuilder_template.rb +++ b/lib/jbuilder/jbuilder_template.rb @@ -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) 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) @@ -170,7 +171,7 @@ def _render_partial_with_options(options) end def _render_partial(options) - options[:locals].merge! json: self + options[:locals][:json] = self @context.render options end @@ -226,10 +227,16 @@ 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 @@ -243,7 +250,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 From ec3c3582ac87558a37c9216c88a53b408de2e8e2 Mon Sep 17 00:00:00 2001 From: Michael Oberegger Date: Fri, 30 May 2025 15:07:29 -0400 Subject: [PATCH 2/3] Call set_value directly --- lib/jbuilder/jbuilder_template.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/jbuilder/jbuilder_template.rb b/lib/jbuilder/jbuilder_template.rb index f514a93..5aaf601 100644 --- a/lib/jbuilder/jbuilder_template.rb +++ b/lib/jbuilder/jbuilder_template.rb @@ -239,7 +239,7 @@ def _set_inline_partial(name, object, options) end end - set! name, value + _set_value name, value end def _render_explicit_partial(name_or_options, locals = {}) From d3cb090cc45df3ec3e8c5f03135d6617956f6eb4 Mon Sep 17 00:00:00 2001 From: Michael Oberegger Date: Fri, 30 May 2025 15:08:20 -0400 Subject: [PATCH 3/3] Save memory allocation when calling render --- lib/jbuilder/jbuilder_template.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/jbuilder/jbuilder_template.rb b/lib/jbuilder/jbuilder_template.rb index 5aaf601..07425a6 100644 --- a/lib/jbuilder/jbuilder_template.rb +++ b/lib/jbuilder/jbuilder_template.rb @@ -172,7 +172,7 @@ def _render_partial_with_options(options) def _render_partial(options) options[:locals][:json] = self - @context.render options + @context.render options, nil end def _cache_fragment_for(key, options, &block)