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

Conversation

mvz
Copy link
Contributor

@mvz mvz commented Jun 6, 2025

What are you trying to accomplish?

Speed up rendering when there are lots of calls to #with_collection for the same componen, for example when rendering a collection of items, where each item contains a collection of sub-items.

What approach did you choose and why?

I focused on reducing redundant allocation of string objects. This is because rack-mini-profiler's flamegraph showed me lots of repeats of the same string interpolation.

Anything you want to highlight for special attention from reviewers?

The code setting the expected number of allocations will have to be updated to include values for all the relevant Ruby and Rails versions.

@joelhawksley joelhawksley merged commit bac7a24 into ViewComponent:main Jun 9, 2025
23 of 25 checks passed
@joelhawksley
Copy link
Member

Thank you for your contribution!

joelhawksley added a commit that referenced this pull request Jun 9, 2025
* Deprecate use_helper(s) in favor of include MyHelper or helpers. (#2331)

* Reduce allocations in #with_collection (#2333)

* Add test of number of allocations when rendering with with_collection

* Ensure one-time initializations are done before testing allocations

* Cache calculated collection parameter

* Cache collection_counter_parameter

* Cache collection_iteration_parameter

* Cache initialize_parameter_names

* Add expected number of allocations for all tested Ruby/Rails combinations

* Remove code handling obsolete Rails version

* fix allocations

---------

Co-authored-by: Matijs van Zuijlen <[email protected]>
@mvz mvz deleted the reduce-collection-allocations branch June 10, 2025 08:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants