-
-
Notifications
You must be signed in to change notification settings - Fork 673
Description
This is an optimization, and a code cleanup, that will also help us toward #3949.
Our Redux state tree is currently made up entirely of primitive values, plain arrays, and plain objects. These are very nice for struct-like data structures like User
or Message
. They're not so nice for key-value maps: we end up using objects as maps, and that's messier in the code and for type-checking than a proper Map
. It also turns out to be about 2x slower for building them up and making new ones, as measured in #3339 (comment) .
So, where we use an object as a map, we should use a real Map
instead. (Better, perhaps, an Immutable.Map
-- that's #3949. But this will be a step on the way.)
We've already converted lots of objects-as-maps to real Map
s in much of our code, notably in the return values of caching selectors. The reason we haven't yet done so at all in our Redux state tree is a technical obstacle that shouldn't fundamentally be complicated once we work out how, but will require some working out: we'll need to serialize and deserialize these data structures for redux-persist
.
Specifically we'll need to study the docs of redux-persist
and friends, then possibly their implementations where docs are inadequate, in order to see how to wire up appropriate serialization and deserialization functions.
Relatedly, we'll want to think through and test the migration path: what happens when a version of the app using a new data structure (for a given spot in the Redux state tree) first launches and there's existing data serialized by a version of the app that used the old data structure.
Some quick notes (adapted from notes I made after some reading in 2019-04) on possible tools for the persistence side:
- The redux-persist feature we want is "transforms": https://github.com/rt2zz/redux-persist#transforms
- In particular
remotedev-serialize
looks like a pretty good-quality implementation of serializing things likeMap
andSet
. It'll also support Immutable.js data structures if we go on to adopt those in the future (Use good data structures in Redux state, to avoid quadratic work #3949). The implementation is here:
https://github.com/kolodny/jsan/blob/master/lib/cycle.js
and enabled here:
https://github.com/zalmoxisus/remotedev-serialize/blob/master/constants/options.js
The migration will happen one data structure at a time; each one is pretty much independent of others. Most of the work will probably come as part of doing the first one, though -- figuring out just how to handle persistence and any migration, and getting that set up.