From 7d8034b3bb8a5d44d986e1031cc1f24d8f604b69 Mon Sep 17 00:00:00 2001 From: Steffen Neubauer Date: Thu, 16 May 2024 00:42:48 +0200 Subject: [PATCH] improvement: improve cli startup performance using v8 cache (#6049) * feat: experimental flag to enable v8 cache This can speed up the core command startup time. Can be enabled by now using `GARDEN_COMPILE_CACHE=1` and is disabled by default. On my M2 mac it saves ~11-30ms when running `garden --help` * improvement: enable v8 cache by default --- garden-sea/src/node.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/garden-sea/src/node.rs b/garden-sea/src/node.rs index 4b85d1a34e..4383aebe62 100644 --- a/garden-sea/src/node.rs +++ b/garden-sea/src/node.rs @@ -39,6 +39,18 @@ where #[cfg(all(target_os = "linux"))] command.env("GARDEN_SEA_TARGET_ENV", TARGET_ENV); + // Enable v8 compilation cache by default. That saves ~10-30ms on an M2 mac and we've seen 2 seconds startup time shaved off on Windows. + // See also https://nodejs.org/api/cli.html#node_compile_cachedir + let enable_compile_cache = env::var("GARDEN_COMPILE_CACHE").unwrap_or("true".into()); + if enable_compile_cache == "true" || enable_compile_cache == "1" { + let cache_dir = path.join("v8cache"); + fs::create_dir_all(cache_dir.clone())?; + command.env( + "NODE_COMPILE_CACHE", + OsString::from(cache_dir), + ); + } + // Allow users to override the heap size if needed. let max_old_space_size = env::var("GARDEN_MAX_OLD_SPACE_SIZE").unwrap_or("4096".into()); let max_semi_space_size = env::var("GARDEN_MAX_SEMI_SPACE_SIZE").unwrap_or("64".into());