From 86b394640a186f65f2ee194428e750d7d483d263 Mon Sep 17 00:00:00 2001 From: Michael Oberegger Date: Thu, 8 May 2025 15:29:43 -0400 Subject: [PATCH 1/5] Optimize options merging --- lib/jbuilder/jbuilder_template.rb | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/lib/jbuilder/jbuilder_template.rb b/lib/jbuilder/jbuilder_template.rb index 55f2d5f..c929c6a 100644 --- a/lib/jbuilder/jbuilder_template.rb +++ b/lib/jbuilder/jbuilder_template.rb @@ -4,11 +4,7 @@ require 'active_support/cache' class JbuilderTemplate < Jbuilder - class << self - attr_accessor :template_lookup_options - end - - self.template_lookup_options = { handlers: [:jbuilder] } + HANDLERS = [:jbuilder].freeze def initialize(context, *args) @context = context @@ -137,14 +133,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::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,8 +169,8 @@ 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 + member_locals[:collection] = collection + member_locals[as] = member _render_partial options.merge(locals: member_locals) end else @@ -186,7 +182,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 From 866d2892c2b9fe4c407cb4437f954afd9637d8c3 Mon Sep 17 00:00:00 2001 From: Michael Oberegger Date: Fri, 9 May 2025 12:31:03 -0400 Subject: [PATCH 2/5] 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 c929c6a..5228d5a 100644 --- a/lib/jbuilder/jbuilder_template.rb +++ b/lib/jbuilder/jbuilder_template.rb @@ -244,7 +244,7 @@ def _set_inline_partial(name, object, options) _scope{ _render_partial_with_options options.merge(locals: locals) } end - set! name, value + _set_value name, value end def _render_explicit_partial(name_or_options, locals = {}) From c6b9bffed86c5a8dbdab0e475c92377b7eec1387 Mon Sep 17 00:00:00 2001 From: Michael Oberegger Date: Mon, 12 May 2025 11:48:07 -0400 Subject: [PATCH 3/5] Put template_lookup_options to avoid possible breaking change --- lib/jbuilder/jbuilder_template.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/jbuilder/jbuilder_template.rb b/lib/jbuilder/jbuilder_template.rb index 5228d5a..b4ba87e 100644 --- a/lib/jbuilder/jbuilder_template.rb +++ b/lib/jbuilder/jbuilder_template.rb @@ -4,7 +4,11 @@ require 'active_support/cache' class JbuilderTemplate < Jbuilder - HANDLERS = [:jbuilder].freeze + class << self + attr_accessor :template_lookup_options + end + + self.template_lookup_options = { handlers: [:jbuilder] } def initialize(context, *args) @context = context @@ -134,7 +138,7 @@ def set!(name, object = BLANK, *args) def _render_partial_with_options(options) options[:locals] ||= options.except(:partial, :as, :collection, :cached) - options[:handlers] ||= ::JbuilderTemplate::HANDLERS + options[:handlers] ||= ::JbuilderTemplate.template_lookup_options[:handlers] as = options[:as] if as && options.key?(:collection) && CollectionRenderer.supported? From ba3a6274a9827773769f24a39956358059be7d11 Mon Sep 17 00:00:00 2001 From: Michael Oberegger Date: Mon, 12 May 2025 12:13:21 -0400 Subject: [PATCH 4/5] Save on more merges --- lib/jbuilder/jbuilder_template.rb | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/lib/jbuilder/jbuilder_template.rb b/lib/jbuilder/jbuilder_template.rb index b4ba87e..3dac041 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 @@ -175,7 +176,8 @@ def _render_partial_with_options(options) member_locals = locals.clone member_locals[:collection] = collection member_locals[as] = member - _render_partial options.merge(locals: member_locals) + options[:locals] = member_locals + _render_partial options end else array! @@ -242,10 +244,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_value name, value @@ -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 From 9ca4d0526996d1dfc6e97f1f9ccbc8d971581038 Mon Sep 17 00:00:00 2001 From: Michael Oberegger Date: Mon, 12 May 2025 16:50:23 -0400 Subject: [PATCH 5/5] 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 3dac041..74ed66e 100644 --- a/lib/jbuilder/jbuilder_template.rb +++ b/lib/jbuilder/jbuilder_template.rb @@ -189,7 +189,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)