Use match? in FactoryBot#aliases_for to save allocations #1457
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.
FactoryBot relies on ruby >= 2.5, so we can use Regexp#match? that does not allocate MatchData
Allocations measured with heap-profiler gem in an internal test suite with 7497 examples and 149470 calls to aliases_for:
allocated memory by file
allocated objects by file
The remaining allocations are mainly caused by
factory_bot/lib/factory_bot/aliases.rb
Line 14 in 9e58a34
In the test suite I used, I noticed that
attribute
values are repeated quite often. For the 149470 calls I just got 16 uniqueattribute
values. So a simple caching mechanism prevents most of the allocations:In another test suite I got 56 unique
attribute
values for 228979 calls toaliases_for
. I don't know if this is representative for other users of FactoryBot, but I could prepare a second PR with the cache idea.