Skip to content

fix: treat batches as a linked list#18205

Merged
Rich-Harris merged 32 commits into
mainfrom
linked-batches
May 13, 2026
Merged

fix: treat batches as a linked list#18205
Rich-Harris merged 32 commits into
mainfrom
linked-batches

Conversation

@Rich-Harris

@Rich-Harris Rich-Harris commented May 12, 2026

Copy link
Copy Markdown
Member

This is another attempt to address some of the tricky edge cases that arise when async batches resolve out of order. The idea is this:

Essentially, when a batch resolves:

  1. we find the latest batch it shares changes with
  2. if none exists (either because this is the earliest batch, or because it is independent of any earlier batches):
    • we commit it: effects are flushed, oncommit callbacks are run
    • we restart any async work in later batches that depends on both the committed values and the later batch's changes
  3. otherwise, we don't commit the batch. instead:
    • we merge the changes from the later batch onto the earlier batch. in some cases this may mean restarting async work on the earlier batch, but we avoid doing so unnecessarily.
    • we then #process() the earlier batch. if it resolves, goto 1

This feels like it ought to work. There are still two failing tests, which I'm currently looking into.

Notable changes:

  • Instead of having a batches set, we have a linked list. When a batch resolves, this makes it easy to find the batch that it should be merged into
  • The #is_deferred logic now takes account of skipped effects — there's no need to wait for a promise inside a falsy if block (this accounts for the change in the async-inner-after-outer test)
  • We no longer care about #blockers

I would like to believe that this approach will allow us to simplify and delete some code, for example the rebase logic, though that remains to be seen. It also feels like some version of #18035 would be helpful.

Before submitting the PR, please make sure you do the following

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • Prefix your PR title with feat:, fix:, chore:, or docs:.
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.
  • If this PR changes code within packages/svelte/src, add a changeset (npx changeset).

Tests and linting

  • Run the tests with pnpm test and lint the project with pnpm lint

@changeset-bot

changeset-bot Bot commented May 12, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 14e6437

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
svelte Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@svelte-docs-bot

Copy link
Copy Markdown

@github-actions

Copy link
Copy Markdown
Contributor

Playground

pnpm add https://pkg.pr.new/svelte@18205

Comment thread packages/svelte/src/internal/client/dom/blocks/svelte-head.js

@dummdidumm dummdidumm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This looks super promising! I see a lot of parallels to some of the earlier experiments we tried which gives me confidence. Left a few comments + need to spend more time with it, but looks good so far

Comment thread packages/svelte/src/internal/client/dom/blocks/svelte-head.js
Comment thread packages/svelte/src/internal/client/reactivity/batch.js Outdated
Comment thread packages/svelte/src/internal/client/reactivity/batch.js Outdated
Comment thread packages/svelte/src/internal/client/reactivity/batch.js Outdated
Comment thread packages/svelte/src/internal/client/reactivity/batch.js Outdated
Comment thread packages/svelte/src/internal/client/reactivity/batch.js Outdated
Comment thread packages/svelte/src/internal/client/reactivity/batch.js Outdated
Comment thread packages/svelte/src/internal/client/reactivity/deriveds.js
Rich-Harris and others added 3 commits May 12, 2026 17:38
Co-authored-by: Simon H <5968653+dummdidumm@users.noreply.github.com>
Co-authored-by: Simon H <5968653+dummdidumm@users.noreply.github.com>
Comment thread packages/svelte/src/internal/client/reactivity/batch.js
@vercel

vercel Bot commented May 13, 2026

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
svelte-previews Error Error May 13, 2026 2:57pm

Request Review

@dummdidumm dummdidumm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

great work 👍

@Rich-Harris Rich-Harris merged commit 5122936 into main May 13, 2026
20 of 21 checks passed
@Rich-Harris Rich-Harris deleted the linked-batches branch May 13, 2026 20:06
@github-actions github-actions Bot mentioned this pull request May 13, 2026
Rich-Harris pushed a commit that referenced this pull request May 14, 2026
This PR was opened by the [Changesets
release](https://github.com/changesets/action) GitHub action. When
you're ready to do a release, you can merge this and the packages will
be published to npm automatically. If you're not ready to do a release
yet, that's fine, whenever you add more changesets to main, this PR will
be updated.


# Releases
## svelte@5.55.6

### Patch Changes

- fix: leave stale promises to wait for a later resolution, instead of
rejecting ([#18180](#18180))

- fix: keep dependencies of `$state.eager/pending`
([#18218](#18218))

- fix: reapply context after transforming error during SSR
([#18099](#18099))

- fix: don't rebase just-created batches
([#18117](#18117))

- chore: allow `null` for `pending` in typings
([#18201](#18201))

- fix: flush eager effects in production
([#18107](#18107))

- fix: rethrow error of failed iterable after calling `return()`
([#18169](#18169))

- fix: account for proxified instance when updating `bind:this`
([#18147](#18147))

- fix: ensure scheduled batch is flushed if not obsolete
([#18131](#18131))

- fix: resolve stale deriveds with latest value
([#18167](#18167))

- chore: remove unnecessary `increment_pending` calls
([#18183](#18183))

- fix: correctly compile component member expressions for SSR
([#18192](#18192))

- fix: reset `source.updated` stack traces after `flush`
([#18196](#18196))

- fix: replacing async 'blocking' strategy with 'merging'
([#18205](#18205))

- fix: allow `@debug` tags to reference awaited variables
([#18138](#18138))

- fix: re-run fallback props if dependencies update
([#18146](#18146))

- fix: abort running obsolete async branches
([#18118](#18118))

- fix: ignore comments when reading CSS values
([#18153](#18153))

- fix: wrap `Promise.all` in `save` during SSR
([#18178](#18178))

- fix: ignore false-positive errors of `$inspect` dependencies
([#18106](#18106))

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Navigating between pages of a layout can re-evaluate $derived with wrong values before navigating

2 participants