Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@jimsimon
Copy link
Collaborator

This PR adds a cache for previously computed digest results. This should improve throughput when rendering the same TemplateResult multiple times.

@jimsimon jimsimon requested a review from kevinpschaaf as a code owner August 29, 2025 21:43
@changeset-bot
Copy link

changeset-bot bot commented Aug 29, 2025

🦋 Changeset detected

Latest commit: 6a1a879

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

This PR includes changesets to release 1 package
Name Type
@lit-labs/ssr-client 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

const digestSize = 2;

// Digest cache
const digestCache = new WeakMap<TemplateStringsArray, string>();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's make sure that we're not exacerbating the static template memory leak with this...

I think we're not because once the static template cache frees a template, the forged TemplateStringsArray will be able to be freed and this cache will release the entry.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I believe you're correct based on my understanding of how WeakMaps work in JS

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Once an object used as a key has been collected, its corresponding values in any WeakMap become candidates for garbage collection as well — as long as they aren't strongly referred to elsewhere.
-MDN

So as long as the template is only associated with WeakMaps (or I'm assuming other "weak" data structures), we should be good.

// for `Buffer` from Node's built-in `buffer` module in our Rollup config (see
// note at the top of this file), and use that.
return NODE_MODE ? Buffer.from(str, 'binary').toString('base64') : btoa(str);
digest = NODE_MODE
Copy link
Collaborator

Choose a reason for hiding this comment

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

btw, we should start using btoa in Node soon...

Copy link
Collaborator

@justinfagnani justinfagnani left a comment

Choose a reason for hiding this comment

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

awesome!

@justinfagnani justinfagnani merged commit 12e3d94 into lit:main Aug 30, 2025
7 checks passed
@lit-robot lit-robot mentioned this pull request Dec 18, 2025
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