Skip to content

feat: add partial evaluation #15494

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 16 commits into from
Apr 14, 2025
Merged

feat: add partial evaluation #15494

merged 16 commits into from
Apr 14, 2025

Conversation

Rich-Harris
Copy link
Member

@Rich-Harris Rich-Harris commented Mar 11, 2025

Noticed while looking into something unrelated that this input...

<script>
  let choice = $state('one');
</script>

<input type="radio" bind:group={choice} value="one" />
<input type="radio" bind:group={choice} value="two" />
<input type="radio" bind:group={choice} value="three" />

<p>{choice}</p>

...yields this output:

input.value = null == (input.__value = 'one') ? '' : 'one';

At minimum, it should be this instead:

input.value = input.__value = 'one' ?? '';

But we can go much further. Something I've been meaning to get round to for a while is to add some simple partial evaluation, which would make it possible to avoid the ?? '' stuff in a lot more cases. @Ocean-OS made some progress on this front in #15374 but I'd like it to be able to accommodate bindings as well (in other words, knowing that message is always a string and count is always a number). I think it makes sense to implement this as an evaluate method on the Scope class.

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

Sorry, something went wrong.

Copy link

changeset-bot bot commented Mar 11, 2025

🦋 Changeset detected

Latest commit: 9294e66

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

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

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

Copy link
Contributor

Playground

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

SSR
@Rich-Harris Rich-Harris marked this pull request as ready for review March 14, 2025 00:34
@Rich-Harris
Copy link
Member Author

Rich-Harris commented Mar 14, 2025

Marking as ready for review, even though there's room for future improvement:

  • marking some bindings as always defined (e.g. each block indices), so they don't need ?? ''
  • tracking reassignments and bind: directives (e.g. if count = 0 and count++ then we know that count is always a number
  • skipping $.escape calls where unnecessary (e.g. because value is a number)
  • baking more stuff into $.template calls
  • maybe simplifying gather_possible_values
  • hoisting stuff
  • (one day) evaluating imports and props

@Ocean-OS
Copy link
Contributor

Is this PR meant to be combined with #15374, or just replace it?

@svelte-docs-bot
Copy link

@Rich-Harris
Copy link
Member Author

I think it's probably a replacement given their overlap. Which isn't a commentary on your PR! Convergent evolution; I started working on this before I understood the degree to which we were addressing a similar problem.

The reason I favour this PR over #15374 is that it feels more future-proof to have an Evaluation class with different properties than a single value which could be DYNAMIC. For example it's useful to know that an each index binding is a) defined and b) always a number, because then we can ditch the ?? '' in the client output and the $.escape(...) in the server output. I suspect it will also be useful to know that a ? 'b' : 'c' will always be either "b" or "c" when doing CSS pruning, for example.

Which isn't to say this analysis is particularly sophisticated — it definitely isn't, compared to what a good general purpose optimiser will give you. But it gives us something to build from. (Maybe one day we'll be able to incorporate type information and so on.)

Copy link
Member

@dummdidumm dummdidumm left a comment

Choose a reason for hiding this comment

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

Nice work 👍 Couple of minor comments but looks good overall

Rich-Harris and others added 3 commits April 14, 2025 14:00

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
Co-authored-by: Simon H <[email protected]>
@Rich-Harris Rich-Harris merged commit 90563e9 into main Apr 14, 2025
13 checks passed
@Rich-Harris Rich-Harris deleted the scope-evaluate branch April 14, 2025 18:38
@github-actions github-actions bot mentioned this pull request Apr 14, 2025
@Ocean-OS Ocean-OS mentioned this pull request Apr 17, 2025
9 tasks
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.

None yet

3 participants