Skip to content
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

Speed up nested #simple_fields_for usage by a considerable amount #1810

Merged

Conversation

meanphil
Copy link
Contributor

@meanphil meanphil commented Feb 20, 2023

The FormBuilder#find_mapping method is relying on Object#const_get raising an exception if the constant doesn't exist, rather than directly checking if it exists with Object#const_defined?.

Exceptions are slow due to generating backtraces - and then when other debugging gems (such as did_you mean, which are included with Ruby and Rails these days) get involved it resulted in excessive Object allocation.

Normally the discovery_cache would mitigate this, so there wouldn't be many calls to #mapping_override, however when you use #simple_fields_for with a collection/has_many association the cache is empty for each item in the collection.

Before this change this code was taking approximately 2 seconds and 2.9million allocations for 235 records:

<%= f.simple_fields_for :provider_access_masks, @access_masks do |pam| %>
  <%= pam.input :id, as: 'hidden' %>
  <%= pam.input :_destroy, as: 'boolean', label: "Delete", class: 'destroy' %>
<% end %>

After this change it was down to approximately 100ms and 200,000 allocations. (All on my M1 MacBook Air)

Exceptions are slow due to generating backtraces - and then when other
debugging gems (such as did_you mean, which are included with
Ruby and Rails these days) get involved it resulted in excessive Object
allocation.
Copy link
Member

@carlosantoniodasilva carlosantoniodasilva left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had a recollection of seeing something like this before, and sure enough there was a similar PR a while ago: #1725, I just haven't been able to circle back on that one at the time.

This all sounds like a safe change, and hopefully an improvement to the slowness caused by raising/rescuing exceptions in general. Appreciate it!

@carlosantoniodasilva carlosantoniodasilva merged commit b6ae67d into heartcombo:main Feb 20, 2023
carlosantoniodasilva added a commit that referenced this pull request Feb 20, 2023
carlosantoniodasilva added a commit that referenced this pull request May 14, 2024
This reverts commit b6ae67d, reversing
changes made to 0ef4a58.

This caused custom inputs to not load properly on non-eager load
environments like dev/test, because those constants may not have been
defined yet.

The previous code would try to constantize them and force-load the
lazily-loaded files/constants, the new code breaks that behavior, so
we're reverting.

Closes #1824, see for more info.
carlosantoniodasilva added a commit that referenced this pull request May 14, 2024
This reverts commit b6ae67d, reversing
changes made to 0ef4a58.

This caused custom inputs to not load properly on non-eager load
environments like dev/test, because those constants may not have been
defined yet.

The previous code would try to constantize them and force-load the
lazily-loaded files/constants, the new code breaks that behavior, so
we're reverting.

Closes #1824, see for more info.
@carlosantoniodasilva
Copy link
Member

I'm reverting this change, as it caused a regression. See #1824 for more info.

Happy to review something else that fixes the original problem without the regression, but for now we'll have to live with it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants