From edbc0e1455ec779e654a6154d3a400bcb5bf4451 Mon Sep 17 00:00:00 2001 From: tycho Date: Sat, 15 Jun 2024 21:30:40 +0800 Subject: [PATCH 1/2] refactor(effect): cache tracking value --- packages/reactivity/src/effect.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/packages/reactivity/src/effect.ts b/packages/reactivity/src/effect.ts index 727513fc66c..de03aa1cfff 100644 --- a/packages/reactivity/src/effect.ts +++ b/packages/reactivity/src/effect.ts @@ -306,14 +306,19 @@ export function triggerEffects( ) { pauseScheduling() for (const effect of dep.keys()) { + // dep.get(effect) is very expensive, we need to calculate it lazily and reuse the result + let tracking: boolean | undefined + if (!dep.computed && effect.computed) { - if (dep.get(effect) === effect._trackId && effect._runnings > 0) { + if ( + (tracking ??= dep.get(effect) === effect._trackId) && + effect._runnings > 0 + ) { effect._dirtyLevel = DirtyLevels.MaybeDirty_ComputedSideEffect_Origin continue } } - // dep.get(effect) is very expensive, we need to calculate it lazily and reuse the result - let tracking: boolean | undefined + if ( effect._dirtyLevel < dirtyLevel && (tracking ??= dep.get(effect) === effect._trackId) From c0ca9968b9b28cd1ea0c22b723434b0d681cbe1f Mon Sep 17 00:00:00 2001 From: tycho Date: Sat, 15 Jun 2024 23:11:27 +0800 Subject: [PATCH 2/2] chore: validate _runnings before tracking --- packages/reactivity/src/effect.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/reactivity/src/effect.ts b/packages/reactivity/src/effect.ts index de03aa1cfff..40868545b4b 100644 --- a/packages/reactivity/src/effect.ts +++ b/packages/reactivity/src/effect.ts @@ -311,8 +311,8 @@ export function triggerEffects( if (!dep.computed && effect.computed) { if ( - (tracking ??= dep.get(effect) === effect._trackId) && - effect._runnings > 0 + effect._runnings > 0 && + (tracking ??= dep.get(effect) === effect._trackId) ) { effect._dirtyLevel = DirtyLevels.MaybeDirty_ComputedSideEffect_Origin continue