Skip to content

Commit bbd1a6c

Browse files
authored
chore: tune signals for better runtime perf (#9529)
1 parent a36dba7 commit bbd1a6c

File tree

3 files changed

+12
-22
lines changed

3 files changed

+12
-22
lines changed

.changeset/thirty-ghosts-fix.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
chore: tweak signals for better runtime perf

packages/svelte/src/internal/client/runtime.js

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,6 @@ export let current_effect = null;
5555
/** @type {null | import('./types.js').Signal[]} */
5656
let current_dependencies = null;
5757
let current_dependencies_index = 0;
58-
// Used to prevent over-subscribing dependencies on a consumer
59-
let current_consumer_read_clock = 1;
60-
let current_read_clock = 1;
6158
// Handling capturing of signals from object property getters
6259
let current_should_capture_signal = false;
6360
/** If `true`, `get`ting the signal should not register it as a dependency */
@@ -153,7 +150,6 @@ function create_signal_object(flags, value, block) {
153150
equals: null,
154151
flags,
155152
init: null,
156-
read: 0,
157153
references: null,
158154
value
159155
};
@@ -190,13 +186,15 @@ function is_signal_dirty(signal) {
190186
let i;
191187
for (i = 0; i < length; i++) {
192188
const dependency = dependencies[i];
189+
const dep_flags = dependency.flags;
193190

194-
if ((dependency.flags & MAYBE_DIRTY) !== 0 && !is_signal_dirty(dependency)) {
191+
if ((dep_flags & MAYBE_DIRTY) !== 0 && !is_signal_dirty(dependency)) {
195192
set_signal_status(dependency, CLEAN);
196193
continue;
197194
}
198-
if ((dependency.flags & DIRTY) !== 0 || dependency.value === UNINITIALIZED) {
199-
if ((dependency.flags & DERIVED) !== 0) {
195+
// The flags can be marked as dirty from the above is_signal_dirty call.
196+
if ((dependency.flags & DIRTY) !== 0) {
197+
if ((dep_flags & DERIVED) !== 0) {
200198
update_derived(dependency, true);
201199
// Might have been mutated from above get.
202200
if ((signal.flags & DIRTY) !== 0) {
@@ -221,7 +219,6 @@ function execute_signal_fn(signal) {
221219
const init = signal.init;
222220
const previous_dependencies = current_dependencies;
223221
const previous_dependencies_index = current_dependencies_index;
224-
const previous_consumer_read_clock = current_consumer_read_clock;
225222
const previous_consumer = current_consumer;
226223
const previous_block = current_block;
227224
const previous_component_context = current_component_context;
@@ -230,12 +227,6 @@ function execute_signal_fn(signal) {
230227
const previous_untracking = current_untracking;
231228
current_dependencies = /** @type {null | import('./types.js').Signal[]} */ (null);
232229
current_dependencies_index = 0;
233-
if (current_read_clock === MAX_SAFE_INT) {
234-
current_read_clock = 1;
235-
} else {
236-
current_read_clock++;
237-
}
238-
current_consumer_read_clock = current_read_clock;
239230
current_consumer = signal;
240231
current_block = signal.block;
241232
current_component_context = signal.context;
@@ -290,7 +281,6 @@ function execute_signal_fn(signal) {
290281
} finally {
291282
current_dependencies = previous_dependencies;
292283
current_dependencies_index = previous_dependencies_index;
293-
current_consumer_read_clock = previous_consumer_read_clock;
294284
current_consumer = previous_consumer;
295285
current_block = previous_block;
296286
current_component_context = previous_component_context;
@@ -427,7 +417,7 @@ function flush_queued_effects(effects) {
427417
for (i = 0; i < length; i++) {
428418
const signal = effects[i];
429419
const flags = signal.flags;
430-
if ((flags & DESTROYED) === 0 && (flags & INERT) === 0) {
420+
if ((flags & (DESTROYED | INERT)) === 0) {
431421
if (is_signal_dirty(signal)) {
432422
set_signal_status(signal, CLEAN);
433423
execute_effect(signal);
@@ -744,12 +734,9 @@ export function get(signal) {
744734
current_dependencies_index++;
745735
} else if (current_dependencies === null) {
746736
current_dependencies = [signal];
747-
} else if (signal.read !== current_consumer_read_clock) {
737+
} else if (signal !== current_dependencies.at(-1)) {
748738
current_dependencies.push(signal);
749739
}
750-
if (!unowned) {
751-
signal.read = current_consumer_read_clock;
752-
}
753740
}
754741

755742
if ((flags & DERIVED) !== 0 && is_signal_dirty(signal)) {

packages/svelte/src/internal/client/types.d.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ export type Signal<V = unknown> = {
6363
flags: SignalFlags;
6464
/** The function that we invoke for effects and computeds */
6565
init: null | (() => V) | (() => void | (() => void)) | ((b: Block) => void | (() => void));
66-
/** The read clock from the given context the signal was read/written to */
67-
read: number;
6866
/** Anything that a signal owns */
6967
references: null | Signal[];
7068
/** The latest value for this signal, doubles as the teardown for effects */

0 commit comments

Comments
 (0)