-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
fix: memoize repeated state reads in guard expressions #16881
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
Open
LeeWxx
wants to merge
5
commits into
sveltejs:main
Choose a base branch
from
LeeWxx:fix/guard-derived-short-circuit
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+561
−12
Open
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0bca1c7
fix: memoize repeated state reads in guard expressions
LeeWxx 5053ce2
test: add guard-derived short-circuit regression
LeeWxx 30fb52e
chore: changeset
LeeWxx 5e4fbea
fix: share guarded values across control blocks
LeeWxx a758384
test: add guard-derived regression samples
LeeWxx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'svelte': patch | ||
--- | ||
|
||
fix: memoize repeated state reads in guard expressions |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
packages/svelte/tests/runtime-runes/samples/guard-derived-short-circuit/_config.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { test } from '../../test'; | ||
import { flushSync } from 'svelte'; | ||
|
||
export default test({ | ||
mode: ['client'], | ||
async test({ target, assert }) { | ||
const button = target.querySelector('button'); | ||
|
||
flushSync(() => button?.click()); | ||
|
||
assert.equal(target.textContent?.trim(), 'Trigger'); | ||
} | ||
}); |
20 changes: 20 additions & 0 deletions
20
packages/svelte/tests/runtime-runes/samples/guard-derived-short-circuit/main.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<script> | ||
let virtualItems = $state([{ index: 0 }, { index: 1 }, { index: 2 }]); | ||
let centerRows = $state([{ depth: 2 }, { depth: 1 }, { depth: 1 }]); | ||
|
||
let someChange = $state(false); | ||
$effect(() => { | ||
if (someChange) centerRows = []; | ||
}); | ||
</script> | ||
|
||
{#each virtualItems as row (row.index)} | ||
{@const centerRow = centerRows[row.index]} | ||
{#if centerRow != undefined && centerRow.depth != undefined && typeof centerRow === "object" && "depth" in centerRow && typeof centerRow.depth === "number"} | ||
{#if centerRow.depth != undefined && centerRow.depth > 0} | ||
Hello World<br /> | ||
{/if} | ||
{/if} | ||
{/each} | ||
|
||
<button onclick={() => (someChange = true)}>Trigger</button> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have a suspicion that this is what "fixes" the problem by turning
into
which is not really equal.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@7nik Thanks for double-checking! At first glance it might look a bit different, but I think Svelte still seems to emit the leading guard
From what I can tell, the optional chaining only comes into play after we already know that
centerRow_value
isn’t null or undefined. In that case,centerRow_value?.depth
would effectively behave the same ascenterRow_value.depth
. The ?. just seems to act as a safety net, in case the value somehow changes between reads while reusing the cached value. So I believe the semantics of the guard essentially remain the same.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Look at this variant - it fails on this PR as well. The problem is that the nested
if
/ template effect / etc runs when it shouldn't.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@7nik Oops, you’re right
it looks like this new variant still slips through what I’ve tried so far. From what I can tell, the inner template effect is still calling
$.get(centerRow)
even after the guard has already short-circuited, which makes the block run when it really shouldn’t.I see two possible ways we could approach this. One would be to hoist the guard result so that the entire block (effects and nested branches included) reuses the same snapshot. The other would be to change how the template effect checks for guard failure before it runs.
Do you think one of these directions would fit better?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The outer
if
should execute first and destroy the branch, thus no execution ofcenterRow.depth
whencenterRow
isundefined
. But I'm not so familiar with the reactivity internals (especially async ones) to say why effects are executed in another order and how to fix it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@7nik Thanks for catching that earlier variant! I hoisted the guard value so the entire block (including template effects and nested branches) reuses the same snapshot.
If you’re aware of other guard scenarios we should test, I’d really appreciate the pointers.