-
Notifications
You must be signed in to change notification settings - Fork 168
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
refactor: memoize some methods #130
Conversation
I've added memoization to the association classes. It might be unsafe but our tests and mirage tests work fine with it. The issue with these methods is that they don't get an argument. |
I've also ran a slightly larger test, which uses more mirage instances to check how it compares:
1416ms
1053ms Seems like a constant improvement. |
Great work 👍 I'm willing to change these things and if they cause bugs it means we need better test coverage. We should be able to push changes to Mirage if the tests pass. Stylistically when I've done memoization in the past I've done it like this get identifier() {
if (!this._identifier) {
this._identifier = `${camelize(this._container.inflector.singularize(this.key))}Ids`;
}
return this._identifier
} Just curious if that would achieve the same effect as lodash's |
It's probably better in regards to complexity. |
I can confirm that I saw a large improvement with this change set. Models which were taking ~300ms to create are now taking ~100ms. I will note that I did have to modify db-collection.js as follows as sometimes the
|
That's odd. @chbonser can you maybe post a stacktrace of the _findRecord that failed on you? I think it might be becauses some of the methods are memoizing when using this. We should extract a method to pass the this arguments in. I'll update the PR later on. |
@chbonser I've refactored some of the association memoized methods. Now they're native getters that call the methods by passing an argument. Can you check if your adjustment is still needed? @samselikoff I'm not sure if the latest commit is fine as it's potentially breaking api consumers due to multiple api changes. |
@makepanic That did the trick. |
@makepanic Yeah I mean these methods have never been documented, there's no way every method in Mirage is public. I think they are not breaking changes but we should still clearly communicate the changes. Great job on all this work, super appreciate of you diving into this! So using a getter that calls a memoized function is faster eh? |
I don't think that there's a perf difference. The alternative would be to keep the |
implements parts of #128
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, thanks so much!
I left a question and a stylistic comment. Would love to get your feedback or changes on those before merging.
implements parts of #128
🎉 🎉 🎉 Nice job @makepanic |
implements parts of #128