readAtomState is called many times before atoms are mounted #2334
Replies: 4 comments 6 replies
-
Thanks for reporting. I'll take a closer look eventually, but it may take time. |
Beta Was this translation helpful? Give feedback.
-
Okay, I have some time and read your description again. (My previous comment was based on my misunderstanding.) First of all, yes I understand the behavior and I had also wondered if there's any way to avoid re-evaluation for unmounted atoms before. Now, what I wasn't aware is this becomes a real issue with SSR. Because, in SSR, atoms are never mounted, atom states are never cached, right? That's something to consider. I thought about something like your second approach too. Yeah, That said, if this is SSR only issue, I think providing a wrapper util or even non-subscribeable cache-only store creator makes sense. Or, SSR-aware Provider. In any case, I think it's a better idea to not modify the core implementation, but do things outside. |
Beta Was this translation helpful? Give feedback.
-
@edkimmel Did you find a solution here? We have the exact same issue. We are moving from Recoil to Jotai and have a case where loading with Recoil takes 100ms and Jotai takes 10 000ms + (worst cases crashes the browser) |
Beta Was this translation helpful? Give feedback.
-
Thanks, this helped us. @edkimmel: Did you update the hack to work with the new store implementation? Would you be willing to share the patch with us, or is it the same as in the PR above? |
Beta Was this translation helpful? Give feedback.
-
We had a bad performance regression in our project recently. The root cause was the addition of a deep atom nesting structure to an already deep nested atom structure.
The call count for readAtomState during SSR jumped from 60k (Which is already a lot) to 170k due to the introduction of a few new atoms at the top of the tree. This is a situation where all data has been fetched and we are bootstrapping and rendering a large stateful page in one render.
Update - a new content rendering flow was found to call readAtomState 1,050,000 times during SSR due to a very heavy atom tree. This was brought down to 30k with a local patch to jotai similar to the proposal here.
#2333 was created to showcase the issue and hopefully find a solution acceptable to be put back upstream.
The root of the issue appears to be that jotai cannot make assumptions about the value of an atom until after the atoms are mounted. This is due to useHydrateAtoms and other functionality which is able to mutate the state of an atom immediately, expecting all future reads in the same render pass to return the new state.
One proposal is the ability to tell a jotai store that an atom will be immutable prior to mounting, allowing it to exit without iterating through the dependency tree. This has the benefit of being backwards compatible and not changing the core interface most users of jotai would work with.
Beta Was this translation helpful? Give feedback.
All reactions