Skip to content

feat(binding): expose extractBatch on the native and WASM engines - #1114

Open
owjs3901 wants to merge 10 commits into
perf/parallel-prewarm-corefrom
perf/parallel-prewarm-binding
Open

owjs3901 wants to merge 10 commits into
perf/parallel-prewarm-corefrom
perf/parallel-prewarm-binding

Conversation

@owjs3901

@owjs3901 owjs3901 commented Dec 5, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Stack: #1113 (A, core) → this PR (B, binding) → C (Next plugin prewarm, on #1112). Based on #1113's branch.

Adds extractBatch to both engines:

extractBatch(rows: [filename, code, cssDir][], packageName, singleCss,
  importMainCssInCode, importMainCssInCss, importAliases, sourceMap,
  threads?): (Output | Error)[]

Each result is what codeExtract / codeExtractWithoutSourceMap would return for that row, without CSS, or the Error it would have thrown. Class names, CSS and the exported maps are the same as extracting the rows one by one in order.

  • Native (@devup-ui/binding): runs perf(binding): extract a batch on worker threads with in-order numbering #1113's core batch on worker threads. threads defaults to the number of cores, capped at 8: past about 4 the resolver on the JS thread is the bottleneck. The module resolver is still called on the JS thread only, once per (specifier, importer) per batch.
  • WASM (@devup-ui/wasm): extracts the rows one by one through the same path as codeExtract. Both engines keep one ABI, which the parity harness requires.
  • DevupCompiler marks extractBatch optional, so plugins can fall back on older engines.
  • argument-validation.cjs validates the new arguments the same way on both engines. The ABI name lists, the export-count checks in the wasm post-build script, and the validation counts (56 → 64 positions) are updated.

Tests

  • New corpus scenario extract-batch-sources-in-order, compared byte for byte between native and WASM by identity.mjs. It covers a resolver answering with a module, nothing, a throw and a malformed result; a broken source; source maps; explicit and default thread counts; the empty batch; a bad row; and no resolver. identity.mjs, validation.mjs (742 runs, 0 failed) and entries.mjs (node and bun × both engines) pass locally.
  • A private native check covers a raw resolver that throws past the adapter. That failure comes back for that source alone, at 1 and 2 threads.
  • wasm-abi.test.ts passes. Lint is clean.

Micro benchmark (large Turbopack app, 334 files, real JS resolver, 7 runs, median): one by one 260 ms → extractBatch at 2 threads 151 ms, 4 threads 130 ms, 8 threads 125 ms. Outputs and exported state were identical at every thread count.


요약

스택: #1113 (A, 코어) → 이 PR (B, 바인딩) → C (Next 플러그인 prewarm, #1112 기반). #1113 브랜치 기반입니다.

두 엔진 모두에 extractBatch를 추가합니다.

extractBatch(rows: [filename, code, cssDir][], packageName, singleCss,
  importMainCssInCode, importMainCssInCss, importAliases, sourceMap,
  threads?): (Output | Error)[]

각 결과는 그 행에 대해 codeExtract / codeExtractWithoutSourceMap이 반환했을 출력(CSS 제외)이거나, 던졌을 Error입니다. 클래스 이름, CSS, 내보낸 map은 행을 순서대로 하나씩 추출한 결과와 같습니다.

  • 네이티브 (@devup-ui/binding): #1113의 코어 배치를 워커 스레드에서 실행합니다. threads 기본값은 코어 수이고 최대 8입니다. 약 4개를 넘으면 JS 스레드의 리졸버가 병목이 되기 때문입니다. 모듈 리졸버는 여전히 JS 스레드에서만, 배치 안에서 (specifier, importer)마다 한 번 호출됩니다.
  • WASM (@devup-ui/wasm): codeExtract와 같은 경로로 행을 하나씩 추출합니다. 패리티 하네스가 요구하는 대로 두 엔진의 ABI를 하나로 유지합니다.
  • DevupCompiler에서 extractBatch는 선택 항목이라서, 플러그인은 이전 엔진에서 기존 경로로 돌아갈 수 있습니다.
  • argument-validation.cjs는 새 인자를 두 엔진에서 똑같이 검증합니다. ABI 이름 목록, wasm 빌드 후 스크립트의 export 개수 검사, 검증 개수(위치 56 → 64)도 갱신했습니다.

테스트

  • 새 코퍼스 시나리오 extract-batch-sources-in-order를 identity.mjs가 네이티브와 WASM 사이에서 바이트 단위로 비교합니다. 다루는 경우는 리졸버가 모듈을 돌려주거나, 아무것도 돌려주지 않거나, 예외를 던지거나, 잘못된 결과를 돌려주는 경우입니다. 깨진 소스, 소스맵, 명시한 스레드 수와 기본 스레드 수, 빈 배치, 잘못된 행, 리졸버 없음도 포함합니다. 로컬에서 identity.mjs, validation.mjs(742회 실행, 실패 0), entries.mjs(node와 bun × 두 엔진)가 통과합니다.
  • 비공개 네이티브 검사는 어댑터를 거치지 않고 예외를 던지는 raw 리졸버를 다룹니다. 그 실패는 해당 소스에만, 1 스레드와 2 스레드에서 돌아옵니다.
  • wasm-abi.test.ts가 통과하고 lint 경고가 없습니다.

