Skip to content

Try moving RTKQ subscription state handling from a reducer into the middleware #2682

@markerikson

Description

@markerikson

Currently, the biggest factor impacting RTKQ perf is that we dispatch some action for every hook that mounts, in order to track subscription state.

In v1.9 alpha, we initially tried adding a batching middleware that intercepts some of those actions and groups them into a single parent action. That helped, but it was single-purpose and could be seen as altering behavior. We've also been trying out an "auto-batch enhancer" that delays notifications, but that doesn't improve perf as much as the middleware.

We've come up with an alternate idea: track subscription status inside the middleware itself, rather than in the reducer. This would avoid the perf hit from all these actions being processed by the reducer (and also all the spam in the Redux DevTools action log).

Some caveats / things to consider:

  • We're already tracking several other pieces of state in the middleware anyway (timeouts, whatever). We should consider adding a data structure that collects all these.
  • It probably needs to be on a per-store basis somehow, since it's possible for one middleware instance to be used with many store instances (including in an SSR scenario)
  • This may actually make SSR rehydration easier (?)
  • The RTKQ DevTools panel does currently show a "Subs" tab with this data. We should consider flushing a subscriptionsUpdated action periodically so that the state still exists

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions