-
Notifications
You must be signed in to change notification settings - Fork 124
feat(template-webpack-plugin): parallelize TASM encode in a shared worker pool #2634
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
4898a0d
feat(template-webpack-plugin): parallelize TASM encode in a shared wo…
upupming ee3116c
fixup: simplify worker path resolution to `require.resolve('../lib/wo…
upupming e86d182
fixup: regenerate api-extractor report for new `encodePool` static
upupming d3dfcfe
fixup: use full `availableParallelism()` and bump engines to support it
upupming File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@lynx-js/template-webpack-plugin": patch | ||
| --- | ||
|
|
||
| Run TASM template encoding in a shared `tinypool` worker pool so multi-entry builds encode in parallel and watch-mode rebuilds reuse warm workers. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
packages/webpack/template-webpack-plugin/src/worker/encode.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| // Copyright 2026 The Lynx Authors. All rights reserved. | ||
| // Licensed under the Apache License Version 2.0 that can be found in the | ||
| // LICENSE file in the root directory of this source tree. | ||
| import type { EncodeResult } from '@lynx-js/tasm'; | ||
|
|
||
| export interface EncodeWorkerOptions { | ||
| encodeBinary?: string | undefined; | ||
| encodeOptions: unknown; | ||
| tasmPkg?: string; | ||
| } | ||
|
|
||
| export default async function encode( | ||
| { | ||
| encodeBinary = undefined, | ||
| encodeOptions, | ||
| tasmPkg = '@lynx-js/tasm', | ||
| }: EncodeWorkerOptions, | ||
| ): Promise<EncodeResult> { | ||
| const { getEncodeMode } = | ||
| (await import(tasmPkg)) as typeof import('@lynx-js/tasm'); | ||
| // Napi will be used if supported | ||
| const encode = getEncodeMode(encodeBinary) as ( | ||
| options: unknown, | ||
| ) => Promise<EncodeResult>; | ||
| return encode(encodeOptions); | ||
| } |
96 changes: 96 additions & 0 deletions
96
packages/webpack/template-webpack-plugin/test/encode-worker-pool.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| // Copyright 2026 The Lynx Authors. All rights reserved. | ||
| // Licensed under the Apache License Version 2.0 that can be found in the | ||
| // LICENSE file in the root directory of this source tree. | ||
| import { dirname } from 'node:path'; | ||
|
|
||
| import { describe, expect, test } from '@rstest/core'; | ||
| import webpack from 'webpack'; | ||
|
|
||
| import { LynxEncodePlugin, LynxTemplatePlugin } from '../src/index.js'; | ||
|
|
||
| const context = dirname(new URL(import.meta.url).pathname); | ||
|
|
||
| function runWebpack(config: webpack.Configuration): Promise<webpack.Stats> { | ||
| const compiler = webpack(config); | ||
| return new Promise((resolve, reject) => { | ||
| compiler.run((err, stats) => { | ||
| if (err) return reject(err); | ||
| if (!stats) return reject(new Error('webpack returned empty stats')); | ||
| resolve(stats); | ||
| compiler.close(() => void 0); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| describe('LynxEncodePlugin shared worker pool', () => { | ||
| // The static `LynxEncodePlugin.encodePool` is a process-wide singleton. | ||
| // These tests rely on rstest running each file in its own worker process, | ||
| // so the pool starts fresh for this file and the assertions below see | ||
| // monotonic state. | ||
|
|
||
| test('runs multi-entry encodes in parallel via the shared pool', async () => { | ||
| const completedBefore = LynxEncodePlugin.encodePool.completed; | ||
|
|
||
| const stats = await runWebpack({ | ||
| context, | ||
| mode: 'development', | ||
| devtool: false, | ||
| output: { iife: false, filename: '[name].js' }, | ||
| entry: { | ||
| a: './fixtures/basic.tsx', | ||
| b: './fixtures/basic.tsx', | ||
| }, | ||
| plugins: [ | ||
| new LynxTemplatePlugin(), | ||
| new LynxEncodePlugin(), | ||
| ], | ||
| }); | ||
|
|
||
| expect(stats.compilation.errors).toEqual([]); | ||
|
|
||
| // Two entries → two encode tasks went through the pool. | ||
| expect(LynxEncodePlugin.encodePool.completed - completedBefore).toBe(2); | ||
|
|
||
| // Pool grew to (or already had) at least two threads to run them in | ||
| // parallel rather than serializing on a single worker. | ||
| expect(LynxEncodePlugin.encodePool.threads.length).toBeGreaterThanOrEqual( | ||
| 2, | ||
| ); | ||
|
|
||
| const { assets } = stats.toJson({ all: false, assets: true }); | ||
| expect(assets?.find(i => i.name === 'a.js')).not.toBeUndefined(); | ||
| expect(assets?.find(i => i.name === 'b.js')).not.toBeUndefined(); | ||
| }); | ||
|
|
||
| test('subsequent compile reuses warm workers (no respawn)', async () => { | ||
| const warmIds = new Set( | ||
| LynxEncodePlugin.encodePool.threads.map(t => t.threadId), | ||
| ); | ||
| expect(warmIds.size).toBeGreaterThan(0); | ||
|
|
||
| const completedBefore = LynxEncodePlugin.encodePool.completed; | ||
|
|
||
| // Simulate a watch-mode rebuild: a fresh compiler instance against the | ||
| // *same* process-wide pool. | ||
| await runWebpack({ | ||
| context, | ||
| mode: 'development', | ||
| devtool: false, | ||
| output: { iife: false, filename: '[name].js' }, | ||
| entry: { rebuild: './fixtures/basic.tsx' }, | ||
| plugins: [ | ||
| new LynxTemplatePlugin(), | ||
| new LynxEncodePlugin(), | ||
| ], | ||
| }); | ||
|
|
||
| // The rebuild ran a task through the pool… | ||
| expect(LynxEncodePlugin.encodePool.completed - completedBefore).toBe(1); | ||
|
|
||
| // …but every thread currently in the pool was already warm from the | ||
| // previous compile — none were newly spawned for the rebuild. | ||
| for (const { threadId } of LynxEncodePlugin.encodePool.threads) { | ||
| expect(warmIds.has(threadId)).toBe(true); | ||
| } | ||
| }); | ||
| }); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.