Speed up nested #simple_fields_for usage by a considerable amount #1810
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The
FormBuilder#find_mapping
method is relying onObject#const_get
raising an exception if the constant doesn't exist, rather than directly checking if it exists withObject#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:
After this change it was down to approximately 100ms and 200,000 allocations. (All on my M1 MacBook Air)