Memoize serializer classes #101
Merged
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.
These should be memoizable, and it avoids (in most cases, though an explicit
jsonapi_serializer_class_name
can't benefit here since we can't know it'll be reliably the same) the need for an expensive#constantize
operation.Monkeypatching in this change in a production application I'm working with shaved ~5% off total request time when dealing with large numbers of included models. I also threw together a quick Rails app to give an accurate-ish benchmark (I believe
#constantize
performs worse when there are more constants/namespaces to choose from, but don't quote me on it): https://github.com/amcaplan/jsonapi-serializer-benchmark/blob/master/lib/tasks/benchmark.rakeUsing
benchmark-ips
, on the master branch, I saw 5.92/sec with a high of 5.949/sec over 3 runs, as opposed to 6.31/sec with a low of 6.276/sec over 3 runs on my branch, meaning it's a 6.5% performance increase (in that particular case) and the variance of the two sets of figures didn't overlap (so it's pretty clearly a win).