마이크로 벤치마크(대형 Turbopack 앱, 334개 파일, 실제 JS 리졸버, 7회 중앙값): 하나씩 260 ms → extractBatch 2 스레드 151 ms, 4 스레드 130 ms, 8 스레드 125 ms. 모든 스레드 수에서 출력과 내보낸 상태가 같았습니다.

Fix after review: a valid thread count whatever the caller passes

The native adapter turned threads into a u32 with >>> 0, so -1 asked for 4,294,967,295 workers (one per source in practice), 2.5 for 2 and NaN for 0. An explicit count also had no cap. threads now reaches the core as a number. A whole count from 1 is kept and capped at 8, like the default. Anything else (none, 0, a negative, a fraction, NaN, an infinity) gets the default: the cores, at most 8. This follows the n argument convention, so no count throws. The WASM engine reads it the same way and still extracts one by one. Tests: an rstest table in core, odd counts in the binding corpus both engines run and compare, raw native calls in the private host, and a bun test through both engines.


리뷰 후 수정: 호출자가 무엇을 넘기든 올바른 스레드 수

네이티브 어댑터는 threads를 >>> 0으로 u32로 바꿨습니다. 그래서 -1은 워커 4,294,967,295개(실제로는 소스마다 하나), 2.5는 2개, NaN은 0개를 요청했습니다. 명시한 수에는 상한도 없었습니다. 이제 threads는 숫자 그대로 코어에 전달됩니다. 1 이상의 정수는 기본값처럼 최대 8로 제한해 그대로 쓰고, 그 밖의 값(없음, 0, 음수, 소수, NaN, 무한대)은 기본값(코어 수, 최대 8)을 씁니다. n 인자 규칙을 따르므로 어떤 수도 예외를 던지지 않습니다. WASM 엔진도 같은 방식으로 읽고, 여전히 하나씩 추출합니다. 테스트: 코어의 rstest 표, 두 엔진이 실행해 비교하는 바인딩 corpus의 특이한 스레드 수, private host의 네이티브 직접 호출, 두 엔진을 거치는 bun 테스트.

🤖 Generated with Claude Code

`extractBatch(rows, package, singleCss, importMainCssInCode,
importMainCssInCss, aliases, sourceMap, threads?)` extracts [filename, code,
cssDir] rows and returns, for each, the output codeExtract would give without
CSS or the Error its source failed with. The native engine runs the core batch
on worker threads (by default the cores, at most eight; more only queue on
the resolver), calling the module resolver on the JS thread. The WASM engine
extracts the rows one by one, so both engines keep one ABI.

The argument validator, ABI name lists and the native/WASM parity corpus learn
the new export; a corpus scenario compares the batch on both engines with
every resolver answer, and a private native check covers a resolver that
throws past the adapter.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
owjs3901 and others added 2 commits December 5, 2026 12:25
…I check

Name the WASM threads parameter like the native one, so the ABI coverage
pairs it with its validation case.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The native consumer check and the compiler structural types test pin the
number of engine exports; extractBatch adds one to each.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Dec 5, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

Files with missing lines Coverage Δ
bindings/devup-ui-binding/index.mjs 100.00% <100.00%> (ø)
bindings/devup-ui-binding/src/host.rs 100.00% <ø> (ø)
bindings/devup-ui-binding/src/lib.rs 100.00% <ø> (ø)
bindings/devup-ui-wasm/src/lib.rs 100.00% <ø> (ø)
bindings/devup-ui-wasm/src/reservation.rs 100.00% <ø> (ø)
bindings/devup-ui-wasm/src/reservation/rows.rs 100.00% <100.00%> (ø)
libs/binding/src/batch.rs 100.00% <100.00%> (ø)
libs/binding/src/lib.rs 100.00% <ø> (ø)

... and 1 file with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

owjs3901 and others added 5 commits December 5, 2026 16:47
…binding

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…binding

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…binding

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…odegen again

Holding each source's turn until its output was committed (acb9501)
was correct but serialized code generation and the stylesheet commit: the
large app's prewarm only went from 289 ms to about 200-230 ms.

The turn passes at code generation again, and the batch commits on the
calling thread in source order. What the commit would name first
(global-bucket names of styles a source reaches through an import, for
instance) is now named before the turn passes: the extractor walks the
styles in the commit's order and scope (`name_as_committed`), so the
commit only looks names up. Outside a batch the hook does nothing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…binding

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
owjs3901 and others added 2 commits December 5, 2026 18:55
…ler passes

The native adapter turned `threads` into a u32 with `>>> 0`, so -1 asked
for 4,294,967,295 workers (one per source in practice), 2.5 for 2 and NaN
for 0, and an explicit count was never capped at the default's eight.

`threads` now reaches the core as a number, and `batch_threads` keeps a
whole count from 1, capped at 8, and gives the default (the cores, at most
8) for none, 0, a negative, a fraction, NaN or an infinity. This follows
the `n` argument convention: any number or null is accepted, and a count
that makes no sense falls back like an `atomHoist` that does. The WASM
engine reads `threads` as a number too and still extracts one by one.

Covered by an rstest table in core, by the binding corpus that both
engines run and compare, by the native private host calling the raw
binding with those counts, and by a bun test through both engines.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

1 participant