-
Notifications
You must be signed in to change notification settings - Fork 90
Cache type and attribute names as class variables #100
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
Cache type and attribute names as class variables #100
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks awesome and I think will be a huge performance boost based on our benchmarking. One comment above.
@@ -48,19 +52,22 @@ def id | |||
# per the spec naming recommendations: http://jsonapi.org/recommendations/#naming | |||
# For example, 'MyApp::LongCommment' will become the 'long-comments' type. | |||
def type | |||
object.class.name.demodulize.tableize.dasherize | |||
class_name = object.class.name | |||
@@class_names[class_name] ||= class_name.demodulize.tableize.dasherize |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you could safely add a .freeze
to the end of all of these values, here and below, which would "guarantee" the safety that this caching system will never store strings that might mutate. I can't think of a case in which they would be mutated, but to be safe.
For posterity, here is the before profiling info where we found that the
All of this expense is basically zero'd out with this PR. |
This resulted in a 50-60% speed improvement in many of our API calls, especially the larger ones. 👍 🚀 🚀 |
Folks, I was reviewing this part of code and realized that there are couple of issues with it:
def self.type
@type ||= ....
end
def type
self.class.type
end |
No description provided.