Open nathanmarks opened 1 year ago
+1.
Similar to some of the performance issues I noted in this community post.
+1 here's another issue that's regarding this
https://github.com/apollographql/apollo-client/issues/10322
@jerelmiller Is there any implication to doing stopQueryNoBroadcast? Could there be an options object in the constructor to let us change this behaviour?
Edit: At least until InMemoryCache's performance is fixed.
When we end up with a lot of
useQuery
hooks on the page, we've noticed significant performance issues trying to unmount the components on navigation. This becomes a huge issue on pages where we do infinite scroll pagination and end up with a lot of results, each of which has numeroususeQuery
instances within.I tracked this down to here: https://github.com/apollographql/apollo-client/blob/v3.7.1/src/core/ObservableQuery.ts#L926
stopQuery
callsbroadcastQueries
: https://github.com/apollographql/apollo-client/blob/v3.7.1/src/core/QueryManager.ts#L703This means that for each of the
ObservableQuery
instances being torn down (which, is a LOT), we're iterating over every single query known byQueryManager
to maybe synchronously flush/notify their listeners if they have already been marked as dirty. WhenQueryManager
is aware of thousands of queries, this adds up and can lock up the thread for a long period of time -- especially on slower devices.This seems to be somehat of a brute force flush, the only place
queryInfo.dirty
seems to even get set totrue
is insetDiff
, which will kicknotify
off itself if it needs to: https://github.com/apollographql/apollo-client/blob/v3.7.1/src/core/QueryInfo.ts#L204-L215. Yes it's not synchronous there, but there doesn't seem to be a reason to incur this cost on every single useQuery teardown just to maybe flush synchronously. With some brief tests I couldn't find a single place in our app where any of thosebroadcastQuery
notifies were synchronously flushing an update anyways. It's just a ton of work done for nothing.Why do we need to do this broadcast to all queries here? it's crippling when scaling the use of the apollo hooks.
I did a before and after performance recording in some extreme (but real) circumstances. The "change" here is that i changed the teardown method to call
stopQueryNoBroadcast
instead.Before change
After change