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: improve signal perf by using Set rather than array for reactions #12831

Merged
merged 5 commits into from
Aug 14, 2024
Merged
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
5 changes: 5 additions & 0 deletions .changeset/calm-clocks-raise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

chore: improve signal perf by using Set rather than array for reactions
4 changes: 1 addition & 3 deletions packages/svelte/src/internal/client/reactivity/sources.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,8 @@ function mark_reactions(signal, status) {
if (reactions === null) return;

var runes = is_runes();
var length = reactions.length;

for (var i = 0; i < length; i++) {
var reaction = reactions[i];
for (var reaction of reactions) {
var flags = reaction.f;

// Skip any effects that are already dirty
Expand Down
2 changes: 1 addition & 1 deletion packages/svelte/src/internal/client/reactivity/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export interface Signal {

export interface Value<V = unknown> extends Signal {
/** Signals that read from this signal */
reactions: null | Reaction[];
reactions: null | Set<Reaction>;
/** Equality function */
equals: Equals;
/** The latest value for this signal */
Expand Down
36 changes: 9 additions & 27 deletions packages/svelte/src/internal/client/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ export function check_dirtiness(reaction) {

if ((flags & DISCONNECTED) !== 0) {
for (i = 0; i < dependencies.length; i++) {
(dependencies[i].reactions ??= []).push(reaction);
(dependencies[i].reactions ??= new Set()).add(reaction);
}

reaction.f ^= DISCONNECTED;
Expand All @@ -188,11 +188,11 @@ export function check_dirtiness(reaction) {

if (is_unowned) {
// TODO is there a more logical place to do this work?
if (!current_skip_reaction && !dependency?.reactions?.includes(reaction)) {
if (!current_skip_reaction && !dependency?.reactions?.has(reaction)) {
// If we are working with an unowned signal as part of an effect (due to !current_skip_reaction)
// and the version hasn't changed, we still need to check that this reaction
// if linked to the dependency source – otherwise future updates will not be caught.
(dependency.reactions ??= []).push(reaction);
(dependency.reactions ??= new Set()).add(reaction);
}
}
}
Expand Down Expand Up @@ -323,17 +323,7 @@ export function update_reaction(reaction) {

if (!current_skip_reaction) {
for (i = skipped_deps; i < deps.length; i++) {
dependency = deps[i];
var reactions = dependency.reactions;

if (reactions === null) {
dependency.reactions = [reaction];
} else if (
reactions[reactions.length - 1] !== reaction &&
!reactions.includes(reaction)
) {
reactions.push(reaction);
}
(deps[i].reactions ??= new Set()).add(reaction);
}
}
} else if (deps !== null && skipped_deps < deps.length) {
Expand All @@ -358,24 +348,16 @@ export function update_reaction(reaction) {
* @returns {void}
*/
function remove_reaction(signal, dependency) {
const reactions = dependency.reactions;
let reactions_length = 0;
let reactions = dependency.reactions;
if (reactions !== null) {
reactions_length = reactions.length - 1;
const index = reactions.indexOf(signal);
if (index !== -1) {
if (reactions_length === 0) {
dependency.reactions = null;
} else {
// Swap with last element and then remove.
reactions[index] = reactions[reactions_length];
reactions.pop();
}
reactions.delete(signal);
if (reactions.size === 0) {
reactions = dependency.reactions = null;
}
}
// If the derived has no reactions, then we can disconnect it from the graph,
// allowing it to either reconnect in the future, or be GC'd by the VM.
if (reactions_length === 0 && (dependency.f & DERIVED) !== 0) {
if (reactions === null && (dependency.f & DERIVED) !== 0) {
set_signal_status(dependency, MAYBE_DIRTY);
// If we are working with a derived that is owned by an effect, then mark it as being
// disconnected.
Expand Down
16 changes: 8 additions & 8 deletions packages/svelte/tests/signals/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,13 +236,13 @@ describe('signals', () => {
return () => {
flushSync(() => set(count, 1));
// Ensure we're not leaking consumers
assert.deepEqual(count.reactions?.length, 1);
assert.deepEqual(count.reactions?.size, 1);
flushSync(() => set(count, 2));
// Ensure we're not leaking consumers
assert.deepEqual(count.reactions?.length, 1);
assert.deepEqual(count.reactions?.size, 1);
flushSync(() => set(count, 3));
// Ensure we're not leaking consumers
assert.deepEqual(count.reactions?.length, 1);
assert.deepEqual(count.reactions?.size, 1);
assert.deepEqual(log, [0, 1, 2, 3]);
};
});
Expand Down Expand Up @@ -304,8 +304,8 @@ describe('signals', () => {
flushSync(() => set(count, 4));
flushSync(() => set(count, 0));
// Ensure we're not leaking consumers
assert.deepEqual(count.reactions?.length, 1);
assert.deepEqual(calc.reactions?.length, 1);
assert.deepEqual(count.reactions?.size, 1);
assert.deepEqual(calc.reactions?.size, 1);
assert.deepEqual(log, [0, 2, 'limit', 0]);
destroy();
// Ensure we're not leaking consumers
Expand Down Expand Up @@ -484,7 +484,7 @@ describe('signals', () => {
return () => {
flushSync();
assert.equal(a?.deps?.length, 1);
assert.equal(state?.reactions?.length, 1);
assert.equal(state?.reactions?.size, 1);
destroy();
assert.equal(a?.deps?.length, 1);
assert.equal(state?.reactions, null);
Expand Down Expand Up @@ -658,12 +658,12 @@ describe('signals', () => {
});

assert.equal($.get(d), 0);
assert.equal(count.reactions?.length, 1);
assert.equal(count.reactions?.size, 1);
assert.equal(d.deps?.length, 1);

set(count, 1);
assert.equal($.get(d), 2);
assert.equal(count.reactions?.length, 1);
assert.equal(count.reactions?.size, 1);
assert.equal(d.deps?.length, 1);

destroy();
Expand Down
Loading