Skip to content

Commit b0df8b6

Browse files
committed
perf(server): expired PR diffs no longer stay in memory until restart
The PR diff cache and its stale-while-revalidate map each hold up to 128 patches of up to 512 KiB, and both dropped an expired patch only when its own key was read again. A diff's key changes with every push, so patches for a closed PR page stayed in the server heap until restart or until 128 newer diffs rotated them out. A value left in either holder now arms one sweep a minute later that drops expired entries from both and re-arms itself while anything is still held, so an idle server keeps no timer. Freshness and stale-serving windows are unchanged.
1 parent c77a7b7 commit b0df8b6

2 files changed

Lines changed: 83 additions & 2 deletions

File tree

‎apps/server/src/pullRequest/PullRequestService.test.ts‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import * as NodeServices from "@effect/platform-node/NodeServices";
2+
import * as NodeV8 from "node:v8";
3+
import * as NodeVM from "node:vm";
24
import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
35
import * as KeyValueStore from "effect/persistence/KeyValueStore";
46
import { assert, it } from "@effect/vitest";
@@ -5971,6 +5973,41 @@ it.effect("caches a small replacement after releasing a large diff", () =>
59715973
}),
59725974
);
59735975

5976+
it.effect("releases an expired diff nobody reads again", () =>
5977+
Effect.gen(function* () {
5978+
NodeV8.setFlagsFromString("--expose-gc");
5979+
const collect = NodeVM.runInNewContext("gc") as () => void;
5980+
const settle = Effect.promise(async () => {
5981+
// A weak reference keeps its target for the rest of the job that created it.
5982+
await new Promise((resolve) => setImmediate(resolve));
5983+
collect();
5984+
});
5985+
const service = yield* makeService({
5986+
projects: [project({ id: "p1", title: "web", workspaceRoot: "/a", repository: "acme/web" })],
5987+
providers: [
5988+
fakeProvider("github", {
5989+
getDiff: () => Effect.sync(() => ({ patch: "@@", truncated: false, nextCursor: null })),
5990+
}),
5991+
],
5992+
});
5993+
const reference = { projectId: "p1" as ProjectId, repository: "acme/web", number: 1 };
5994+
const read = (input: Parameters<typeof service.diff>[0]) =>
5995+
Effect.map(service.diff(input), (result) => new WeakRef(result));
5996+
const page = yield* read(reference);
5997+
const commit = yield* read({ ...reference, commit: "a".repeat(40) });
5998+
5999+
yield* TestClock.adjust("9 minutes");
6000+
yield* settle;
6001+
assert.isDefined(page.deref());
6002+
assert.isDefined(commit.deref());
6003+
6004+
yield* TestClock.adjust("2 minutes");
6005+
yield* settle;
6006+
assert.isUndefined(page.deref());
6007+
assert.isUndefined(commit.deref());
6008+
}),
6009+
);
6010+
59746011
const environmentViewedProvider = (
59756012
revisions: Map<string, string>,
59766013
asked: Array<ReadonlyArray<string>>,

‎apps/server/src/pullRequest/PullRequestService.ts‎

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,8 @@ const LIST_STATS_CACHE_TTL = Duration.seconds(60);
176176
const FILES_VIEWED_CACHE_TTL = Duration.seconds(15);
177177
/** A diff can stay interactive while its next cached value is fetched off the critical path. */
178178
const DIFF_STALE_WINDOW = Duration.minutes(10);
179+
/** How long an expired diff may stay held before it is dropped unread. */
180+
const DIFF_SWEEP_INTERVAL = Duration.minutes(1);
179181
/** How long one host's signed-in login is believed without asking its CLI again. */
180182
const VIEWER_CACHE_TTL = Duration.minutes(10);
181183
const SEARCH_VISIBILITY_TTL = Duration.minutes(10);
@@ -1939,6 +1941,7 @@ export const make = Effect.gen(function* () {
19391941
);
19401942

19411943
const context = yield* Effect.context<never>();
1944+
const scope = yield* Effect.scope;
19421945
/** Runs a refresh as its own fiber, for the reads that answer from a held value first. */
19431946
const runFork = Effect.runForkWith(context);
19441947

@@ -2659,8 +2662,9 @@ export const make = Effect.gen(function* () {
26592662
if (oldest !== undefined) held.delete(oldest);
26602663
}
26612664
held.set(key, { at, value });
2665+
armDiffSweep();
26622666
});
2663-
return <E>(key: string, read: Effect.Effect<PullRequestDiffResult, E>) => {
2667+
const serve = <E>(key: string, read: Effect.Effect<PullRequestDiffResult, E>) => {
26642668
const recorded = read.pipe(Effect.tap((value) => record(key, value)));
26652669
return Effect.flatMap(Clock.currentTimeMillis, (now) => {
26662670
const snapshot = held.get(key);
@@ -2671,6 +2675,14 @@ export const make = Effect.gen(function* () {
26712675
return revalidate(recorded).pipe(Effect.as(snapshot.value));
26722676
});
26732677
};
2678+
/** Drops what `serve` would already refuse, and says how many values are still held. */
2679+
const dropExpired = Effect.map(Clock.currentTimeMillis, (now) => {
2680+
for (const [key, snapshot] of held) {
2681+
if (now - snapshot.at > staleMs) held.delete(key);
2682+
}
2683+
return held.size;
2684+
});
2685+
return { serve, dropExpired };
26742686
})();
26752687

26762688
const makeLastGoodRead = <A>(capacity: number) => {
@@ -3234,6 +3246,36 @@ export const make = Effect.gen(function* () {
32343246
},
32353247
},
32363248
);
3249+
/**
3250+
* Both diff holders drop an expired value only when its own key is read again, and a diff's key
3251+
* moves with every push, so a closed page's patches would stay held until the server restarted.
3252+
* One pending sweep at a time, armed in the same step that leaves a value held and re-armed
3253+
* while anything still is, so an idle server keeps no timer.
3254+
*/
3255+
let diffSweepPending = false;
3256+
// Forked from the service's own context, in one step with the flag: a caller's interruption
3257+
// cannot strand the flag, and its request is not kept alive by the wait.
3258+
const armDiffSweep = () => {
3259+
if (diffSweepPending) return;
3260+
diffSweepPending = true;
3261+
runFork(Effect.forkIn(sweepDiffs, scope));
3262+
};
3263+
const sweepDiffs: Effect.Effect<void> = Effect.sleep(DIFF_SWEEP_INTERVAL).pipe(
3264+
Effect.andThen(
3265+
Effect.suspend(() => {
3266+
// Cleared before the sweep, so a read that settles during it arms the next one itself.
3267+
diffSweepPending = false;
3268+
return staleDiff.dropExpired;
3269+
}),
3270+
),
3271+
// The cache drops its expired entries as its keys are walked.
3272+
Effect.flatMap((stale) =>
3273+
Effect.map(Cache.keys(diffCache), (keys) => stale + Array.from(keys).length),
3274+
),
3275+
Effect.map((held) => {
3276+
if (held > 0) armDiffSweep();
3277+
}),
3278+
);
32373279
const diff: PullRequestService["Service"]["diff"] = (input) => {
32383280
const key = JSON.stringify([
32393281
refCacheKey(input),
@@ -3256,8 +3298,10 @@ export const make = Effect.gen(function* () {
32563298
Effect.uninterruptible,
32573299
),
32583300
),
3301+
// However the read ends, it may have left a value behind in the cache.
3302+
Effect.ensuring(Effect.sync(armDiffSweep)),
32593303
);
3260-
return staleDiff(key, read);
3304+
return staleDiff.serve(key, read);
32613305
};
32623306

32633307
const filesViewedCache = yield* Cache.makeWith(

0 commit comments

Comments
 (0)