Optimization: stop recursing if there are no drafts left
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Michel Weststrate · committed by Michel Weststrate · parent 037b474 · Browse files · 4 files·+84 added−17 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+84 added−17 removed
- tests:+51 added−1 removed
- other:+33 added−16 removed
Provenance
- Commit
ebda6f8- Tree
8c3cf5d- Author
Michel Weststrate <mweststrate@fb.com>- Committer
Michel Weststrate <mweststrate@fb.com>- Parent
037b474
4 files changed+84 added−17 removed
modified:__performance_tests__/add-data.js+22 added-16 removed
21 unmodified lines2223242526272829303145 unmodified lines7778798079818283848283848586878889879091929390919294959697989599100101102989910010310410510610710310810911011110610710811211311411521 unmodified linesconst frozenBazeState = deepFreeze(cloneDeep(baseState))const immutableJsBaseState = fromJS(baseState)const seamlessBaseState = Seamless.from(baseState)
const MAX = 10000
measure( "just mutate", () => ({draft: cloneDeep(baseState)}), ({draft}) => {45 unmodified linesmeasure("seamless-immutable + asMutable", () => { seamlessBaseState.set("data", dataSet).asMutable({deep: true})})
measure("immer (proxy) - without autofreeze", () => {measure("immer (proxy) - without autofreeze * " + MAX, () => { setUseProxies(true) setAutoFreeze(false) for (let i = 0; i < MAX; i++) produce(baseState, draft => { draft.data = dataSet }) produce(baseState, draft => { draft.data = dataSet })})
measure("immer (proxy) - with autofreeze", () => {measure("immer (proxy) - with autofreeze * " + MAX, () => { setUseProxies(true) setAutoFreeze(true) for (let i = 0; i < MAX; i++) produce(frozenBazeState, draft => { draft.data = dataSet }) produce(frozenBazeState, draft => { draft.data = dataSet })})
measure("immer (es5) - without autofreeze", () => {measure("immer (es5) - without autofreeze * " + MAX, () => { setUseProxies(false) setAutoFreeze(false) for (let i = 0; i < MAX; i++) produce(baseState, draft => { draft.data = dataSet }) produce(baseState, draft => { draft.data = dataSet })})
measure("immer (es5) - with autofreeze", () => {measure("immer (es5) - with autofreeze * " + MAX, () => { setUseProxies(false) setAutoFreeze(true) for (let i = 0; i < MAX; i++) produce(frozenBazeState, draft => { draft.data = dataSet }) produce(frozenBazeState, draft => { draft.data = dataSet })})modified:__tests__/base.js+51 added-1 removed
5 unmodified lines6 isDraft,7 immerable,8 enableAllPlugins9 } from "../src/immer"10-import {each, shallowCopy, isEnumerable, DRAFT_STATE} from "../src/internal"10+import {each, shallowCopy, DRAFT_STATE} from "../src/internal"11 import deepFreeze from "deep-freeze"12 import cloneDeep from "lodash.clonedeep"13 import * as lodash from "lodash"14 960 unmodified lines975 expect(s.foo).toBe(s.bar)976 })977 })978 979+ it("optimization: does not visit properties of new data structures if autofreeze is disabled and no drafts are unfinalized", () => {980+ const newData = {}981+ Object.defineProperty(newData, "x", {982+ enumerable: true,983+ get() {984+ throw new Error("visited!")985+ }986+ })987+988+ const run = () =>989+ produce({}, d => {990+ d.data = newData991+ })992+ if (autoFreeze) {993+ expect(run).toThrow("visited!")994+ } else {995+ expect(run).not.toThrow("visited!")996+ }997+ })998+999+ it("same optimization doesn't cause draft from nested producers to be unfinished", () => {1000+ const base = {1001+ y: 1,1002+ child: {1003+ x: 11004+ }1005+ }1006+ const wrap = produce(draft => {1007+ return {1008+ wrapped: draft1009+ }1010+ })1011+1012+ const res = produce(base, draft => {1013+ draft.y++1014+ draft.child = wrap(draft.child)1015+ draft.child.wrapped.x++1016+ })1017+1018+ expect(res).toEqual({1019+ y: 2,1020+ child: {wrapped: {x: 2}}1021+ })1022+ })1023+1024 it("supports a base state with multiple references to an object", () => {1025 const obj = {}1026 const res = produce({a: obj, b: obj}, d => {1027 // Two drafts are created for each occurrence of an object in the base state.1134 unmodified lines2162 }2163 })2164 return copy2165 }2166+2167+function isEnumerable(base, prop) {2168+ const desc = Object.getOwnPropertyDescriptor(base, prop)2169+ return desc && desc.enumerable ? true : false2170+}modified:src/finalize.ts+10 added
20 unmodified lines21 } from "./internal"22 import invariant from "tiny-invariant"23 24 export function processResult(result: any, scope: ImmerScope) {25+ scope.unfinalizedDrafts_ = scope.drafts_.length26 const baseDraft = scope.drafts_![0]27 const isReplaced = result !== undefined && result !== baseDraft28 if (!scope.immer_.useProxies_)29 getPlugin("es5").willFinalizeES5_(scope, result, isReplaced)52 unmodified lines82 }83 // Not finalized yet, let's do that now84 if (!state.finalized_) {85 state.finalized_ = true86+ state.scope_.unfinalizedDrafts_--87 const result =88 // For ES5, create a good copy from the draft first, with added keys and without deleted keys.89 state.type_ === ProxyTypeES5Object || state.type_ === ProxyTypeES5Array90 ? (state.copy_ = shallowCopy(state.draft_, true))48 unmodified lines139 return140 }141 // Search new objects for unfinalized drafts. Frozen objects should never contain drafts.142 if (isDraftable(childValue)) {143+ if (!rootScope.immer_.autoFreeze_ && rootScope.unfinalizedDrafts_ < 1) {144+ // optimization: if an object is not a draft, and we don't have to145+ // deepfreeze everything, and we are sure that no drafts are left in the remaining object146+ // cause we saw and finalized all drafts already; we can stop visiting the rest of the tree.147+ // This benefits especially adding large data tree's without further processing.148+ // See add-data.js perf test149+ return150+ }151 finalize(rootScope, childValue)152 // immer deep freezes plain objects, so if there is no parent state, we freeze as well153 if (!parentState || !parentState.scope_.parent_)154 maybeFreeze(rootScope, childValue)8 unmodified linesmodified:src/scope.ts+1 added
15 unmodified lines16171819202122232444 unmodified lines15 unmodified lines drafts_: any[] parent_?: ImmerScope patchListener_?: PatchListener immer_: Immer unfinalizedDrafts_ = 0
constructor(parent: ImmerScope | undefined, immer: Immer) { this.drafts_ = [] this.parent_ = parent44 unmodified lines