Memoize Database Field Names For Faster Fields Access #5809
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.
Background:
While working on improving performance on our app, I've noticed that the
database_field_name
is consistently taking long than expected when calling the same field repeatedly(very common when iterating over tens of thousands of objects or in inner loops)While debugging I've noticed that
Mongoid::Fields#database_field_name
is taking a significant time to perform.Comparison(very naive):
Before this change takes roughly 1.55 seconds, after it would take 0.99 seconds.
In more realistic cases such as validations of large number of objects as part of factories I've seen 15-20% differences
This code should be able to accept aliases and non-aliases and preserve the mapping, and save time on repeated access.
It is very possible I've missed some cache busters, please help me identify cases like that, as well as confirm my performance estimations, or if you can help with an even faster solution.