Skip to content
Open
Show file tree
Hide file tree
Changes from 51 commits
Commits
Show all changes
93 commits
Select commit Hold shift + click to select a range
d28ea11
wip
dummdidumm Oct 15, 2026
4a22781
speed up / reduce code size a bit / fix error bug
dummdidumm Oct 16, 2026
0a1b1c2
tweak implementation, reduce concepts a bit
dummdidumm Oct 16, 2026
ab9863c
fix
dummdidumm Oct 16, 2026
a996f40
simplify/fix
dummdidumm Oct 16, 2026
508b190
merge dirty_reactions into unprocessed, comments
dummdidumm Oct 16, 2026
6d3317f
simplify/comments
dummdidumm Oct 16, 2026
06f7ec0
bring over tests that pass unchanged
dummdidumm Oct 16, 2026
fd71c0c
tmp: commit md file
dummdidumm Oct 16, 2026
f9f01bd
turn references into crosslinks
Rich-Harris Oct 17, 2026
39aa80d
define folding
Rich-Harris Oct 17, 2026
b92bc88
note possible future simplification (simon, feel free to revert if th…
Rich-Harris Oct 17, 2026
7e395a1
commit some adjusted tests (some names outdated, some superfluous bec…
dummdidumm Oct 17, 2026
52909d7
fix tricky "assigned deriveds" cases
dummdidumm Oct 18, 2026
f1e0759
fix unskip logic, avoid needless reruns when derived's earlier value …
dummdidumm Oct 19, 2026
aeefd9c
each bug fix
dummdidumm Oct 19, 2026
847f648
note found bug, don't fix now but add skipped tests
dummdidumm Oct 19, 2026
268b74d
bring over two more passing tests from pt-1 branch
dummdidumm Oct 19, 2026
abd8bb9
(partially) fix behavior when pending snippet exists
dummdidumm Oct 19, 2026
f0dee8a
wait on the batch at the run/flatten creation time to resume with the…
dummdidumm Oct 19, 2026
75b6957
forgot this test
dummdidumm Oct 19, 2026
4b481d3
fix + simplify derived assignments, move cache deriveds out of batch_…
dummdidumm Oct 20, 2026
8eaa2d7
tidy up
dummdidumm Oct 20, 2026
32c62ea
fix/simplify/consolidate error handling
dummdidumm Oct 20, 2026
e4927f2
cleanup
dummdidumm Oct 20, 2026
f7a5597
code-golf
dummdidumm Oct 20, 2026
82b4298
tweak
dummdidumm Oct 20, 2026
b202c4c
prune effect edges
dummdidumm Oct 20, 2026
d3d995a
more code golf
dummdidumm Oct 20, 2026
b3d3a0a
Merge branch 'main' into batch-views
dummdidumm Oct 20, 2026
9561f67
tweak/fix pending boundary logic, still not super happy with the outcome
dummdidumm Oct 21, 2026
edc48da
remove obsolete test
dummdidumm Oct 21, 2026
2c94751
making tracing view-aware
dummdidumm Oct 21, 2026
272e649
use deps here
dummdidumm Oct 21, 2026
7d1e346
fix effect resume bug
dummdidumm Oct 21, 2026
ecd3fa9
merge main
Rich-Harris Oct 21, 2026
7b9b639
merge main
Rich-Harris Oct 21, 2026
a62c812
Apply suggestions from code review
Rich-Harris Oct 22, 2026
0586820
chore: DRY out dirty reaction code (#19074)
Rich-Harris Oct 22, 2026
4e5654c
chore: skip eager effects during traversal (#19079)
Rich-Harris Oct 22, 2026
611f512
add test that fails on `batch-views-simplify-capture`
Rich-Harris Oct 22, 2026
b2fc8db
fix test
Rich-Harris Oct 23, 2026
33718ef
Merge branch 'main' into batch-views
Rich-Harris Oct 23, 2026
b96b9ea
merge main
Rich-Harris Oct 23, 2026
729c983
Update packages/svelte/src/internal/client/reactivity/batch.js
Rich-Harris Oct 23, 2026
f47ce78
Update packages/svelte/src/internal/client/runtime.js
Rich-Harris Oct 23, 2026
9e9c54c
Update packages/svelte/src/internal/client/reactivity/batch.js
Rich-Harris Oct 23, 2026
96f1e7c
tweak
Rich-Harris Oct 23, 2026
7e127b3
tweak
Rich-Harris Oct 23, 2026
dc26210
fix: don't run `$inspect` and `$state.eager` with the values of a for…
Rich-Harris Oct 23, 2026
6cd2bc5
chore: DRY out update_derived callsites (#19110)
Rich-Harris Oct 28, 2026
14c896a
merge main
Rich-Harris Oct 28, 2026
42f278f
fix: revert state changes in effect teardowns based on the currently …
Rich-Harris Oct 28, 2026
4ef7a45
chore: bypass internal_set when handling async results (#19111)
Rich-Harris Oct 28, 2026
e3e2cd3
fix: don't re-run `$inspect` effects if values are unchanged (#19077)
Rich-Harris Oct 28, 2026
42bc77d
chore: dedupe source.v etc assignments (#19115)
Rich-Harris Oct 28, 2026
58ebd5b
Update packages/svelte/src/internal/client/runtime.js
Rich-Harris Oct 28, 2026
867262b
prettier
Rich-Harris Oct 29, 2026
387cda6
perf: don't re-validate branches that were just created offscreen (#1…
Rich-Harris Oct 29, 2026
2e54086
perf: don't revalidate a batch that was the most recent to be process…
Rich-Harris Oct 29, 2026
d239ba4
fix: don't touch the active batch's records when running in the real …
Rich-Harris Oct 29, 2026
25bce04
fix: don't throw when committing a fork whose changes were all supers…
Rich-Harris Oct 29, 2026
c678d4e
fix: reset view if fork callback throws (#19120)
Rich-Harris Oct 29, 2026
0f8e6cd
chore: document that the active view sees writes made while it is act…
Rich-Harris Oct 29, 2026
ece1e40
docs: fix various errors in `batch-views.md` (#19127)
Rich-Harris Oct 30, 2026
bc53186
fix: don't re-run effects that already saw a write made while a batch…
Rich-Harris Oct 30, 2026
23b3898
fix: rebase later batches whose writes are all among those of a commi…
Rich-Harris Oct 30, 2026
931b389
Merge branch 'main' into batch-views
dummdidumm Oct 30, 2026
fb0058a
Merge branch 'main' into batch-views
dummdidumm Nov 2, 2026
f0486b8
fix: don't let teardowns see a derived's computed value from before a…
Rich-Harris Nov 2, 2026
6ae1805
chore: make it explicit that update_derived is called with sources (#…
Rich-Harris Nov 2, 2026
9dd642c
chore: reduce number of `current_batch` assignments, remove `previous…
Rich-Harris Nov 2, 2026
40e9ac8
chore: document that a batch's first-value writes have no previous en…
Rich-Harris Nov 2, 2026
cd5baf5
chore: defer all effects while another batch commits, regardless of a…
Rich-Harris Nov 2, 2026
a657999
fix: correctly preserve/discard each item effects in forks (#19167)
Rich-Harris Nov 4, 2026
a09517e
fix: in `batch.current`, always specify whether a value was assigned …
Rich-Harris Nov 4, 2026
26de2ef
chore: make `e` and `assigned` required properties of `BatchValue` (#…
Rich-Harris Nov 4, 2026
4300a59
fix: keep assignments to deriveds whose derived dependencies are eval…
Rich-Harris Nov 4, 2026
50d6f08
chore: revalidate without recursion (#19060)
Rich-Harris Nov 5, 2026
3653d77
chore: simplify `update_dependencies` (#19182)
Rich-Harris Nov 5, 2026
a391635
chore: simplify `update_record` (#19185)
Rich-Harris Nov 6, 2026
33d2b8f
chore: use `should_record` boolean inside `update_record` (#19189)
Rich-Harris Nov 6, 2026
c8513b9
fix: don't commit branches eagerly during flush (#19221)
dummdidumm Nov 9, 2026
51718d6
fix: move batches that committed with pending work to the front (#19235)
Rich-Harris Nov 9, 2026
784fcd4
fix: don't let forks take over async results (#19234)
dummdidumm Nov 10, 2026
4f34b11
fix: better abort signal handling (#19168)
Rich-Harris Nov 10, 2026
54c3364
fix: properly support `{#await }` blocks in async mode (#19236)
dummdidumm Nov 10, 2026
c829f8a
Merge branch 'main' into batch-views
dummdidumm Nov 11, 2026
b8e262c
fix: don't override newer values on fork commit (#19244)
dummdidumm Nov 13, 2026
e7ea0c4
fix: create effects in `flatten` whenever values are ready (#19242)
Rich-Harris Nov 14, 2026
0a3f505
fix: don't bail out of internal_set if value matches real world but d…
Rich-Harris Nov 14, 2026
4a669eb
obsolete comment
Rich-Harris Nov 14, 2026
ef13a2b
Merge branch 'main' into batch-views
Rich-Harris Nov 15, 2026
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/exact-batch-versions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: keep values computed in a batch's view of the world local to that batch, and don't needlessly re-run deriveds while several batches are pending
5 changes: 5 additions & 0 deletions .changeset/quiet-forks-inspect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: don't run `$inspect` and `$state.eager` with the values of a fork before it is committed
601 changes: 601 additions & 0 deletions batch-views.md

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions packages/svelte/src/internal/client/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ export const ASYNC = 1 << 22;

export const ERROR_VALUE = 1 << 23;

// Flags exclusive to deriveds
/** The global state of the derived is a value that was assigned to it, rather than a run of it */
export const ASSIGNED = 1 << 26;

export const STATE_SYMBOL = Symbol('$state');
/** Marks component export objects, so that `proxy(...)` leaves them untouched */
export const COMPONENT_SYMBOL = Symbol('component');
Expand Down
12 changes: 6 additions & 6 deletions packages/svelte/src/internal/client/dev/debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,11 @@ export function log_effect_tree(effect, highlighted = [], depth = 0, is_reachabl
console.groupEnd();
}

if (effect.deps !== null) {
if (effect.edges !== null) {
Comment thread
Rich-Harris marked this conversation as resolved.
Resolved
// eslint-disable-next-line no-console
console.groupCollapsed('%cdeps', 'font-weight: normal');

for (const dep of effect.deps) {
for (const dep of effect.edges) {
log_dep(dep);
}

Expand Down Expand Up @@ -163,8 +163,8 @@ function log_dep(dep) {
untrack(() => snapshot(derived.v))
);

if (derived.deps) {
for (const d of derived.deps) {
if (derived.edges) {
for (const d of derived.edges) {
log_dep(d);
}
}
Expand Down Expand Up @@ -459,10 +459,10 @@ export function log_inconsistent_branches(effect) {
console.log('%cfn:', 'font-weight: bold', branch.effect.fn);
}

if (branch.effect.deps !== null) {
if (branch.effect.edges !== null) {
// eslint-disable-next-line no-console
console.groupCollapsed('%cdeps', 'font-weight: normal');
for (const dep of branch.effect.deps) {
for (const dep of branch.effect.edges) {
log_dep(dep);
}
// eslint-disable-next-line no-console
Expand Down
78 changes: 67 additions & 11 deletions packages/svelte/src/internal/client/dev/tracing.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
/** @import { Derived, Reaction, Value } from '#client' */
/** @import { BatchRecord, BatchValue, Derived, DerivedRecord, Reaction, Value } from '#client' */
import { UNINITIALIZED } from '../../../constants.js';
import { snapshot } from '../../shared/clone.js';
import { DERIVED, ASYNC, PROXY_PATH_SYMBOL, STATE_SYMBOL } from '#client/constants';
import { effect_tracking } from '../reactivity/effects.js';
import { active_reaction, untrack } from '../runtime.js';
import { batch_values, view_batch } from '../reactivity/batch.js';
import { get_view } from '../reactivity/deriveds.js';
import { active_reaction, run_of, untrack, version_of } from '../runtime.js';

/**
* @typedef {{
Expand All @@ -15,19 +17,67 @@ import { active_reaction, untrack } from '../runtime.js';
export let tracing_expressions = null;

/**
* The value of `signal`, with its version, as the active view of the world sees it (see `get`): for a
* derived, the run of it that applies — a record in the batch, or the derived itself — or an assigned value
* @param {Value} signal
* @param {TraceEntry} [entry]
* @returns {Value | DerivedRecord | BatchValue}
*/
function log_entry(signal, entry) {
const value = signal.v;
function view_of(signal) {
if (batch_values === null) return signal;
if ((signal.f & DERIVED) !== 0) return get_view(/** @type {Derived} */ (signal));
return batch_values.get(signal) ?? signal;
}

/**
* The run of `reaction` that applies in the active view of the world (see `run_of`), which for a derived
* in the view of a batch is its record in the batch if it has one (see `get_view`). While the reaction
* runs, that's its previous run
* @param {Reaction} reaction
* @returns {Reaction | BatchRecord}
*/
function run_in_view(reaction) {
if ((reaction.f & DERIVED) !== 0 && batch_values !== null) {
return view_batch?.get_record(reaction) ?? reaction;
}

return run_of(reaction);
}

/**
* Whether `signal` changed in the active view of the world since the traced reaction last ran. If its previous
* run read it, that's whether the version it saw is no longer current. Otherwise (it's a dependency of a derived
* the reaction read, or the reaction didn't read it before), whether it was written since `since`: the one place
* where versions are compared by order, which is only a heuristic — a view can undo writes, which gives values
* older versions again
* @param {Value} signal
* @param {Reaction | BatchRecord} run the previous run of the traced reaction (see `run_in_view`)
* @param {number} since the version of the reaction's previous run (`0` if there's none)
*/
function is_changed(signal, run, since) {
var i = run.deps?.indexOf(signal) ?? -1;
var seen = i === -1 ? undefined : run.dv?.[i];

if (seen !== undefined) return version_of(signal) !== seen;

return since === 0 || version_of(signal) > since;
}

/**
* @param {Value} signal
* @param {TraceEntry | undefined} entry
* @param {Reaction | BatchRecord} run the previous run of the traced reaction (see `run_in_view`)
* @param {number} since the version of the reaction's previous run (`0` if there's none)
*/
function log_entry(signal, entry, run, since) {
const view = view_of(signal);
const value = view.v;

if (value === UNINITIALIZED) {
return;
}

const type = get_type(signal);
const current_reaction = /** @type {Reaction} */ (active_reaction);
const dirty = signal.wv > current_reaction.wv || current_reaction.wv === 0;
const dirty = is_changed(signal, run, since);
const style = dirty
? 'color: CornflowerBlue; font-weight: bold'
: 'color: grey; font-weight: normal';
Expand All @@ -42,10 +92,13 @@ function log_entry(signal, entry) {
: value
);

if (type === '$derived') {
const deps = new Set(/** @type {Derived} */ (signal).deps);
if ((signal.f & DERIVED) !== 0) {
const deps = new Set(
('deps' in view ? view : run_in_view(/** @type {Derived} */ (signal))).deps
);

for (const dep of deps) {
log_entry(dep);
log_entry(dep, undefined, run, since);
}
}

Expand Down Expand Up @@ -111,10 +164,13 @@ export function trace(label, fn) {
console.group(`${prefix} %c(${time}ms)`, 'color: grey');

var entries = tracing_expressions.entries;
var reaction = /** @type {Reaction} */ (active_reaction);
var run = run_in_view(reaction);
var since = run.wv ?? reaction.wv;

untrack(() => {
for (const [signal, traces] of entries) {
log_entry(signal, traces);
log_entry(signal, traces, run, since);
}
});

Expand Down
6 changes: 3 additions & 3 deletions packages/svelte/src/internal/client/dom/blocks/boundary.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export class Boundary {
#pending_count_update_queued = false;

/** @type {Map<Effect, number>} */
#dirty_reactions = new Map();
#dirty_effects = new Map();

/**
* A source containing the number of pending async deriveds/expressions.
Expand Down Expand Up @@ -355,15 +355,15 @@ export class Boundary {

// any effects that were previously deferred should be transferred
// to the batch, which will flush in the next microtask
batch.transfer_reactions(this.#dirty_reactions);
batch.transfer_effects(this.#dirty_effects);
}

/**
* Defer an effect inside a pending boundary until the boundary resolves
* @param {Effect} effect
*/
defer_effect(effect) {
defer_effect(effect, this.#dirty_reactions);
defer_effect(effect, this.#dirty_effects);
}

/**
Expand Down
36 changes: 14 additions & 22 deletions packages/svelte/src/internal/client/dom/blocks/branches.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
resume_effect
} from '../../reactivity/effects.js';
import { HMR_ANCHOR } from '../../constants.js';
import { EFFECT_OFFSCREEN } from '#client/constants';
import { hydrate_node, hydrating } from '../hydration.js';
import { create_text, should_defer_append } from '../operations.js';
import { DEV } from 'esm-env';
Expand Down Expand Up @@ -107,26 +108,12 @@ export class BranchManager {
// ...and append the fragment
this.anchor.before(offscreen.fragment);
onscreen = offscreen.effect;
onscreen.f &= ~EFFECT_OFFSCREEN;
}
}

for (const [b, k] of this.#batches) {
this.#batches.delete(b);

if (b === batch) {
// keep values for newer batches
break;
}

const offscreen = this.#offscreen.get(k);

if (offscreen) {
// for older batches, destroy offscreen effects
// as they will never be committed
destroy_effect(offscreen.effect);
this.#offscreen.delete(k);
}
}
// other batches can still commit (in any order), so their selections remain
this.#discard(batch);

// outro/destroy all onscreen effects...
for (const [k, effect] of this.#onscreen) {
Expand Down Expand Up @@ -166,7 +153,10 @@ export class BranchManager {
* @param {Batch} batch
*/
#discard = (batch) => {
this.#batches.delete(batch);
for (const b of this.#batches.keys()) {
// (batches that were merged into another one don't select anything anymore)
if (b === batch || !b.linked) this.#batches.delete(b);
}

const keys = Array.from(this.#batches.values());

Expand Down Expand Up @@ -194,10 +184,12 @@ export class BranchManager {

fragment.append(target);

this.#offscreen.set(key, {
effect: branch(() => fn(target)),
fragment
});
var effect = branch(() => fn(target));

// until it is committed, the branch only exists for the batches that select it
effect.f |= EFFECT_OFFSCREEN;

this.#offscreen.set(key, { effect, fragment });
} else {
this.#onscreen.set(
key,
Expand Down
18 changes: 18 additions & 0 deletions packages/svelte/src/internal/client/dom/blocks/each.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,13 @@ function destroy_effects(state, to_destroy, remove_dom = true) {

const fragment = document.createDocumentFragment();
move_effect(e, fragment);

// `reconcile` expects offscreen items to come after all onscreen items
if (e !== state.effect.last) {
link(state, e.prev, e.next);
link(state, state.effect.last, e);
link(state, e, null);
}
Comment thread
Rich-Harris marked this conversation as resolved.
Resolved
} else {
destroy_effect(to_destroy[i], remove_dom);
}
Expand Down Expand Up @@ -336,7 +343,9 @@ export function each(node, flags, get_collection, get_key, render_fn, fallback_f
);

if (!first_run) {
// until it is committed, the item only exists for the batches that select it
item.e.f |= EFFECT_OFFSCREEN;
if (defer) batch.unskip_effect(item.e);
}

items.set(key, item);
Expand All @@ -350,10 +359,19 @@ export function each(node, flags, get_collection, get_key, render_fn, fallback_f
fallback = branch(() => fallback_fn(anchor));
} else {
fallback = branch(() => fallback_fn((offscreen_anchor ??= create_text())));
// until it is committed, the fallback only exists for the batches that select it
fallback.f |= EFFECT_OFFSCREEN;
}
}

if (defer && fallback !== null && (fallback.f & EFFECT_OFFSCREEN) !== 0) {
if (length === 0) {
batch.unskip_effect(fallback);
} else {
batch.skip_effect(fallback);
}
}

if (length > keys.size) {
if (DEV) {
validate_each_keys(array, get_key);
Expand Down
16 changes: 4 additions & 12 deletions packages/svelte/src/internal/client/error-handling.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
/** @import { Derived, Effect } from '#client' */
/** @import { Effect } from '#client' */
/** @import { Boundary } from './dom/blocks/boundary.js' */
import { DEV } from 'esm-env';
import { FILENAME, HYDRATION_ERROR } from '../../constants.js';
import { is_firefox } from './dom/operations.js';
import {
ERROR_VALUE,
BOUNDARY_EFFECT,
REACTION_RAN,
EFFECT,
DESTROYED,
DESTROYING
} from './constants.js';
import { BOUNDARY_EFFECT, REACTION_RAN, EFFECT, DESTROYED, DESTROYING } from './constants.js';
import { define_property, get_descriptor } from '../shared/utils.js';
import { active_effect, active_reaction } from './runtime.js';
import { active_effect } from './runtime.js';

const adjustments = new WeakMap();

Expand All @@ -22,9 +15,8 @@ const adjustments = new WeakMap();
export function handle_error(error) {
var effect = active_effect;

// for unowned deriveds, don't throw until we read the value
// for unowned deriveds, don't throw until we read the value (the error becomes their value)
if (effect === null) {
/** @type {Derived} */ (active_reaction).f |= ERROR_VALUE;
return error;
}

Expand Down
1 change: 0 additions & 1 deletion packages/svelte/src/internal/client/reactivity/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ export function capture() {
// TODO we only need optional chaining here because `{#await ...}` blocks
// are anomalous. Once we retire them we can get rid of it
previous_batch?.activate();
previous_batch?.apply();
}

if (DEV) {
Expand Down
Loading
Loading