fix: optimize consecutive if blocks#18233
Conversation
🦋 Changeset detectedLatest commit: 9817754 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
|
Local validation on the current head (
The build/check steps did not leave generated-file drift; the branch is clean locally after validation. |
| !expression.has_await && | ||
| !expression.has_call && | ||
| !expression.has_assignment && | ||
| !expression.has_blockers() |
There was a problem hiding this comment.
We bail in quite a few cases here - I do wonder if this is still worth the additional code
|
I'm inclined to agree with @dummdidumm here — this does microscopically improve performance in the cases where this optimisation can be applied, but they're few and far between, and the original test case should just be rewritten to be a single block which would make the source code more readable. |
|
Thanks for taking a look. That makes sense to me; the edge-case win is not worth carrying extra compiler complexity here. I will close this out. |
Description
Fixes #10049.
This adds a conservative compiler-analysis optimization for consecutive nested
{#if}blocks. When an{#if}contains only another simple{#if}and both conditions are sync, call-free, assignment-free, blocker-free, and have noelsebranch, the compiler merges them into oneouter && innercondition and reuses the inner body.That avoids generating an extra client
$.ifblock and nested serveriffor common template shapes like:{#if iconData} {#if iconData.paths} ... {/if} {/if}The optimization is disabled when
preserveWhitespaceis enabled, because whitespace-only text around nested blocks can be observable in that mode.Validation
pnpm --filter svelte buildFILTER=/nested-if-/ pnpm test snapshotpnpm test snapshotpnpm --filter svelte checkpnpm lintgit diff --check