Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: simplify process_effects #13297

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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_HAS_DIRTY_CHILDREN = 1 << 19;

export const STATE_SYMBOL = Symbol('$state');
export const STATE_SYMBOL_METADATA = Symbol('$state metadata');
Expand Down
94 changes: 40 additions & 54 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_HAS_DIRTY_CHILDREN
} from './constants.js';
import { flush_tasks } from './dom/task.js';
import { add_owner } from './dev/ownership.js';
Expand Down Expand Up @@ -582,17 +583,21 @@ export function schedule_effect(signal) {

var effect = signal;

while (effect.parent !== null) {
effect = effect.parent;
var flags = effect.f;
while (true) {
if ((effect.f & EFFECT_HAS_DIRTY_CHILDREN) !== 0) {
return;
}

var parent = effect.parent;

if ((flags & BRANCH_EFFECT) !== 0) {
if ((flags & CLEAN) === 0) return;
set_signal_status(effect, MAYBE_DIRTY);
if (parent === null) {
queued_root_effects.push(effect);
return;
}
}

queued_root_effects.push(effect);
effect.f |= EFFECT_HAS_DIRTY_CHILDREN;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is also super expensive, it's now 12ms, before the:

set_signal_status(effect, MAYBE_DIRTY);

was only 1.9ms

effect = parent;
}
}

/**
Expand All @@ -608,69 +613,50 @@ export function schedule_effect(signal) {
*/
function process_effects(effect, collected_effects) {
var current_effect = effect.first;
var effects = [];
var deferred = [];

main_loop: while (current_effect !== null) {
while (current_effect !== null) {
var flags = current_effect.f;
var is_active = (flags & INERT) === 0;
var is_branch = (flags & BRANCH_EFFECT) !== 0;
var is_clean = (flags & CLEAN) !== 0;
var child = current_effect.first;

// Skip this branch if it's clean
if (is_active && (!is_branch || !is_clean)) {
if (is_branch) {
set_signal_status(current_effect, CLEAN);
}

if ((flags & RENDER_EFFECT) !== 0) {
if (!is_branch && check_dirtiness(current_effect)) {
if ((flags & INERT) === 0) {
var has_dirty_children = (flags & EFFECT_HAS_DIRTY_CHILDREN) !== 0;
if (has_dirty_children) current_effect.f ^= EFFECT_HAS_DIRTY_CHILDREN;

if (check_dirtiness(current_effect)) {
if ((flags & RENDER_EFFECT) !== 0) {
update_effect(current_effect);
has_dirty_children = true;
} else {
deferred.push(current_effect);
}
// Child might have been mutated since running the effect or checking dirtiness
child = current_effect.first;
}

if (has_dirty_children) {
var first = current_effect.first;

if (child !== null) {
current_effect = child;
if (first) {
current_effect = first;
continue;
}
} else if ((flags & EFFECT) !== 0) {
if (is_branch || is_clean) {
if (child !== null) {
current_effect = child;
continue;
}
} else {
effects.push(current_effect);
}
}
}

var sibling = current_effect.next;

if (sibling === null) {
let parent = current_effect.parent;
while (current_effect !== null) {
var sibling = current_effect.next;

while (parent !== null) {
if (effect === parent) {
break main_loop;
}
var parent_sibling = parent.next;
if (parent_sibling !== null) {
current_effect = parent_sibling;
continue main_loop;
}
parent = parent.parent;
if (sibling) {
current_effect = sibling;
break;
}
}

current_effect = sibling;
current_effect = current_effect.parent;
}
}

// We might be dealing with many effects here, far more than can be spread into
// an array push call (callstack overflow). So let's deal with each effect in a loop.
for (var i = 0; i < effects.length; i++) {
child = effects[i];
for (var i = 0; i < deferred.length; i++) {
var child = deferred[i];
collected_effects.push(child);
process_effects(child, collected_effects);
}
Expand Down
24 changes: 12 additions & 12 deletions packages/svelte/src/reactivity/map.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,14 @@ test('map handling of undefined values', () => {
render_effect(() => {
log.push(map.get(1));
});
});

flushSync(() => {
map.delete(1);
});
flushSync(() => {
map.delete(1);
});

flushSync(() => {
map.set(1, 1);
});
flushSync(() => {
map.set(1, 1);
});

assert.deepEqual(log, [undefined, undefined, 1]);
Expand All @@ -220,14 +220,14 @@ test('not invoking reactivity when value is not in the map after changes', () =>
render_effect(() => {
log.push(map.get(2));
});
});

flushSync(() => {
map.delete(1);
});
flushSync(() => {
map.delete(1);
});

flushSync(() => {
map.set(1, 1);
});
flushSync(() => {
map.set(1, 1);
});

assert.deepEqual(log, [1, undefined, undefined, undefined, 1, undefined]);
Expand Down