Skip to content

Reduce allocations in #with_collection #2333

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 8 commits into from
Jun 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions lib/view_component/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -660,12 +660,12 @@ def validate_initialization_parameters!

# @private
def collection_parameter
provided_collection_parameter || name && name.demodulize.underscore.chomp("_component").to_sym
@provided_collection_parameter ||= name && name.demodulize.underscore.chomp("_component").to_sym
end

# @private
def collection_counter_parameter
:"#{collection_parameter}_counter"
@collection_counter_parameter ||= :"#{collection_parameter}_counter"
end

# @private
Expand All @@ -675,7 +675,7 @@ def counter_argument_present?

# @private
def collection_iteration_parameter
:"#{collection_parameter}_iteration"
@collection_iteration_parameter ||= :"#{collection_parameter}_iteration"
end

# @private
Expand All @@ -691,11 +691,12 @@ def splatted_keyword_argument_present?
end

def initialize_parameter_names
return attribute_names.map(&:to_sym) if respond_to?(:attribute_names)

return attribute_types.keys.map(&:to_sym) if Rails::VERSION::MAJOR <= 5 && respond_to?(:attribute_types)

initialize_parameters.map(&:last)
@initialize_parameter_names ||=
if respond_to?(:attribute_names)
attribute_names.map(&:to_sym)
else
initialize_parameters.map(&:last)
end
end

def initialize_parameters
Expand Down
26 changes: 26 additions & 0 deletions test/sandbox/test/rendering_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,32 @@ def test_render_collection
assert_selector("p", text: "Mints counter: 1")
end

def test_render_collection_inline_allocations
# Stabilize compilation status ahead of testing allocations to simulate rendering
# performance with compiled component
ViewComponent::CompileCache.cache.delete(ProductComponent)
ProductComponent.ensure_compiled

allocations =
if Rails.version.to_f < 8.0
{"3.3.8" => 128, "3.2.8" => 125, "3.1.7" => 125, "3.0.7" => 122}
elsif Rails.version.split(".").first(2).map(&:to_i) == [8, 0]
{"3.5.0" => 121, "3.4.4" => 121, "3.3.8" => 128}
else
{"3.4.4" => 119}
end

products = [Product.new(name: "Radio clock"), Product.new(name: "Mints")]
notice = "On sale"
# Ensure any one-time allocations are done
render_inline(ProductComponent.with_collection(products, notice: notice))

assert_allocations(**allocations) do
render_inline(ProductComponent.with_collection(products, notice: notice))
end
assert_selector("h1", text: "Product", count: 2)
end

def test_render_collection_custom_collection_parameter_name
coupons = [Coupon.new(percent_off: 20), Coupon.new(percent_off: 50)]
render_inline(ProductCouponComponent.with_collection(coupons))
Expand Down
Loading