Skip to content

perf: let the daemon's zccache run one compiler per core - #1546

Merged
zackees merged 1 commit into
mainfrom
perf/zccache-compile-cap
Sep 28, 2026
Merged

zackees merged 1 commit into
mainfrom
perf/zccache-compile-cap

Conversation

@zackees

@zackees zackees commented Sep 28, 2026 •

Copy link
Copy Markdown
Member

Part of #1537 (the "oversubscription" item) and #1469.

Problem

zccache's daemon core gates every compiler spawn behind one global semaphore (compile_concurrency.rs). Unset, its size is cores - 1 on hosts it doesn't recognise as CI. The CLI spawns fbuild-daemon with a scrubbed environment (daemon_client.rs, env_clear() plus a user baseline plus FBUILD_*), so the daemon never sees CI or GITHUB_ACTIONS. Every build, CI included, runs at most cores - 1 compilers.

Evidence on bench/blink -e esp32s3, pinned to 4 cores:

  • The daemon log reads compile concurrency capped at 3 via in-process semaphore.
  • Sampling /proc every ~25 ms during a cold build shows exactly 3 xtensa-esp32s3-elf-g++ drivers and 3 cc1/cc1plus processes the whole time, even though fbuild hands out 8 permits.
  • The CI numbers fit this: compile-core-variant takes 4.99 s against a raw compiler floor of 4.02 s. Three-way parallelism predicts about 4.0 × 4/3 ≈ 5.3 s.

Fix

Before the embedded service starts, the daemon exports ZCCACHE_MAX_PARALLEL_COMPILES = available_parallelism().

  • An explicit ZCCACHE_MAX_PARALLEL_COMPILES already in the daemon's environment is left alone.
  • FBUILD_MAX_PARALLEL_COMPILES overrides the default, because FBUILD_* survives the scrubbed spawn. It accepts a number, 0, or unlimited; anything invalid falls back to every core.
  • The rule is a pure function, compile_cap_env_value, covered by unit tests.

It defaults to every core, not cores - 1, because zccache already runs compiles at low priority on interactive hosts, so the desktop stays responsive. Holding a core back on top of that only costs throughput.

Oversubscription

That zccache gate is the one process-wide governor. It covers every concurrent build and every library compile the daemon runs, so the box never runs more than cores compilers, whatever --jobs each build asks for. fbuild's per-build default_jobs = cores*2 stays as is: the extra permits act as a prefetch queue, so the next TU is already waiting at the gate when a compiler exits.

Numbers

Before (CI, main 417f5f43, 4-core ubuntu-24.04): fbuild cold 6.61 s, compile-core-variant 4.99 s, raw floor 4.02 s, PlatformIO 7.08 s.
After: benchmark dispatched on this branch, results to follow in a comment.

Local A/B timings aren't usable: the shared box is at load average ~29 from other builds. The mechanism was verified by process sampling instead.

Summary by CodeRabbit

  • New Features
    • Added support for configuring the embedded build service’s maximum number of parallel compiles through environment variables. An existing zccache limit takes precedence; otherwise, a valid fbuild override is used, with available CPU cores as the fallback.
    • The limit accepts a positive number or unlimited.

zccache caps concurrent compilers at cores-1 unless it sees a CI marker,
and the CLI spawns the daemon with a scrubbed environment that never
carries one. Every build, CI included, therefore ran 3 compilers on a
4-core runner. The daemon now exports ZCCACHE_MAX_PARALLEL_COMPILES as
every core before the embedded service starts (overridable via
FBUILD_MAX_PARALLEL_COMPILES; an explicit ZCCACHE_ value is left alone).
That cap is the single global governor across concurrent builds.
@coderabbitai

coderabbitai Bot commented Sep 28, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository: FastLED/fbuild/.coderabbit.yaml

Review profile: CHILL

Plan: Advanced

Run ID: e72d6128-f444-47af-8f0a-1db1ff355d1f

📥 Commits

Reviewing files that changed from the base of the PR and between 417f5f4 and 9162728.

📒 Files selected for processing (2)
  • crates/fbuild-build-engine/src/zccache_embedded.rs
  • crates/fbuild-daemon/src/main.rs

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.


📝 Walkthrough

Walkthrough

The build engine now selects an embedded zccache compile cap from environment values and available CPU parallelism. The daemon applies the selected cap before embedded zccache starts, while preserving an explicitly configured zccache value.

Changes

Embedded compile cap

Layer / File(s) Summary
Compile-cap selection and tests
crates/fbuild-build-engine/src/zccache_embedded.rs
The build engine defines the zccache and fbuild environment variable names. It selects a trimmed numeric or unlimited override, preserves an existing zccache value, and otherwise defaults to at least one available core. Tests cover these cases.
Daemon startup integration
crates/fbuild-daemon/src/main.rs
Before embedded zccache starts, the daemon reads the environment values and available CPU parallelism. It sets the zccache cap only when the selection helper returns a value.

Priority: ➖ Normal

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Merge Risk: ⚪ Minimal · up to 91627

This change raises the daemon's default compiler concurrency to use all available cores instead of one fewer, with tests covering the override and fallback logic. The change appears safe to merge based on the supplied evidence.

Security Architecture Review

Security architecture risk: 🔵 Low · up to 91627

The new limit is set when the daemon starts and can be overridden by the launcher. No build-request path to change it was identified. The implications for deployments that share a daemon remain uncertain.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — The resource effect is daemon-wide rather than confined to the launcher's individual build. Evidence does not establish cross-tenant exposure or authority beyond control of the daemon launch environment.

Trust Boundaries and Controls

  • observed — Environment scrubbing excludes arbitrary inherited variables while intentionally forwarding FBUILD_* launch controls. The cap is installed before the embedded service starts; no build-request route to reset it was identified.

Resilience and Maintainability Implications

  • inferred — Preserving an explicit zccache value and accepting unbounded overrides is consistent with launcher-owned configuration, but leaves resource containment dependent on who may start a daemon. The examined shutdown path flushes the backend; it does not resolve every interruption path.

Hardening Proposals

  • proposed — If a daemon is shared across different trust principals, define who may choose its startup cap and whether that deployment requires a bounded maximum.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: allowing the daemon's embedded zccache to run one compiler per available core.
Docstring Coverage ✅ Passed Docstring coverage is 87.50% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 8 functions across 2 files.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@zackees

zackees commented Sep 28, 2026

Copy link
Copy Markdown
Member Author

CI benchmark on this branch (run 36460654491, 4-core ubuntu-24.04, median of 3, bench/blink -e esp32s3):

main 417f5f43 this PR
fbuild cold 6611 ms 5695 ms (−14%)
compile-core-variant 4995 ms 4191 ms
raw compiler floor 4018 ms 3800 ms
PlatformIO cold 7083 ms 6714 ms
fbuild / PlatformIO cold 0.933 0.848
fbuild warm 153 ms 139 ms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Triage

Development

Successfully merging this pull request may close these issues.

1 participant