Skip to content

Commit

Permalink
chore: improve performance of scheduling effects (but without creatin…
Browse files Browse the repository at this point in the history
…g 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 <dg@domgan.com>
  • Loading branch information
Rich-Harris and trueadm authored Sep 18, 2024
1 parent dbc5793 commit b441738
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/fresh-houses-check.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: improve performance of scheduling effects
1 change: 1 addition & 0 deletions packages/svelte/src/internal/client/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
23 changes: 13 additions & 10 deletions packages/svelte/src/internal/client/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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);
}
}

Expand Down

0 comments on commit b441738

Please sign in to comment.