Currently autoFreeze is disabled by default in production builds, with the assumption that that is faster. However, since the deep pruning for drafts introduced in Immer 7, that is no longer the case; frozen data allows for an early bail out, where non frozen data doesnt. So although initial storage is slower with autofreeze, subsequent updates are faster.
By enabling auttoFreeze in all environments by default we fix a few problems:
As optimisation tip it should be made clear that shallowly pre-freezing data about to be stored can bail out of expensive deep freezing without loosing the benefits of the non-deep pruning. Maybe even export a specific preFreeze method for this reason, or reuse the existing freeze export.
@mweststrate if you are to make this change can I please ask that you maintain the ability to override this setting manually still - both globally or via the Immer class constructor. 馃檹
Would someone be able to point me in the direction of docs/discussions around the reasoning and behaviour of the deep pruning? x
@ctrlplusb yes, it will still be possible to use setAutoFreeze. I don't think there is a good summary of the discussion anywhere, but the TL,DR is: since Immer 6, Immer might do a full tree traversal of any data stored using produce; either to freeze it, or to see if there are any pending drafts.
produce accessing that data, Immer might potentially do a full traversal to see if there were any mutations in that part of the tree (often it doesn't, but it might)freeze(data) to _shallowly_ freeze data before storing it, e.g. draft.push(freeze(event)). That was already a valid optimization before, but again with the new defaults this means that these kind of optimizations do properly reflect what will happen in production as well.I appreciate the insight 馃挏
Released in 8.0.0
Most helpful comment
@ctrlplusb yes, it will still be possible to use
setAutoFreeze. I don't think there is a good summary of the discussion anywhere, but the TL,DR is: since Immer 6, Immer might do a full tree traversal of any data stored usingproduce; either to freeze it, or to see if there are any pending drafts.produceaccessing that data, Immer might potentially do a full traversal to see if there were any mutations in that part of the tree (often it doesn't, but it might)freeze(data)to _shallowly_ freeze data before storing it, e.g.draft.push(freeze(event)). That was already a valid optimization before, but again with the new defaults this means that these kind of optimizations do properly reflect what will happen in production as well.