-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Reducers without immer #2405
Comments
Honestly, I'd not go so far. Check what slows it down instead. Usually those are reducers with a lot (thousands or tens of thousands) reads on the |
Ok, interesting. It would also be nice to have that information as a summary of #242, and in the docs. The docs talk at length about how immer won't have that big of a performance cost... but no word about what to do if there is a performance cost. |
Well, so far we had probably two reports of performance costs ^^ (and a lot of people just generally being against immer) But if you want to dive in a bit and make a writeup to add for the docs, a PR would be welcome :) |
Ok sure, I'll send a PR for the docs in the next days. About the |
yes, it's one of the functions RTK re-exports. |
Alright, I've run some tests with both solutions, unfortunately Here is the code being tested: https://gist.github.com/romgrk/6aac91df09cdebf8a0761b9fb3afd20e ( The result table below shows an example run for each solution. There has been some variation in the profiling run times, but the orders of magnitude stay the same. Any solution with immer is in the hundreds of ms range, and the solution without immer is in the 0-1ms range.
|
instead of
can you try doing
|
Would be interesting to see the dataset here, too. |
I've run again with the proposition from @EskiMojo14, it stays in the same order of magnitude. For comparison, I'm including profiling for both the original and the proposition.
So the performance is identical. |
The issue with immer is that for some reason, the performance is |
Can you share a fully running sandbox or repo that shows this behavior, with a meaningful dataset? Also, note that Immer does recursively freeze data by default. Can't tell if that's the issue here because there's not enough of the perf capture shown in those screenshots. However, if that is the issue, note that you can do an |
@romgrk what do you mean by freezing the data? Are you perhaps calling I'm running into a similar performance issue which I suspect has to do with immer. |
@drop-george |
Ah, forgot this was open :) |
I also had this performance problem. Using freeze fixed the problem for any one that runs across this issue. |
@markerikson so what's the action item here?
I guess everyone agrees that there neither should not be such issues with performance, nor everyone should read the whole doc for immer and enable/disable autofreeze, or freeze states manually to be able to use RTK efficiently. Currently I don't see anything in the docs about that problem and how to fix it. |
@gentlee no plans to change any of the behavior here. Do you have specific perf traces showing that freezing is an issue atm? |
@markerikson no I'm just looking at this conversation and at our codebase where we added these lines some time ago: // Performance optimization for Immer by disabling auto freeze in other envs than dev
// https://github.com/reduxjs/redux-toolkit/issues/2405
// https://immerjs.github.io/immer/freezing
setAutoFreeze(__DEV__) As I see in this thread are already some traces and perf metrics used, but solution is not included into any docs / PRs. So I assume the problem still exists. PS. Also writing some article with state manager comparison and just can't avoid this issue with additional complexity with configuring immer, would be much better [for redux] if it did not exist. |
@gentlee I'd recommend actually doing some perf benchmarking to see if it is really an issue in your app's case. Use of Immer (and its freezing behavior) is the right default behavior to prevent common bugs. Accidental mutations were the number one cause of Redux bugs prior to RTK, and since then they've been effectively eliminated. Worst case, as your snippet there shows, freezing can be disabled if absolutely necessary. Also, note that this issue is multiple years old. RTK 2.0 includes Immer 10, which had some noticeable perf improvements. |
@markerikson I'll do some a bit later, just out of interest. About redux bugs - I'm sure these kind of bugs are mostly happening only during the initial development of some feature and trying to understand why it doesn't work. It is hardly possible to make something work properly with mutations - subscriptions won't work. So for sure such things help during development, but unnecessary in prod. Most common bugs from my experience is breaking memoizations by returning [] or new objects / functions from not memoized selectors. I even proposed this some time ago. |
@gentlee I've spent 8+ years helping people with Redux, and answered thousands of questions. Trust me when I say that pre-RTK, accidental mutations were absolutely the most common bug :) |
There's one more thing to consider: afaik, the auto-freezing variant of immer became at some version faster than the non-auto-freezing variant. So at that point it's "immer" vs "not immer", and believe me, to this day we see enough broken code that we won't budge on that. PS: regarding freezing and performance see this release and all the linked threads: https://github.com/immerjs/immer/releases/tag/v8.0.0 |
@markerikson @phryneas added benchmark in separate issue, got very confusing results. Shortly - performance is x100 slower at average and neither disabling auto freeze nor using |
Hey,
I'm aware of the position of the maintainers regarding disabling immer, as stated in #242 and #183. However, both of those discussions are the top google results when searching
redux toolkit without immer
. After some time, I realized that I could both keep RTK and add additional reducers without immer, which is required in edge cases for performance reasons*. I've created a gist with the solution, if you're interested it could be nice to add the gist to the end of the discussion in #242, as that is the top-level result and I'm sure many people have been landing there without a clear way to solve their issue.Here is the link: https://gist.github.com/romgrk/bb16d7b8d3827481d04eb535e8d1bc74
This issue can be closed.
* I'm not sure what goes on inside immer's
produce()
, but in the edge-case I encountered, using a raw reducer took the runtime of a single action from ~200ms to ~2ms.The text was updated successfully, but these errors were encountered: