Skip to content

perf(size): ship one binary — run the daemon from fbuild instead of a second full-stack executable #1851

Closed

Description

@zackees

Problem

Every wheel ships two executables that each statically link nearly the whole workspace (build engine, packages, deploy, zccache, tree-sitter, …):

dist profile (#1846), Linux x86_64, --strip-debug size
fbuild 35.7 MB
fbuild-daemon 38.1 MB

fbuild-cli uses the build stack in-process (fbuild_build:: 52 refs, fbuild_packages:: 36, fbuild_deploy:: 13 in crates/fbuild-cli/src, re-counted against main @ bebd679c), so the CLI can't simply become a thin HTTP client without a large refactor. The two binaries are mostly the same code twice.

Strategy: one binary, argv[0] multicall, self-relocated daemon

Ship one native executable, fbuild. fbuild-daemon stops being a separate link and becomes a name for the same image, selected by argv[0] — busybox-style, the way soldr already does it.

  1. One link. fbuild-daemon's main body moves behind a library entry (fbuild_daemon::entry::run() -> i32); fbuild-cli depends on fbuild-daemon as a library. The fbuild-daemon [[bin]] is dropped from the release build.
  2. argv[0] dispatch before clap/tokio. A small multicall module in fbuild-cli takes the file stem of argv[0] (strip dir, strip .exe, case-insensitive on Windows) and, if it is fbuild-daemon, calls straight into the daemon entry. Anything else falls through to today's CLI path unchanged.
  3. fbuild-daemon is a hardlink to fbuild (same inode). Not a symlink (Windows symlinks need SeCreateSymbolicLinkPrivilege/developer mode, and argv[0] through a symlink is launcher-dependent), and not a copy (doubles disk again — the thing we're removing).
  4. The running daemon executes from a relocated copy under the user data dir, not from the installed image. This, not the hardlink, is what fixes Windows upgrade locking — see below.

soldr reference (read the code, it is the template)

  • ~/dev/soldr/crates/soldr-cli/src/multicall.rs:1-7 — module doc: the release archive no longer ships soldr-shim, soldr-clang-shim, or zccache-soldr sidecars; installers hardlink/copy the main soldr binary under each shim name and this module "selects the tiny fast path before clap/tokio startup".
  • multicall.rs:104-119 — maybe_dispatch(); :116 is ShimIdentity::SoldrDaemon => Some(MulticallDispatch::Exit(crate::daemon_entry::run())).
  • multicall.rs:143-158 — classify_argv0() (stem → identity, SOLDR_DAEMON = "soldr-daemon" at :23); :184 argv0_stem(); tests at :605 (classify_argv0_recognizes_runtime_aliases).
  • multicall.rs:62-81 — SOLDR_ARGV0_SHIM / apply_shim_argv0_override(): an env carrier for argv[0] identity when an sh trampoline stands in for a hardlink (can't set argv[0] portably). fbuild needs the equivalent only if we ever fall back to a trampoline; note it, don't build it up front.
  • ~/dev/soldr/crates/soldr-core/src/self_relocate.rs:102-117 — ensure_daemon_relocated(): copies the daemon image into ~/.soldr/runtime/soldr-daemon/<hash>/ and runs from there so "uninstall/upgrade/rm of the source no longer needs to wait for the daemon to exit". Returns the source unchanged if it already lives under the runtime root (:155). Content-hashed dir, a runtime-root lock around the copy (:224, :478), and periodic GC of stale copies (:344-348, :427 refresh_running_image_ledger so GC never reaps the running image).
  • Caller: ~/dev/soldr/crates/soldr-cli/src/broker_launcher.rs relocates before spawning.

Why argv[0] and not fbuild __daemon …

The previous version of this issue proposed a hidden subcommand. argv[0] is strictly better here:

  • External callers keep working because the name still exists. FastLED, scripts, docs, service definitions and the running-process broker all exec fbuild-daemon by name. With a hidden subcommand every one of them has to change to fbuild __daemon, in lockstep with a release. With argv[0] they don't change at all — fbuild-daemon is still on disk and still behaves like the daemon.
  • Process identity is preserved. fbuild identifies its daemon by exe stem in several places: fbuild_paths::daemon_ownership::DAEMON_EXE_STEM = "fbuild-daemon" (crates/fbuild-paths/src/daemon_ownership.rs:36, used by fbuild-paths/src/lib.rs:475,507,662), running_process::DAEMON_BINARY_NAME (crates/fbuild-paths/src/running_process.rs:49), port_holders.rs:117 in fbuild-serial, and broker/service.rs:233 in the daemon. A fbuild __daemon process has stem fbuild and is indistinguishable from a CLI invocation; an argv[0] daemon's stem is still fbuild-daemon (it is the alias path that's executed — or the relocated copy, which is named fbuild-daemon[.exe]).
  • No clap cost and no second CLI surface. The dispatch happens before clap parses fbuild's large subcommand tree and before any tokio runtime is built, so the daemon starts exactly as it does today (its own Args::parse(), its own #[tokio::main]-equivalent runtime). There's no hidden subcommand to keep out of --help, document, or guard from users.

What has to change (in order)

  1. crates/fbuild-daemon/src/main.rs — move the body of async fn main() (:42-44, carrying the too_many_lines expect, which must not grow) into a library fn fbuild_daemon::entry::run() -> i32 that builds its own multi-thread runtime. Keep fbuild_daemon::heap_profile::start_from_env() as the first statement (perf: adopt the mimalloc-pprof allocator for heap/leak profiling; verify on-CPU, off-CPU, and symbolization from running-process #1361). main.rs becomes a 3-line shim (kept for dev/cargo run -p fbuild-daemon while the alias rolls out).
  2. crates/fbuild-cli/Cargo.toml — depend on fbuild-daemon as a library (no new crate; this fits the modules-first rule).
  3. New module crates/fbuild-cli/src/multicall.rs — classify_argv0 / maybe_dispatch modeled on soldr's, recognising only fbuild-daemon. Unit tests for: /x/fbuild-daemon, C:\x\FBUILD-DAEMON.EXE, relocated path, fbuild (falls through), empty argv.
  4. crates/fbuild-cli/src/main.rs:9 — the hook goes at the very top of fn main(), before the 8 MB fbuild-main thread trampoline and before async_main_entry() (which derives the dev daemon namespace and builds the tokio runtime at :51, then cli::async_main() runs clap). The daemon path should not pay for either; it sets its own env exactly as the standalone binary does today. If the daemon needs the large stack too, spawn it on its own thread inside entry::run().
  5. Alias creation — see Packaging. fbuild (the CLI's daemon-spawn path in crates/fbuild-cli/src/daemon_client.rs) ensures fbuild-daemon[.exe] exists next to itself as a hardlink before spawning, falling back to spawning a relocated copy directly.
  6. Self-relocation — port ensure_daemon_relocated into fbuild-paths (shared, dependency-free; both CLI and daemon already depend on it). Target: <fbuild cache root>/runtime/fbuild-daemon/<content-hash>/fbuild-daemon[.exe], respecting FBUILD_DEV_MODE (~/.fbuild/dev/…). Copy under a runtime-root lock, atomic rename into place, content hash as dir name, and GC of stale copies that never touches the running image's dir. The CLI relocates, then spawns the relocated path; on Windows the relocated copy must carry any adjacent DLLs/PDB it needs (check the wheel layout — soldr's exe_depends_on_bundled_wheel_libs at self_relocate.rs:282 exists for exactly this).
    • Lock policy: the runtime-copy lock is a new file lock. CLAUDE.md's "no file-based locks" rule has one sanctioned exception (daemon startup/lifetime ownership + spawn election, handoff(#1154): finish cold-cache reset with soldr-style daemon ownership #1159); the relocation copy sits inside that spawn-election path, so it should be done under the existing spawn.lock rather than adding a new lock. If that doesn't fit, it needs explicit maintainer sign-off and a CLAUDE.md update.
  7. Identity/diagnostics —
    • crates/fbuild-core/src/platform/executable.rs:87 current_image_sibling_candidates("fbuild-daemon") keeps resolving: the alias is a real file next to fbuild, so [fbuild-daemon, fbuild-daemon.exe] both still work with no code change.
    • crates/fbuild-cli/src/daemon_client/restart_diag.rs:12-39 compares the sibling fbuild-daemon mtime against the image the daemon reports (health.source_mtime, exe). With relocation, the daemon reports the relocated exe; the health payload must also carry the original (source) exe path + mtime (soldr carries this as SOLDR_ORIGINAL_EXE, self_relocate.rs:32), and restart_diag must compare sibling-vs-source and print both paths. Otherwise every relocated daemon looks like a "second launcher serving".
    • Note a hardlink shares mtime with fbuild — that's correct (same image) and makes the "sibling newer than running" rebuild check still work.
    • Endpoint/version key (fbuild_paths::default_daemon_port / daemon_endpoint_key) is derived from backend version + cache identity, not exe path — relocation must not change it. Add a test that a relocated and a non-relocated daemon of the same version derive the same endpoint, and two versions still derive different ones (daemon: endpoint isolation follow-up to #940 — key the default endpoint by cache-identity/checkout + deterministic version arbitration #1009).
    • Root-owner / spawn locks (handoff(#1154): finish cold-cache reset with soldr-style daemon ownership #1159) are under the cache root, not next to the exe, so they're unaffected — verify, don't assume.
  8. Broker — crates/fbuild-daemon/src/broker/service.rs:233 builds the service definition with name fbuild-daemon; the executable path it passes becomes the relocated path. DAEMON_BINARY_NAME stays.
  9. Packaging — ci/build_backend.py, .github/workflows/template_native_build.yml, ci/wheel_check.py (below).

Windows upgrade locking — the precise mechanism

Today a running daemon maps fbuild-daemon.exe, and Windows denies delete/overwrite of a mapped image, so only that one file is pinned during pip install -U / uv sync (and fbuild.exe is free unless a CLI is mid-run).

A hardlink alone does not fix this — it makes it worse. fbuild.exe and fbuild-daemon.exe would be two directory entries for one file; the loader's section lock is on the file, not the name. A daemon started as fbuild-daemon.exe therefore pins fbuild.exe too, and the installer's attempt to replace fbuild.exe fails for as long as any daemon is up — which is "always" for a long-lived daemon.

Relocation is what fixes it. The daemon never executes from the install directory: the CLI copies the image to <cache root>/runtime/fbuild-daemon/<hash>/fbuild-daemon.exe and spawns that. The only mapped file is under the user data dir, so the installer can freely replace Scripts\fbuild.exe and its alias. The next CLI invocation of the new version hashes a different image, relocates it to a new <hash> dir, and its version-keyed endpoint starts a new daemon; the old daemon keeps running from its own copy until it idles out, and GC removes its dir only once it's no longer the running image. This is the same contract soldr documents at self_relocate.rs:102-108.

Residual: a CLI process (fbuild.exe) running during the upgrade still pins the install image for its lifetime — same as today, not in scope.

Packaging and measurement

  • Wheels can't carry hardlinks. A wheel is a zip; it would store fbuild-daemon as a second full copy. So the wheel ships only fbuild, and the alias is created at runtime by the CLI on first daemon spawn (step 5) — wheels have no post-install hook. If the scripts dir is read-only, the CLI skips the alias and spawns the relocated copy directly (which is named fbuild-daemon, so stem identity holds). External callers that exec fbuild-daemon before any fbuild run would not find it on a fresh install — that is the compatibility gap the rollout below covers.
  • ci/build_backend.py — stop building/staging fbuild-daemon (TARGET_DAEMON_NAME / STAGED_DAEMON_PATH at :84-90, _find_fbuild_daemon_from_json :240, _find_fbuild_daemon_by_search :303, the -p fbuild-daemon build at :450, the error at :496).
  • .github/workflows/template_native_build.yml — drop -p fbuild-daemon (:261, :292-295), the daemon cp into staging (:342), the daemon entry in the strip loop (:409), and the daemon debug sidecars fbuild_daemon.pdb / fbuild-daemon.dSYM / fbuild-daemon.dwp (:432-434).
  • ci/wheel_check.py:31-38 — rebase the comment and budgets on one artifact. Current note: ~30 MiB per wheel (fbuild 13.5 + fbuild-daemon 14.5 MiB gzip -9), MAX_WHEEL_BYTES = 40 MiB, MAX_RELEASE_BYTES = 200 MiB. Add an assertion that the wheel contains exactly one native executable (no fbuild-daemon).
  • Expectation (from the table above; to be measured on the release dist build after the change): one ~40 MB binary instead of ~74 MB (≈ 35.7 + 38.1), roughly 30+ MB uncompressed / ~12 MB compressed saved per wheel, ×6 platforms per release. The merged binary's exact size and the new per-wheel/compressed numbers are not yet measured — record them in the PR and set the new wheel_check.py budgets from them with the same ~30% headroom.

Acceptance

Phases / rollout

  1. Library entry + multicall, two binaries still shipped. Refactor the daemon main into entry::run(), add multicall.rs and the main.rs hook. Both binaries still built; proves fbuild renamed to fbuild-daemon runs the daemon. No packaging change.
  2. Self-relocation behind the spawn path, still two binaries. Lands the Windows-locking fix and the restart_diag/health changes independently of the size change, and gets a release of soak.
  3. One binary. Drop the fbuild-daemon bin from build/staging/wheel_check; CLI creates the alias. Release notes tell external callers that fbuild-daemon now exists after the first fbuild invocation.
  4. Compatibility window (a release or two). Keep the fbuild-daemon alias indefinitely if cheap (it costs nothing on disk), but audit FastLED and docs for direct fbuild-daemon exec on a fresh install and switch them to "spawn via fbuild". Then remove the standalone fbuild-daemon [[bin]] / main.rs shim if dev workflows no longer need it.

Activity

  1. zackees commented on Nov 2, 2026

    @zackees
    MemberAuthor

    Phase 1 of this is up as #1852: fbuild_daemon::entry::run() + argv[0] multicall in fbuild-cli, both binaries still shipped (no packaging change). 4137 unit tests, workspace clippy, both crates' dylint sets, boundary/layout/crate gates all green; e2e proof that a renamed fbuild image boots the real daemon (health payload + profiler-first ordering preserved). Phases 2-4 (self-relocation, one binary, alias creation) intentionally untouched.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions