From 344d458fd2aaff4af791e89fce19f22101790741 Mon Sep 17 00:00:00 2001 From: David Maskasky Date: Fri, 5 Dec 2025 00:14:34 -0800 Subject: [PATCH 1/3] only mounted dependents can be added to mounted.t --- src/vanilla/internals.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/vanilla/internals.ts b/src/vanilla/internals.ts index 44a3de4590..3cf1053ced 100644 --- a/src/vanilla/internals.ts +++ b/src/vanilla/internals.ts @@ -303,9 +303,7 @@ function getMountedOrPendingDependents( ): Set { const dependents = new Set() for (const a of mountedMap.get(atom)?.t || []) { - if (mountedMap.has(a)) { - dependents.add(a) - } + dependents.add(a) } for (const atomWithPendingPromise of atomState.p) { dependents.add(atomWithPendingPromise) @@ -598,7 +596,9 @@ const readAtomState: ReadAtomState = (store, atom) => { if (isPendingPromise(atomState.v)) { addPendingPromiseToDependency(atom, atomState.v, aState) } - mountedMap.get(a)?.t.add(atom) + if (mountedMap.has(atom)) { + mountedMap.get(a)?.t.add(atom) + } if (!isSync) { mountDependenciesIfAsync() } From fbb23df0c72f08af043805aa0642d1877d385dec Mon Sep 17 00:00:00 2001 From: David Maskasky Date: Fri, 5 Dec 2025 00:34:05 -0800 Subject: [PATCH 2/3] improve perf --- src/vanilla/internals.ts | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/src/vanilla/internals.ts b/src/vanilla/internals.ts index 3cf1053ced..e693f96a84 100644 --- a/src/vanilla/internals.ts +++ b/src/vanilla/internals.ts @@ -553,14 +553,14 @@ const readAtomState: ReadAtomState = (store, atom) => { } // Otherwise, check if the dependencies have changed. // If all dependencies haven't changed, we can use the cache. - if ( - Array.from(atomState.d).every( - ([a, n]) => - // Recursively, read the atom state of the dependency, and - // check if the atom epoch number is unchanged - readAtomState(store, a).n === n, - ) - ) { + let hasChangedDeps = false + for (const [a, n] of atomState.d) { + if (readAtomState(store, a).n !== n) { + hasChangedDeps = true + break + } + } + if (!hasChangedDeps) { return atomState } } @@ -771,7 +771,7 @@ const mountDependencies: MountDependencies = (store, atom) => { } } } - for (const a of mounted.d || []) { + for (const a of mounted.d) { if (!atomState.d.has(a)) { mounted.d.delete(a) const aMounted = unmountAtom(store, a) @@ -854,11 +854,17 @@ const unmountAtom: UnmountAtom = (store, atom) => { const unmountAtom = buildingBlocks[19] const atomState = ensureAtomState(store, atom) let mounted = mountedMap.get(atom) - if ( - mounted && - !mounted.l.size && - !Array.from(mounted.t).some((a) => mountedMap.get(a)?.d.has(atom)) - ) { + if (!mounted || mounted.l.size) { + return mounted + } + let isDependent = false + for (const a of mounted.t) { + if (mountedMap.get(a)?.d.has(atom)) { + isDependent = true + break + } + } + if (isDependent) { // unmount self if (mounted.u) { unmountCallbacks.add(mounted.u) From e89dd4053cf5639ae3ae6f1f11c9e3e04b8c380f Mon Sep 17 00:00:00 2001 From: David Maskasky Date: Fri, 5 Dec 2025 10:27:17 -0800 Subject: [PATCH 3/3] fix failing test by inverting isDependent condition --- src/vanilla/internals.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/vanilla/internals.ts b/src/vanilla/internals.ts index e693f96a84..1926a51338 100644 --- a/src/vanilla/internals.ts +++ b/src/vanilla/internals.ts @@ -864,7 +864,7 @@ const unmountAtom: UnmountAtom = (store, atom) => { break } } - if (isDependent) { + if (!isDependent) { // unmount self if (mounted.u) { unmountCallbacks.add(mounted.u)