Skip to content

Commit 3ece9cd

Browse files
trueadmRich-Harris
andauthored
chore: remove deopts and refactor code for controlled optimizations (#11040)
* chore: remove deopts and refactor code for controlled optimizations * remove comment * tune * tweak * tweak * typo * get rid of single-use variable --------- Co-authored-by: Rich Harris <[email protected]>
1 parent d85d5a0 commit 3ece9cd

File tree

2 files changed

+48
-19
lines changed

2 files changed

+48
-19
lines changed

packages/svelte/src/internal/client/dom/blocks/each.js

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ import {
1717
branch,
1818
effect,
1919
pause_effect,
20+
get_out_transitions,
21+
resume_effect,
2022
pause_effects,
21-
resume_effect
23+
destroy_effects
2224
} from '../../reactivity/effects.js';
2325
import { source, mutable_source, set } from '../../reactivity/sources.js';
2426
import { is_array, is_frozen, map_get, map_set } from '../../utils.js';
@@ -244,9 +246,17 @@ function reconcile_indexed_array(array, state, anchor, render_fn, flags) {
244246
effects.push(a_items[i].e);
245247
}
246248

247-
pause_effects(effects, () => {
248-
state.items.length = b;
249-
});
249+
var transitions = get_out_transitions(effects);
250+
var items = state.items;
251+
252+
if (transitions.length === 0) {
253+
destroy_effects(effects);
254+
items.length = b;
255+
} else {
256+
pause_effects(effects, transitions, () => {
257+
items.length = b;
258+
});
259+
}
250260
}
251261
}
252262

@@ -421,11 +431,16 @@ function reconcile_tracked_array(array, state, anchor, render_fn, flags, keys) {
421431
});
422432
}
423433

424-
// TODO: would be good to avoid this closure in the case where we have no
425-
// transitions at all. It would make it far more JIT friendly in the hot cases.
426-
pause_effects(to_destroy, () => {
434+
var transitions = get_out_transitions(to_destroy);
435+
436+
if (transitions.length === 0) {
437+
destroy_effects(to_destroy);
427438
state.items = b_items;
428-
});
439+
} else {
440+
pause_effects(to_destroy, transitions, () => {
441+
state.items = b_items;
442+
});
443+
}
429444
}
430445

431446
/**

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

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -313,23 +313,38 @@ export function pause_effect(effect, callback = noop) {
313313
* Pause multiple effects simultaneously, and coordinate their
314314
* subsequent destruction. Used in each blocks
315315
* @param {import('#client').Effect[]} effects
316-
* @param {() => void} callback
316+
* @returns {import('#client').TransitionManager[]}
317317
*/
318-
export function pause_effects(effects, callback = noop) {
318+
export function get_out_transitions(effects) {
319319
/** @type {import('#client').TransitionManager[]} */
320320
var transitions = [];
321-
var length = effects.length;
322321

323-
for (var i = 0; i < length; i++) {
322+
for (var i = 0; i < effects.length; i++) {
324323
pause_children(effects[i], transitions, true);
325324
}
326325

327-
// TODO: would be good to avoid this closure in the case where we have no
328-
// transitions at all. It would make it far more JIT friendly in the hot cases.
326+
return transitions;
327+
}
328+
329+
/**
330+
* @param {import('#client').Effect[]} effects
331+
*/
332+
export function destroy_effects(effects) {
333+
for (var i = 0; i < effects.length; i++) {
334+
destroy_effect(effects[i]);
335+
}
336+
}
337+
338+
/**
339+
* Pause multiple effects simultaneously, and coordinate their
340+
* subsequent destruction. Used in each blocks
341+
* @param {import('#client').Effect[]} effects
342+
* @param {import('#client').TransitionManager[]} transitions
343+
* @param {() => void} callback
344+
*/
345+
export function pause_effects(effects, transitions, callback = noop) {
329346
out(transitions, () => {
330-
for (var i = 0; i < length; i++) {
331-
destroy_effect(effects[i]);
332-
}
347+
destroy_effects(effects);
333348
callback();
334349
});
335350
}
@@ -370,13 +385,12 @@ function pause_children(effect, transitions, local) {
370385
var child = effect.first;
371386

372387
while (child !== null) {
373-
var sibling = child.next;
374388
var transparent = (child.f & IS_ELSEIF) !== 0 || (child.f & BRANCH_EFFECT) !== 0;
375389
// TODO we don't need to call pause_children recursively with a linked list in place
376390
// it's slightly more involved though as we have to account for `transparent` changing
377391
// through the tree.
378392
pause_children(child, transitions, transparent ? local : false);
379-
child = sibling;
393+
child = child.next;
380394
}
381395
}
382396

0 commit comments

Comments
 (0)