forge
Explore
Log in

Don't you dare go Hollow

Forge serve sets MIMALLOC_ALLOW_THP=0 itself by re-running once at start (same PID), since self-update replaces only the binary and never the unit

Browse files at this commit

05f3e023a10ba1973c84c3bb4d3929c01bd5cc78Ahmet Kaan GümüşParent: cae5972
Some checks failed
Show checks
  • perf
    • forge serve sets MIMALLOC_ALLOW_THP=0 itself by re-running once at start (same PID), since self-update replaces only the binary and never the unit
    • the unit no longer carries it, and an explicit value is left alone
Show full message
⚡ perf: forge serve sets MIMALLOC_ALLOW_THP=0 itself by re-running once at start (same PID), since self-update replaces only the binary and never the unit; the unit no longer carries it, and an explicit value is left alone
crates/forge-web/src/main.rsmodified+41 -0
@@ -109,6 +109,46 @@ fn parse_args(
109 Ok((config, admin, positional))109 Ok((config, admin, positional))
110 }110 }
111 111
112+/// mimalloc reads its options once, before `main`. With `MIMALLOC_ALLOW_THP=0` it also returns
113+/// freed memory in small pieces, about 7% less RSS on top of the prctl. Self-update replaces only
114+/// the binary, never the unit, so the server sets it itself: `serve` re-runs itself once with the
115+/// variable set (same PID, so systemd is unaffected). An explicit value is left alone, and
116+/// short-lived commands such as `hook` skip the extra exec.
117+fn reexec_with_allocator_env(raw: &[std::ffi::OsString]) {
118+ const VAR: &str = "MIMALLOC_ALLOW_THP";
119+ if std::env::var_os(VAR).is_some() {
120+ return;
121+ }
122+ let mut words = raw.iter();
123+ let mut command = None;
124+ while let Some(arg) = words.next() {
125+ match arg.to_str() {
126+ Some("--config") => {
127+ words.next();
128+ }
129+ Some("--admin") => {}
130+ _ => {
131+ command = Some(arg);
132+ break;
133+ }
134+ }
135+ }
136+ if command.is_some_and(|c| c != "serve") {
137+ return;
138+ }
139+ let exe = match std::env::current_exe() {
140+ Ok(exe) => exe,
141+ Err(e) => {
142+ eprintln!("forge: cannot find the executable to set {VAR}: {e}");
143+ return;
144+ }
145+ };
146+ use std::os::unix::process::CommandExt;
147+ // exec only returns on failure; the server then runs on without the variable.
148+ let e = std::process::Command::new(exe).args(raw).env(VAR, "0").exec();
149+ eprintln!("forge: cannot re-run with {VAR}=0: {e}");
150+}
151+
112 fn main() -> ExitCode {152 fn main() -> ExitCode {
113 // The sandbox helpers must start before any runtime or subscriber exists.153 // The sandbox helpers must start before any runtime or subscriber exists.
114 if forge_runner::cli::is_sandbox_invocation() {154 if forge_runner::cli::is_sandbox_invocation() {
@@ -126,6 +166,7 @@ fn main() -> ExitCode {
126 if let Err(e) = nix::sys::prctl::set_thp_disable(true) {166 if let Err(e) = nix::sys::prctl::set_thp_disable(true) {
127 eprintln!("forge: cannot disable transparent huge pages: {e}");167 eprintln!("forge: cannot disable transparent huge pages: {e}");
128 }168 }
169+ reexec_with_allocator_env(&raw);
129 let runtime = match tokio::runtime::Builder::new_multi_thread()170 let runtime = match tokio::runtime::Builder::new_multi_thread()
130 .enable_all()171 .enable_all()
131 .build()172 .build()
deploy/forge.servicemodified+0 -3
@@ -10,9 +10,6 @@ Type=simple
10 User=forge10 User=forge
11 Group=forge11 Group=forge
12 Environment=FORGE_CONFIG=/etc/forge/forge.toml12 Environment=FORGE_CONFIG=/etc/forge/forge.toml
13-# forge turns transparent huge pages off itself at start; this also makes the allocator return
14-# memory in small pieces, which keeps a further ~7% off the resident size.
15-Environment=MIMALLOC_ALLOW_THP=0
16 ExecStart=/usr/local/bin/forge serve13 ExecStart=/usr/local/bin/forge serve
17 WorkingDirectory=/var/lib/forge14 WorkingDirectory=/var/lib/forge
18 Restart=on-failure15 Restart=on-failure