Skip to content
Myr

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

4 files+84 added−17 removed

modified:__performance_tests__/add-data.js+22 added-16 removed
modified:__tests__/base.js+51 added-1 removed
5 unmodified lines
6 isDraft,7 immerable,8 enableAllPlugins9} from "../src/immer"10import {each, shallowCopy, isEnumerable, DRAFT_STATE} from "../src/internal"10import {each, shallowCopy, DRAFT_STATE} from "../src/internal"11import deepFreeze from "deep-freeze"12import cloneDeep from "lodash.clonedeep"13import * as lodash from "lodash"14
960 unmodified lines
975 expect(s.foo).toBe(s.bar)976 })977 })978979 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 })987988 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 })998999 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 })10111012 const res = produce(base, draft => {1013 draft.y++1014 draft.child = wrap(draft.child)1015 draft.child.wrapped.x++1016 })10171018 expect(res).toEqual({1019 y: 2,1020 child: {wrapped: {x: 2}}1021 })1022 })10231024 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 lines
2162 }2163 })2164 return copy2165}21662167function 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 lines
21} from "./internal"22import invariant from "tiny-invariant"2324export 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 lines
82 }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 lines
139 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 lines
modified:src/scope.ts+1 added