Skip to content

Commit d10bd13

Browse files
t3dotggclaude
andauthored
perf(desktop): cache compiled JavaScript between launches (#13501)
Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
1 parent c13f7d9 commit d10bd13

15 files changed

Lines changed: 200 additions & 4 deletions

‎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/main-process-bundle.test.mjs‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as NodeChildProcess from "node:child_process";
12
import * as NodeFSP from "node:fs/promises";
23
import * as NodeOS from "node:os";
34
import * as NodePath from "node:path";
@@ -108,3 +109,65 @@ void import("./linux.ts").then(({ result }) => process.emit("ready", result));`,
108109
await NodeFSP.rm(directory, { recursive: true, force: true });
109110
}
110111
});
112+
113+
it("loads the emitted packaged boot entry and backend cache preload", async () => {
114+
const directory = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "t3-desktop-boot-"));
115+
try {
116+
const entries = ["src/boot.ts", "src/compileCache.ts"];
117+
await NodeFSP.mkdir(NodePath.join(directory, "src"));
118+
await Promise.all(
119+
entries.map((entry) =>
120+
NodeFSP.copyFile(new URL(`../${entry}`, import.meta.url), NodePath.join(directory, entry)),
121+
),
122+
);
123+
assert.ok(Array.isArray(desktopConfig.pack));
124+
for (const packConfig of desktopConfig.pack) {
125+
if (!Array.isArray(packConfig.entry)) continue;
126+
if (!packConfig.entry.some((entry) => entries.includes(entry))) continue;
127+
await build({
128+
...packConfig,
129+
config: false,
130+
cwd: directory,
131+
tsconfig: false,
132+
sourcemap: false,
133+
onSuccess: undefined,
134+
logLevel: "silent",
135+
});
136+
}
137+
const outputDirectory = NodePath.join(directory, "dist-electron");
138+
const fixture = `console.log(require('node:module').getCompileCacheDir() ? 'cached' : 'uncached');`;
139+
await NodeFSP.writeFile(NodePath.join(outputDirectory, "main.cjs"), fixture);
140+
await NodeFSP.writeFile(
141+
NodePath.join(outputDirectory, "backend.mjs"),
142+
`import { getCompileCacheDir } from 'node:module'; console.log(getCompileCacheDir() ? 'cached' : 'uncached');`,
143+
);
144+
for (const disabled of [false, true]) {
145+
for (const args of [
146+
[NodePath.join(outputDirectory, "boot.cjs")],
147+
[
148+
"--require",
149+
NodePath.join(outputDirectory, "compileCache.cjs"),
150+
NodePath.join(outputDirectory, "backend.mjs"),
151+
],
152+
]) {
153+
const child = NodeChildProcess.spawnSync(process.execPath, args, {
154+
encoding: "utf8",
155+
env: {
156+
...process.env,
157+
APPIMAGE: "",
158+
NODE_COMPILE_CACHE: undefined,
159+
NODE_DISABLE_COMPILE_CACHE: disabled ? "1" : undefined,
160+
XDG_CACHE_HOME: directory,
161+
TMPDIR: directory,
162+
TEMP: directory,
163+
TMP: directory,
164+
},
165+
});
166+
assert.equal(child.status, 0, child.stderr);
167+
assert.equal(child.stdout.trim(), disabled ? "uncached" : "cached");
168+
}
169+
}
170+
} finally {
171+
await NodeFSP.rm(directory, { recursive: true, force: true });
172+
}
173+
});

‎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.test.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,11 @@ describe("DesktopBackendConfiguration", () => {
235235
const second = yield* configuration.resolvePrimary;
236236

237237
assert.equal(first.executablePath, process.execPath);
238+
assert.deepEqual(first.args.slice(0, 3), [
239+
"--require",
240+
environment.compileCachePath,
241+
environment.backendEntryPath,
242+
]);
238243
assert.equal(first.entryPath, environment.backendEntryPath);
239244
assert.equal(first.cwd, environment.backendCwd);
240245
assert.equal(first.captureOutput, true);
@@ -927,6 +932,8 @@ describe("DesktopBackendConfiguration", () => {
927932
const configuration = yield* DesktopBackendConfiguration.DesktopBackendConfiguration;
928933
const config = yield* configuration.resolvePrimary;
929934
assert.equal(config.captureOutput, true);
935+
// Dev never shares the prod compile cache.
936+
assert.notInclude(config.args, "--require");
930937
}).pipe(
931938
Effect.provide(
932939
DesktopBackendConfiguration.layer.pipe(

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

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

551551
return {
552552
executablePath: process.execPath,
553-
args: [environment.backendEntryPath, "--bootstrap-fd", "3"],
553+
// Packaged builds only, so a dev instance never shares the cache with the
554+
// prod app it is often run from. `--require` rather than NODE_COMPILE_CACHE,
555+
// so the setting does not leak into the provider and terminal processes
556+
// the backend starts.
557+
args: [
558+
...(environment.isPackaged ? ["--require", environment.compileCachePath] : []),
559+
environment.backendEntryPath,
560+
"--bootstrap-fd",
561+
"3",
562+
],
554563
entryPath: environment.backendEntryPath,
555564
cwd: environment.backendCwd,
556565
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: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
// Packaged builds only: boot.ts loads it for the main process, and the local
10+
// backend gets it with `--require`. Dev launches main.cjs directly and skips it.
11+
// Linux uses the user's cache dir because /tmp is shared between users; the
12+
// macOS and Windows temp dirs are already per user.
13+
//
14+
// Skipped under AppImage: it mounts the app at a new /tmp/.mount_* path each
15+
// launch, and Node keys entries by path, so every launch would miss and leave
16+
// another copy behind. The backend inherits APPIMAGE, so this covers it too.
17+
try {
18+
if (!process.env.APPIMAGE) {
19+
const cacheRoot =
20+
// oxlint-disable-next-line t3code/no-global-process-runtime -- Loads before any Effect runtime.
21+
process.platform === "linux"
22+
? process.env.XDG_CACHE_HOME || NodePath.join(NodeOS.homedir(), ".cache")
23+
: NodeOS.tmpdir();
24+
NodeModule.enableCompileCache(NodePath.join(cacheRoot, "t3code", "compile-cache"));
25+
}
26+
} catch {
27+
// The cache is only a speedup. Never let it stop the app from starting.
28+
}

‎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",

0 commit comments

Comments
 (0)