Skip to content

fix(producer): cap the software encoder to the cores capture leaves free - #8019

Merged
miguel-heygen merged 1 commit into
heygen-com:mainfrom
zhengqiuyang:fix/overlap-encoder-threads
Nov 17, 2026
Merged

miguel-heygen merged 1 commit into
heygen-com:mainfrom
zhengqiuyang:fix/overlap-encoder-threads

Conversation

@zhengqiuyang

Copy link
Copy Markdown
Contributor

What

Cap the software encoder's thread pool to the cores capture still leaves free when an H.264 export overlaps a multi-worker capture. The encoder no longer spawns a full-width x264 pool next to the capture workers.

Why

canEncodeWhileCapturing() allows the overlap whenever workerCount < cpuCount, i.e. when at least one core is nominally free. But appendSoftwareEncoderThreadsArg() gives the encoder every usable core by default, so the overlap that was meant to use the free core actually ran a full-width encoder pool beside the capture workers — the oversubscription described in #7458.

Related work

Fixes #7458.

Refs #5334: that PR degrades multi-worker capture when headroom is insufficient; this one keeps the overlap but gives the encoder only the cores capture is not using. They are complementary, not duplicates.

How

  • New pure helper overlapEncoderThreads(workerCount, cpuCount) = max(1, floor(cpuCount) - floor(workerCount)).
  • encodeWhileCapturing() takes an optional captureBudget and passes the reserved count as softwareEncoderThreads through videoEncoderOptions() → StreamingEncoderOptions → appendSoftwareEncoderThreadsArg().
  • Both overlap call sites (renderOrchestrator, renderChunk) pass their worker count and CPU count.
  • The post-capture encode path is unchanged: it keeps the encoder default (all usable cores). If no budget is passed, behavior is byte-identical to before.

Test plan

  • Unit tests added/updated
    • overlapEncoderThreads(4, 8) = 4, (6, 8) = 2, (7, 8) = 1, never 0.
    • buildStreamingArgs() emits -threads 2 when softwareEncoderThreads: 2.
    • videoEncoderOptions() carries the field only when a budget is passed.
  • Typecheck: @hyperframes/engine and @hyperframes/producer both pass.
  • Manual testing performed — no ffmpeg binary on this Windows checkout, so the byte-identical ffmpeg integration cases in encodeWhileCapturing.test.ts could not run locally; CI has ffmpeg.
  • Documentation updated — not applicable.
  • Comments follow CONTRIBUTING.md and explain why.

Commands run:

bun run --filter @hyperframes/engine typecheck   -> 0
bun run --filter @hyperframes/producer typecheck -> 0
vitest packages/engine/src/services/streamingEncoder.threads.test.ts -> pass
vitest encodeWhileCapturing.test.ts -t "caps the software encoder"    -> pass
oxlint / oxfmt --check on the changed files                           -> pass

@miguel-heygen miguel-heygen left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Clean, well-scoped fix for #7458.

Strengths

  • overlapEncoderThreads (encodeStage.ts) is pure and floors at 1. canEncodeWhileCapturing already requires workerCount < cpuCount, so the cap is always at least one real free core.
  • The cap only applies when a captureBudget is passed, so the post-capture encode and every other caller keep the old -threads value.
  • Distributed determinism holds. chunkWorkerCount comes from computeWorkerSizing, which reads CPU count, cgroup quota and total memory, so it is stable per worker. Same-worker retries therefore get the same -threads value and stay byte-identical. Cross-worker output was already PSNR-equivalent, not byte-equal.

Important (follow-up, not blocking)

  • -threads is fixed for the whole ffmpeg process. If the encoder falls behind capture, finish() encodes the remaining backlog with the capped pool (e.g. 1 thread at 7/8 workers) while every core is idle. Worth checking in a render benchmark at high worker:core ratios that wall time doesn't regress against the oversubscribed baseline.
  • The two "byte-identical to the encode after capture" tests in encodeWhileCapturing.test.ts call encodeWhileCapturing without a budget. They no longer cover the configuration production uses for the overlap. A capped overlap is expected to differ from the full-width post-capture encode, which is fine, but a short note in the tests would stop a later reader from assuming they're equivalent.

Nit

  • renderChunk.ts calls usableCpuCount() twice on the overlap line. Hoisting it into a local would guarantee that the gate and the budget see the same value.

Verdict: APPROVE
Reasoning: Correct, minimal and opt-in at the two overlap call sites. Distributed per-worker determinism is preserved. CI passed on this head.

