Site-kit-wp: Default reducers causing unnecessary datastore updates

Created on 17 Sep 2020  ·  3Comments  ·  Source: google/site-kit-wp

Bug Description

One of the features of the wp.data architecture is that registry listener functions (registered with registry.subscribe()) are only invoked on state _changes_ rather than on every dispatched action. Internally, the registry uses a simple === equality check to determine if the state has changed, and if so it calls all the subscribed listeners.

In our datastore reducers, we currently return a copy state (i.e. { ...state } in the default case for the switch, rather than returning the given state itself. This undermines the optimization we would otherwise get if we returned state un-copied instead.

Steps to reproduce

This issue is has no observable side-effects, but it can be reproduced with tests. The condition which causes it is per-store however so we cannot for example add a single test to catch this across all stores.


_Do not alter or remove anything below. The following sections will be managed by moderators only._

Acceptance criteria

  • All reducer functions used in all data stores should return the given state object if there are no changes to make from a matched action

Implementation Brief

  • Search for { ...state } and replace with state
  • Merge #2053

QA Brief

  • Should be a clean refactor with no observable changes in behavior
  • JS tests should pass – there is an added test to cover this for all stores so an engineer only need review this

    • It would be good to verify this test works as expected by temporarily changing the default case in a reducer to use the copied state and ensure the test causes the expected failure

Changelog entry

  • Improve performance of datastores by avoiding unnecessary datastore updates.
P1 Eng Bug

All 3 comments

IB ✅

Code reviewed as well, just assigning to you for merge 👍🏻

All looks good. Checked codebase and haven't found occurrences of { ...state }. There are no visual changes on the frontend. All tests are passed and I can confirm that added tests fails if I add { ...state } to a random datastore reducer. QA :heavy_check_mark:

Was this page helpful?
0 / 5 - 0 ratings