Skip to content

[labs/ssr] Experiment with node:worker_threads Worker - #5153

Draft
kyubisation wants to merge 4 commits into
lit:mainfrom
kyubisation:feat-worker-experiment
Draft

kyubisation wants to merge 4 commits into
lit:mainfrom
kyubisation:feat-worker-experiment

Conversation

@kyubisation

Copy link
Copy Markdown
Contributor

This is a draft pull request to experiment with node:worker_threads Worker.

Findings:

  • If a lot of workers are spawned, old workers might be killed: https://engineering.zalando.com/posts/2024/07/nodejs-tale-worker-threads.html
    Depending on use case it might make sense to use a single render Worker instance or multiple. Might require additional performance testing.
  • It is not possible to transfer functions into workers, but objects work. Note: Some data might be lost, like the raw property of a TemplateStringsArray, as just the array is transferred.
  • The current state of this PR would allow using a ready-made solution or just parts of it (e.g. use the provided Worker or bring your own, use the registerRenderRequestHandler or implement your own logic for handling messages). See render-worker_test.ts.

Questions:

  • Should lit provide utility for using node:worker_threads Workers?

@changeset-bot

changeset-bot Bot commented Oct 26, 2025 •

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: f4436b7

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@kyubisation
kyubisation force-pushed the feat-worker-experiment branch from 940cc66 to ec6e8a1 Compare November 20, 2025 15:38
@kyubisation
kyubisation force-pushed the feat-worker-experiment branch from ec6e8a1 to f4436b7 Compare November 20, 2025 17:52
@justinfagnani

Copy link
Copy Markdown
Collaborator

@kyubisation awesome stuff!

I think that we can make the shared cache work by making an interface for a sync or async cache:

interface LocalTemplateCache {
  kind: 'local';
  get(strings: TemplateStringsArray): Array<Op>;
  set(strings: TemplateStringsArray, ops: Array<Op>): void;
}

interface RemoteTemplateCache {
  kind: 'remote';
  get(strings: TemplateStringsArray): Promise<Array<Op>>;
  set(strings: TemplateStringsArray, ops: Array<Op>): Promise<void>;
}

type TemplateCache = LocalTemplateCache | RemoteTemplateCache;

Then we have to thread the sync-or-async nature through getTemplateOpcodes() and renderTemplateResult(). When using a RemoteTemplateCache getTemplateOpcodes() would have to return a function, pass that to renderTemplateResult() which returns a function into it it's ThunkedResult... something like that.

A tricky part is CustomElementOpenOp, which has a reference to an element constructor. I think a RemoteTemplateCache will want to just store the tag names on the remote end, then map to a constructor in the worker. So a RemoteTemplateCache needs two ends to work.

This branch has not been deployed

No deployments
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