`canEncodeWhileCapturing` started an H.264 export beside the capture workers
whenever `workerCount < cpuCount`, but `appendSoftwareEncoderThreadsArg` gave the
encoder every usable core, so the overlap spawned a full-width x264 pool next to
the capture workers (heygen-com#7458).

The overlap now reserves the cores capture is using: `softwareEncoderThreads =
max(1, cpuCount - workerCount)` travels through the streaming encoder options to
ffmpeg's `-threads`, while the post-capture encode keeps the old default.
auto-merge was automatically disabled November 17, 2026 04:45

Head branch was pushed to by a user without write access

@zhengqiuyang
zhengqiuyang force-pushed the fix/overlap-encoder-threads branch from 502f128 to 79e5416 Compare November 17, 2026 04:45
@zhengqiuyang

Copy link
Copy Markdown
Contributor Author

Fixed the comment-ratchet regression in 79e5416 (dropped the added comments; node scripts/comment-ratchet.mjs passes locally against 38aaad1). The Windows engine-cli-2 lane failure reads as environmental (Studio workspace build check failed / Install failed, plus browser-test timeouts), so it should be worth one re-run on the new head.

The workflows on the new commit are action_required from the fork; could you re-approve them so CI can run?

@miguel-heygen miguel-heygen left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Clean, well-scoped fix for #7458. Re-approving at 79e5416: the only delta from 502f128 (approved earlier) is the doc comments dropped for comment-ratchet; the code is identical.

Strengths

  • overlapEncoderThreads (encodeStage.ts) is pure and floors at 1. canEncodeWhileCapturing already requires workerCount < cpuCount, so the cap is always at least one real free core.
  • The cap only applies when a captureBudget is passed, so the post-capture encode and every other caller keep the old -threads value.
  • Distributed determinism holds. chunkWorkerCount comes from computeWorkerSizing, which reads CPU count, cgroup quota and total memory, so it is stable per worker. Same-worker retries therefore get the same -threads value and stay byte-identical. Cross-worker output was already PSNR-equivalent, not byte-equal.

Important (follow-up, not blocking)

  • -threads is fixed for the whole ffmpeg process. If the encoder falls behind capture, finish() encodes the remaining backlog with the capped pool (e.g. 1 thread at 7/8 workers) while every core is idle. Worth checking in a render benchmark at high worker:core ratios that wall time doesn't regress against the oversubscribed baseline.
  • The two "byte-identical to the encode after capture" tests in encodeWhileCapturing.test.ts call encodeWhileCapturing without a budget. They no longer cover the configuration production uses for the overlap. A capped overlap is expected to differ from the full-width post-capture encode, which is fine, but a short note in the tests would stop a later reader from assuming they're equivalent.

Nit

  • renderChunk.ts calls usableCpuCount() twice on the overlap line. Hoisting it into a local would guarantee that the gate and the budget see the same value.

Verdict: APPROVE
Reasoning: Correct, minimal and opt-in at the two overlap call sites. Distributed per-worker determinism is preserved. CI passed on this head.

@miguel-heygen
miguel-heygen added this pull request to the merge queue Nov 17, 2026
Merged via the queue into heygen-com:main with commit f89f6d3 Nov 17, 2026
90 checks passed
robinebers added a commit to robinebers/hyperframes that referenced this pull request Nov 22, 2026
…ing its threads

heygen-com#8019 capped the software encoder that runs beside a multi-worker capture to
`cpuCount - workerCount` threads. ffmpeg keeps that pool for its whole run, so
once capture ends the encoder finishes the chunk on the reserved core alone. On a
10 GB AWS Lambda function (6 vCPU, 5 capture browsers) every chunk encoded with
one x264 thread: a 150-frame chunk's encode went from about 4 s to 19 s and the
whole render took 2.5 times as long.

The overlapping encoder now keeps its usual thread count and runs below normal
priority instead (os.setPriority, PRIORITY_BELOW_NORMAL; the priority a Windows
user lowered ffmpeg to in heygen-com#7458). Capture, at normal priority, still gets the CPU
first; the encoder takes the cores capture leaves idle and all of them once it
ends. Its bitstream again matches the encode after capture, since both use the
same thread count. A refused priority change leaves the encode at normal priority.
robinebers added a commit to robinebers/hyperframes that referenced this pull request Nov 22, 2026
…ing its threads

heygen-com#8019 capped the software encoder that runs beside a multi-worker capture to
`cpuCount - workerCount` threads. ffmpeg keeps that pool for its whole run, so
once capture ends the encoder finishes the chunk on the reserved core alone. On a
10 GB AWS Lambda function (6 vCPU, 5 capture browsers) every chunk encoded with
one x264 thread: a 150-frame chunk's encode went from about 4 s to 19 s and the
whole render took 2.5 times as long.

The overlapping encoder now keeps its usual thread count and runs below normal
priority instead (os.setPriority, PRIORITY_BELOW_NORMAL; the priority a Windows
user lowered ffmpeg to in heygen-com#7458). Capture, at normal priority, still gets the CPU
first; the encoder takes the cores capture leaves idle and all of them once it
ends. Its bitstream again matches the encode after capture, since both use the
same thread count. A refused priority change leaves the encode at normal priority.
robinebers added a commit to robinebers/hyperframes that referenced this pull request Nov 25, 2026
…ing its threads

heygen-com#8019 capped the software encoder that runs beside a multi-worker capture to
`cpuCount - workerCount` threads. ffmpeg keeps that pool for its whole run, so
once capture ends the encoder finishes the chunk on the reserved core alone. On a
10 GB AWS Lambda function (6 vCPU, 5 capture browsers) every chunk encoded with
one x264 thread: a 150-frame chunk's encode went from about 4 s to 19 s and the
whole render took 2.5 times as long.

The overlapping encoder now keeps its usual thread count and runs below normal
priority instead (os.setPriority, PRIORITY_BELOW_NORMAL; the priority a Windows
user lowered ffmpeg to in heygen-com#7458). Capture, at normal priority, still gets the CPU
first; the encoder takes the cores capture leaves idle and all of them once it
ends. Its bitstream again matches the encode after capture, since both use the
same thread count. A refused priority change leaves the encode at normal priority.
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.

Potential CPU oversubscription when capture and software H.264 encoding overlap

2 participants