Skip to content

Commit 666c40b

Browse files
t3dotggclaude
andcommitted
perf(desktop): cache compiled JS so launches skip recompiling
Enable Node's V8 compile cache for the desktop main process (via a small boot.cjs entry) and the local backend (via --require), and turn on Chromium's code cache for the production t3code:// scheme. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent d5d4874 commit 666c40b

9 files changed

Lines changed: 52 additions & 4 deletions

File tree

‎apps/desktop/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "0.0.42",
44
"private": true,
55
"type": "module",
6-
"main": "dist-electron/main.cjs",
6+
"main": "dist-electron/boot.cjs",
77
"scripts": {
88
"ensure:electron": "node scripts/ensure-electron-runtime.mjs",
99
"start": "node scripts/start-electron.mjs",

‎apps/desktop/scripts/smoke-test.mjs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { resolveElectronLaunchCommand } from "./electron-launcher.mjs";
55

66
const __dirname = NodePath.dirname(NodeURL.fileURLToPath(import.meta.url));
77
const desktopDir = NodePath.resolve(__dirname, "..");
8-
const mainJs = NodePath.resolve(desktopDir, "dist-electron/main.cjs");
8+
const mainJs = NodePath.resolve(desktopDir, "dist-electron/boot.cjs");
99

1010
console.log("\nLaunching Electron smoke test...");
1111

‎apps/desktop/src/app/DesktopEnvironment.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ export class DesktopEnvironment extends Context.Service<
6666
readonly clientAssetsDir: string;
6767
readonly backendCwd: string;
6868
readonly preloadPath: string;
69+
// Preload that turns on the V8 compile cache for the local backend.
70+
readonly compileCachePath: string;
6971
readonly appUpdateYmlPath: string;
7072
readonly devServerUrl: Option.Option<URL>;
7173
readonly devRemoteT3ServerEntryPath: Option.Option<string>;
@@ -221,6 +223,7 @@ const make = Effect.fn("desktop.environment.make")(function* (
221223
clientAssetsDir: path.join(serverRoot, "apps/server/dist/client"),
222224
backendCwd: input.isPackaged ? homeDirectory : appRoot,
223225
preloadPath: path.join(input.dirname, "preload.cjs"),
226+
compileCachePath: path.join(input.dirname, "compileCache.cjs"),
224227
appUpdateYmlPath: input.isPackaged
225228
? path.join(resourcesPath, "app-update.yml")
226229
: path.join(input.appPath, "dev-app-update.yml"),

‎apps/desktop/src/backend/DesktopBackendConfiguration.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,15 @@ const resolvePrimaryStartConfig = Effect.fn("desktop.backendConfiguration.resolv
550550

551551
return {
552552
executablePath: process.execPath,
553-
args: [environment.backendEntryPath, "--bootstrap-fd", "3"],
553+
// `--require` rather than NODE_COMPILE_CACHE, so the cache setting does not
554+
// leak into the provider and terminal processes the backend starts.
555+
args: [
556+
"--require",
557+
environment.compileCachePath,
558+
environment.backendEntryPath,
559+
"--bootstrap-fd",
560+
"3",
561+
],
554562
entryPath: environment.backendEntryPath,
555563
cwd: environment.backendCwd,
556564
env: {

‎apps/desktop/src/boot.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Packaged app entry. Enables the compile cache before the main bundle loads,
2+
// so the cache also covers main.cjs itself.
3+
require("./compileCache.cjs");
4+
require("./main.cjs");

‎apps/desktop/src/compileCache.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// @effect-diagnostics nodeBuiltinImport:off
2+
// Runs before any Effect runtime exists, so it stays on Node built-ins.
3+
import * as NodeModule from "node:module";
4+
import * as NodeOS from "node:os";
5+
import * as NodePath from "node:path";
6+
7+
// Turns on Node's on-disk V8 code cache for every module loaded after this one,
8+
// so later launches skip recompiling the large main and server bundles.
9+
// boot.ts loads it for the main process, and the local backend gets it with
10+
// `--require`. Linux uses the user's cache dir because /tmp is shared between
11+
// users; the macOS and Windows temp dirs are already per user.
12+
const cacheRoot =
13+
// oxlint-disable-next-line t3code/no-global-process-runtime -- Loads before any Effect runtime.
14+
process.platform === "linux"
15+
? process.env.XDG_CACHE_HOME || NodePath.join(NodeOS.homedir(), ".cache")
16+
: NodeOS.tmpdir();
17+
NodeModule.enableCompileCache(NodePath.join(cacheRoot, "t3code", "compile-cache"));

‎apps/desktop/src/electron/ElectronProtocol.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ function registerDesktopSchemePrivilegesSync(): void {
126126
supportFetchAPI: true,
127127
corsEnabled: true,
128128
stream: true,
129+
// Custom schemes skip Chromium's V8 code cache unless they opt in.
130+
// Dev stays off: Vite serves changing code at stable URLs.
131+
codeCache: true,
129132
},
130133
},
131134
{

‎apps/desktop/vite.config.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,19 @@ export default defineConfig({
8585
onlyBundle: false,
8686
},
8787
},
88+
{
89+
// boot.cjs requires the other two at runtime, so all three stay separate files.
90+
format: "cjs",
91+
outDir: "dist-electron",
92+
dts: false,
93+
sourcemap: true,
94+
outExtensions: () => ({ js: ".cjs" }),
95+
entry: ["src/boot.ts", "src/compileCache.ts"],
96+
clean: false,
97+
deps: {
98+
neverBundle: (id) => id === "./main.cjs" || id === "./compileCache.cjs",
99+
},
100+
},
88101
{
89102
format: "cjs",
90103
outDir: "dist-electron",

‎scripts/build-desktop-artifact.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3643,7 +3643,7 @@ const buildDesktopArtifact = Effect.fn("buildDesktopArtifact")(function* (
36433643
packageManager: rootPackageJson.packageManager,
36443644
description: "T3 Code desktop build",
36453645
author: "T3 Tools",
3646-
main: "apps/desktop/dist-electron/main.cjs",
3646+
main: "apps/desktop/dist-electron/boot.cjs",
36473647
build: yield* createBuildConfig(
36483648
options.platform,
36493649
options.target,

0 commit comments

Comments
 (0)