svelte icon indicating copy to clipboard operation
svelte copied to clipboard

[feat] Implemented a small runtime optimization for SSR

Open coryvirok opened this issue 2 years ago • 6 comments

Prior to this change, the compiler would generate a template literal that had many purely static string variables nested within it. This change collapses these static strings into the surrounding template literal which should result in (minor) size and performance improvements for the SSR generated code.

Example output before change

// dist/server/entries/pages/index.svelte.js

const FeaturedContent = create_ssr_component(($$result, $$props, $$bindings, slots) => {
  let { imgSrc } = $$props;
  let { heading } = $$props;
  if ($$props.imgSrc === void 0 && $$bindings.imgSrc && imgSrc !== void 0)
    $$bindings.imgSrc(imgSrc);
  if ($$props.heading === void 0 && $$bindings.heading && heading !== void 0)
    $$bindings.heading(heading);
  return `<div><img${add_attribute("src", imgSrc, 0)} class="${"rounded-md"}">
  <h3 class="${"py-6 text-3xl font-bold tracking-tight text-primary sm:text-2xl"}">${escape(heading)}</h3>
  <p class="${"prose font-light text-gray-600"}">${slots.default ? slots.default({}) : ``}</p></div>`;
});

// ...

After this change (note the missing ${} where string literals were added to the surrounding static string.)


const FeaturedContent = create_ssr_component(($$result, $$props, $$bindings, slots) => {
  let { imgSrc } = $$props;
  let { heading } = $$props;
  if ($$props.imgSrc === void 0 && $$bindings.imgSrc && imgSrc !== void 0)
    $$bindings.imgSrc(imgSrc);
  if ($$props.heading === void 0 && $$bindings.heading && heading !== void 0)
    $$bindings.heading(heading);
  return `<div><img${add_attribute("src", imgSrc, 0)} class="rounded-md">
  <h3 class="py-6 text-3xl font-bold tracking-tight text-primary sm:text-2xl">${escape(heading)}</h3>
  <p class="prose font-light text-gray-600">${slots.default ? slots.default({}) : ``}</p></div>`;
});

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
  • [x] Prefix your PR title with [feat], [fix], [chore], or [docs].
  • [x] This message body should clearly illustrate what problems it solves.
  • [x] Ideally, include a test that fails without this PR but passes with it.

Tests

  • [x] Run the tests with npm test and lint the project with npm run lint

coryvirok avatar May 17 '22 09:05 coryvirok

It might be nice to add a test for the new method

benmccann avatar May 17 '22 15:05 benmccann

Thanks for the review, @dummdidumm, @Conduitry and @benmccann. I updated the code style to use snake_case for internal vars. I couldn't find any unit tests, so I added a test to the test/js/samples dir to check the generated code vs actual. I made sure to test a few different scenarios where the TemplateLiteral should and should not collapse.

Regarding the client-side optimization, I'm not familiar with that code-path. Can you point me in the right direction?

coryvirok avatar May 17 '22 18:05 coryvirok

@Conduitry anything blocking this from being merged?

coryvirok avatar Jul 11 '22 20:07 coryvirok

@benmccann anything else needed before merging? (I'm just trying to tidy up loose PR ends :)

coryvirok avatar Jul 14 '22 03:07 coryvirok

Looks good to me, but you will need someone more familiar with the code like Li Hau or Conduitry to sign-off on it

benmccann avatar Jul 14 '22 15:07 benmccann

Looks good to me, but you will need someone more familiar with the code like Li Hau or Conduitry to sign-off on it

Gotcha, thanks.

coryvirok avatar Jul 14 '22 15:07 coryvirok

FYI, just ran this through a microbenchmark and the new approach that this PR implements should be over 87% faster than the current.

The benchmark is testing the approach, not the code itself: https://jsben.ch/GL5Mr

image

coryvirok avatar Nov 09 '22 00:11 coryvirok

@dummdidumm is attempting to deploy a commit to the Svelte Team on Vercel.

A member of the Team first needs to authorize it.

vercel[bot] avatar Feb 27 '23 13:02 vercel[bot]

Thank you!

dummdidumm avatar Feb 27 '23 15:02 dummdidumm

Released in 3.56.0 - thanks!

Conduitry avatar Mar 10 '23 00:03 Conduitry

@dummdidumm thx for simplifying the code!

coryvirok avatar Mar 10 '23 04:03 coryvirok