Skip to content

Commit 7e4064c

Browse files
committed
perf: don't reschedule the whole batch on writes
This makes flushing effects much faster in scenarios where there's a lot of effects and many of the write to state. We added this "bail and reschedule" mechanism mainly to prevent subsequent effects from being called when a prior effect execution did e.g. cause an if block to become false and the effect about to run next therefore being destroyed. This change addresses that issue by flushing the newly created batch right away, but only executing its branches. Through this we also get rid of the false-positive loop protection error. The one downside is that effect execution ordering is slightly different: Only things scheduled within the current batch are executed in order, all new (pre-)effects will run afterwards. The worst thing that can happen as a result is rerunning render or user effects more often than they need to, but I expect this to be minimal in comparison to the perf wins. Fixes #16548
1 parent 2e02868 commit 7e4064c

File tree

1 file changed

+26
-12
lines changed
  • packages/svelte/src/internal/client/reactivity

1 file changed

+26
-12
lines changed

packages/svelte/src/internal/client/reactivity/batch.js

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,10 @@ export class Batch {
173173
skipped_effects = new Set();
174174

175175
/**
176-
*
177176
* @param {Effect[]} root_effects
177+
* @param {boolean} block_effects_only
178178
*/
179-
process(root_effects) {
179+
process(root_effects, block_effects_only = false) {
180180
queued_root_effects = [];
181181

182182
previous_batch = null;
@@ -187,7 +187,7 @@ export class Batch {
187187
// if there are multiple batches, we are 'time travelling' —
188188
// we need to undo the changes belonging to any batch
189189
// other than the current one
190-
if (batches.size > 1) {
190+
if (batches.size > (block_effects_only ? 2 : 1)) {
191191
current_values = new Map();
192192
batch_deriveds = new Map();
193193

@@ -209,7 +209,12 @@ export class Batch {
209209
}
210210

211211
for (const root of root_effects) {
212-
this.#traverse_effect_tree(root);
212+
this.#traverse_effect_tree(root, block_effects_only);
213+
}
214+
215+
if (block_effects_only) {
216+
queued_root_effects = root_effects;
217+
return;
213218
}
214219

215220
// if we didn't start any new async work, and no async work
@@ -276,9 +281,12 @@ export class Batch {
276281
* Traverse the effect tree, executing effects or stashing
277282
* them for later execution as appropriate
278283
* @param {Effect} root
284+
* @param {boolean} block_effects_only
279285
*/
280-
#traverse_effect_tree(root) {
281-
root.f ^= CLEAN;
286+
#traverse_effect_tree(root, block_effects_only) {
287+
if (!block_effects_only) {
288+
root.f ^= CLEAN;
289+
}
282290

283291
var effect = root.first;
284292

@@ -290,7 +298,11 @@ export class Batch {
290298
var skip = is_skippable_branch || (flags & INERT) !== 0 || this.skipped_effects.has(effect);
291299

292300
if (!skip && effect.fn !== null) {
293-
if (is_branch) {
301+
if (block_effects_only) {
302+
if ((flags & BLOCK_EFFECT) !== 0 && (flags & CLEAN) === 0) {
303+
update_effect(effect);
304+
}
305+
} else if (is_branch) {
294306
effect.f ^= CLEAN;
295307
} else if ((flags & CLEAN) === 0) {
296308
if ((flags & EFFECT) !== 0) {
@@ -396,6 +408,12 @@ export class Batch {
396408
this.deactivate();
397409
}
398410

411+
flush_block_effects() {
412+
if (queued_root_effects.length > 0) {
413+
this.process(queued_root_effects, true);
414+
}
415+
}
416+
399417
/**
400418
* Append and remove branches to/from the DOM
401419
*/
@@ -626,14 +644,10 @@ function flush_queued_effects(effects) {
626644
current_batch.current.size > n &&
627645
(effect.f & USER_EFFECT) !== 0
628646
) {
629-
break;
647+
current_batch.flush_block_effects();
630648
}
631649
}
632650
}
633-
634-
while (i < length) {
635-
schedule_effect(effects[i++]);
636-
}
637651
}
638652

639653
/**

0 commit comments

Comments
 (0)