Skip to content
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

feat: static template expression evaluation #15374

Open
wants to merge 14 commits into
base: main
Choose a base branch
from

Conversation

Ocean-OS
Copy link
Contributor

@Ocean-OS Ocean-OS commented Feb 23, 2025

This optimizes the compiled output by using static analysis to evaluate some nonreactive template expressions.
For example, this component

<script>
let a = 1;
let b = 2;
</script>
<p>{a} + {b} = {a + b}</p>

would have its template expression compiled to:

p.textContent = `1 + 2 = 3`;

However, this:

<script>
let a = 1;
let b = 2;
a++;
</script>
<p>{a} + {b} = {a + b}</p>

would get compiled to:

$.template_effect(() => $.set_text(text, `${$.get(a) ?? ''} + 2 = ${$.get(a) + b ?? ''}`));

I think the end goal (that would be achieved either here or another PR) would be inlining this in the template, so that the top example would end up having its template look like this:

let root = $.template(`<p>1 + 2 = 3</p>`);

...which removes the (albeit small) overhead of locating the <p> element and creating an effect to update it.

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

Copy link

changeset-bot bot commented Feb 23, 2025

🦋 Changeset detected

Latest commit: 9907cbc

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

Copy link
Contributor

Playground

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

@Ocean-OS
Copy link
Contributor Author

Ocean-OS commented Feb 23, 2025

ok there appear to be some tests that need to be changed as well...

@Ocean-OS Ocean-OS changed the title feat: static template expression evaluation fix: static template expression evaluation Feb 23, 2025
@Ocean-OS Ocean-OS changed the title fix: static template expression evaluation feat: static template expression evaluation Feb 24, 2025
@Ocean-OS
Copy link
Contributor Author

once tests are implemented for the client, I will work on adding this to the server version.

@Ocean-OS Ocean-OS marked this pull request as ready for review February 24, 2025 04:48
@paoloricciuti
Copy link
Member

We tried inlining constant values before and it ended up in such a hassle that we reverted it...there are a thousands things to keep track of for every non trivial example. If you succeed big props to you, just wanted to warn you 😄

@Ocean-OS
Copy link
Contributor Author

Ocean-OS commented Feb 25, 2025

We tried inlining constant values before and it ended up in such a hassle that we reverted it...there are a thousands things to keep track of for every non trivial example. If you succeed big props to you, just wanted to warn you 😄

If I'm correct in assuming you're referring to #10015, this should work, since that appears to hoist constant variables for optimization, so for example:

<script>
let name = 'world';
</script>
<h1>Hello, {name}!</h1>

would become something like this:

let name = 'world';
let root = $.template(`<h1>Hello, ${name}!</h1>`);
function Component() {
  //...
}

This instead takes a more direct approach, by replacing constant primitive variables with their value, like so:

//...
function Component() {
  //...
  let name = 'world';
  h1.textContent = `Hello, world!`;
  //...
}

Additionally, this PR is not only limited to variables; most expressions consisting of literal primitives (and/or those variables) will be evaluated as well.
So, if only the variables part isn't working, then those ~10 lines of code could just be fixed or removed, and there'd still be some (albeit small) optimizations happening without the variables part.

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.

2 participants