Skip to content

fix: reconnected deep derived signals to graph #12350

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

Merged
merged 12 commits into from
Jul 8, 2024
Merged

Conversation

trueadm
Copy link
Contributor

@trueadm trueadm commented Jul 8, 2024

Fixes #12330.

Copy link

changeset-bot bot commented Jul 8, 2024

🦋 Changeset detected

Latest commit: c665ddf

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

@Rich-Harris
Copy link
Member

Huh — should be happening here. It definitely doesn't need to happen in both places

if ((flags & DISCONNECTED) !== 0) {
// reconnect to the graph
deps = derived.deps;
if (deps !== null) {
for (var i = 0; i < deps.length; i++) {
var dep = deps[i];
var reactions = dep.reactions;
if (reactions === null) {
dep.reactions = [derived];
} else if (!reactions.includes(derived)) {
reactions.push(derived);
}
}
}
derived.f ^= DISCONNECTED;
}

@trueadm
Copy link
Contributor Author

trueadm commented Jul 8, 2024

@Rich-Harris I know it's done there, but that logic never gets applied to this signal because if there are multiple disconnected signals, then only the outer signal gets called via get (which the logic you referenced above happens). There isn't a get on the inner disconnected signal, so that inner signal is not connected to the graph again.

@Rich-Harris
Copy link
Member

Why isn't the outer derived getting its dependencies? Aren't they all marked MAYBE_DIRTY upon disconnection?

@trueadm
Copy link
Contributor Author

trueadm commented Jul 8, 2024

@Rich-Harris I added a test case.

@trueadm trueadm marked this pull request as ready for review July 8, 2024 18:55
@trueadm trueadm changed the title fix: fixed derived disconnected issues fix: reconnected deep derived signals to graph Jul 8, 2024
Copy link
Member

@Rich-Harris Rich-Harris left a comment

Choose a reason for hiding this comment

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

weirds me out a bit that we need to call this in two places — it feels like there should be a single choke point where this work can happen, and it feels weird to have this sort of side-effect in check_dirtiness — but I haven't wrapped my head around what that would look like. made a few suggestions

@trueadm
Copy link
Contributor Author

trueadm commented Jul 8, 2024

I haven't benchmarked this, but it kind of feels wasteful to do another O(n() lookup in each case of check_dirtiness. I wonder if we can avoid that like in the original commit.

@Rich-Harris
Copy link
Member

It's not — we early-return as soon as we find a dirty dependency, so we have a choice:

  1. do the separate O(n) thing
  2. reconnect deriveds inside the loop in check_dirtiness, but that means we can no longer return early

Since reconnecting a disconnected derived is a fairly unusual case, number 2 turns out to be much slower — I validated this via the benchmarks in #12272.

Anyway, I figured out how to make it unnecessary to call reconnect_derived in two places — we just need to do it before the loop in check_dirtiness. Change incoming

@Rich-Harris Rich-Harris merged commit 202f1dd into main Jul 8, 2024
9 checks passed
@Rich-Harris Rich-Harris deleted the runtime-derived branch July 8, 2024 22:50
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.

Svelte 5: next.173 broke <something>
2 participants