From b4417383d6da2aef598f5791d70939b9e8f4994e Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Wed, 18 Sep 2024 05:09:07 -0400 Subject: [PATCH] chore: improve performance of scheduling effects (but without creating additional branches) (#13300) * fix: improve performance of scheduling effects * alternative fix * add comment * use same codepath * use same codepath * address feedback * tweak * more tweaks * more tweaks * address feedback * remove EFFECT_HAS_DIRTY_CHILDREN * revert * mark effect root as queued instead of adding a branch effect * revert * revert * unused * revert --------- Co-authored-by: Dominic Gannaway --- .changeset/fresh-houses-check.md | 5 ++++ .../svelte/src/internal/client/constants.js | 1 + .../svelte/src/internal/client/runtime.js | 23 +++++++++++-------- 3 files changed, 19 insertions(+), 10 deletions(-) create mode 100644 .changeset/fresh-houses-check.md diff --git a/.changeset/fresh-houses-check.md b/.changeset/fresh-houses-check.md new file mode 100644 index 0000000000..f71ae286a0 --- /dev/null +++ b/.changeset/fresh-houses-check.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: improve performance of scheduling effects diff --git a/packages/svelte/src/internal/client/constants.js b/packages/svelte/src/internal/client/constants.js index 19a412726d..35ee8292ef 100644 --- a/packages/svelte/src/internal/client/constants.js +++ b/packages/svelte/src/internal/client/constants.js @@ -18,6 +18,7 @@ export const EFFECT_TRANSPARENT = 1 << 15; export const LEGACY_DERIVED_PROP = 1 << 16; export const INSPECT_EFFECT = 1 << 17; export const HEAD_EFFECT = 1 << 18; +export const EFFECT_QUEUED = 1 << 19; export const STATE_SYMBOL = Symbol('$state'); export const STATE_SYMBOL_METADATA = Symbol('$state metadata'); diff --git a/packages/svelte/src/internal/client/runtime.js b/packages/svelte/src/internal/client/runtime.js index 2732039564..0452c8921a 100644 --- a/packages/svelte/src/internal/client/runtime.js +++ b/packages/svelte/src/internal/client/runtime.js @@ -22,7 +22,8 @@ import { BLOCK_EFFECT, ROOT_EFFECT, LEGACY_DERIVED_PROP, - DISCONNECTED + DISCONNECTED, + EFFECT_QUEUED } from './constants.js'; import { flush_tasks } from './dom/task.js'; import { add_owner } from './dev/ownership.js'; @@ -511,6 +512,10 @@ function flush_queued_root_effects(root_effects) { for (var i = 0; i < length; i++) { var effect = root_effects[i]; + if ((effect.f & EFFECT_QUEUED) !== 0) { + effect.f ^= EFFECT_QUEUED; + } + // When working with custom elements, the root effects might not have a root if (effect.first === null && (effect.f & BRANCH_EFFECT) === 0) { flush_queued_effects([effect]); @@ -595,7 +600,12 @@ export function schedule_effect(signal) { if ((flags & BRANCH_EFFECT) !== 0) { if ((flags & CLEAN) === 0) return; - set_signal_status(effect, MAYBE_DIRTY); + effect.f ^= CLEAN; + } + + if ((flags & ROOT_EFFECT) !== 0) { + if ((flags & EFFECT_QUEUED) !== 0) return; + effect.f ^= EFFECT_QUEUED; } } @@ -642,14 +652,7 @@ function process_effects(effect, collected_effects) { continue; } } else if ((flags & EFFECT) !== 0) { - if (is_branch || is_clean) { - if (child !== null) { - current_effect = child; - continue; - } - } else { - effects.push(current_effect); - } + effects.push(current_effect); } }