-
Notifications
You must be signed in to change notification settings - Fork 18
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
Performance of Reads on Draft #35
Comments
hi @kevglass , thanks for your feedback. In fact, even though Mutative has implemented lazy drafts, its access mechanism is still based on ES6 Proxy. As a result, achieving true caching during large-scale draft reads can be quite challenging. When it comes to read-only operations, we strongly recommend using In my tests, the comparison between not using
VS
|
If we were to implement a simple draft WeakMap cache, then indeed, the read performance could be improved by about 40%. I am currently considering whether to implement such an optimization (there may be other factors that need to be considered as well). |
Hey, thanks a lot for the quick reply! In the case I'm working on we won't know whether the developer will be making read only use of the draft or writing to certain pieces. It's likely they'll be doing a lot of reads and some small number of modifications (at least thats the expectations). I don't think I can pass them a |
In the same code example, I implemented a pure object tree based on deep Proxies, which still performs slowly without any other draft code involved. It possibly executed the Proxy getter function 40 million times. Therefore, the performance of Mutative draft reads is challenging to match that of
|
This seems very promising, thanks for providing a comparison that shows the substantial performance difference! There's not that much info in the README on |
hi @bfelbo , the
Therefore, we recommend minimizing the number of times create({ a: { b: { c: 1 } }, d: { f: 1 } }, (draft) => {
draft.a.b.c = 2;
// The node `a` has been modified.
expect(current(draft.a) === current(draft.a)).toBeFalsy();
// The node `d` has not been modified.
expect(current(draft.d) === current(draft.d)).toBeTruthy();
}); |
You can find more documentation on the |
Thanks @unadlib, really appreciate the detailed explanation and docs! |
This makes sense. I find it quite interesting though. Would you be able to share your code? (no need to clean it up or anything like that) |
hi @bfelbo , based on the implementation of this PR(#42), the performance comparison is as follows. Before
After
|
Thanks, and cool that you improved performance further! |
We have released Mutative v1.0.6. Feel free to use it. |
As part of an SDK I'm working on I provide a draft version of user provided data structure back to them to be updated. I want to maintain the immutable status of the original data so that I can compare it safely later on.
However, in this case the user has provided a reasonably large data structure that represents a physics engine. The performance of making changes to the mutative draft is understandably not as fast as a raw object - however, the performance of reads of properties of the draft seem to be impacted too.
To test this out I've put together a simple standalone test case over here: https://github.com/kevglass/mutative-performance-sample/ - it's important to note that the create() is intentionally inside the loop since in the real system the draft is created every frame.
It simulates a collection of balls (30) on a table moving in random directions and colliding. The performance test can be run in two ways - either with writing to the draft object (the same as it would be in a real physics engine) or in read only mode where the simulation is just calculating some values based on the contents of the draft objects.
I feel like I must be doing something wrong but I can't quite understand what it is. The results on my M1 for read only access to the draft object looks like this:
Where RAW is a simple JS object, RAW+COPY is taking a copy of the object at each stage (parse/stringify). Mutative is the lovely library here and Immer for comparison.
I hadn't expected the impact of reading from the draft to be so high, so i'm guessing I've done something very wrong.
Any thoughts or directions appreciated.
For completeness heres my read/write results from my M1:
The text was updated successfully, but these errors were encountered: