Skip to content

Commit 09b7f9b

Browse files
Yuruzuuclaude
andcommitted
perf(server): PR cache invalidation no longer waits on other projects' reads (1,163 ms → 0.12 ms median)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 115739c commit 09b7f9b

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))("PR filesystem ca
4444
const invalidate = yield* cache
4545
.invalidate("pr")
4646
.pipe(Effect.forkChild({ startImmediately: true }));
47+
// Another PR's invalidation does not wait on this read; this one's does.
48+
yield* cache.invalidate("other-pr");
49+
assert.isUndefined(invalidate.pollUnsafe());
4750
yield* Deferred.succeed(release, undefined);
4851
yield* Fiber.join(read);
4952
yield* Fiber.join(invalidate);

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import { detachStackFrame } from "../git/detachStackFrame.ts";
2626
import { forkParked } from "../serverActivation.ts";
2727

2828
const CONCURRENT_READS = 512;
29+
/** Invalidation excludes only reads whose scopes share its stripe, not every PR read. */
30+
const SCOPE_STRIPES = 64;
2931
// Entry filenames are the URI-encoded `<storeId>:<key>`; the scope revisions file has
3032
// no store id. Effect before 4.1 wrote `<storeId><key>`, still pruned here.
3133
const STORE_ID = "pr-v2";
@@ -87,6 +89,11 @@ export const make = Effect.gen(function* () {
8789
const clock = yield* Clock.Clock;
8890
let enabled = true;
8991
const lock = yield* Semaphore.make(CONCURRENT_READS);
92+
const stripes = yield* Effect.forEach(Array.from({ length: SCOPE_STRIPES }), () =>
93+
Semaphore.make(CONCURRENT_READS),
94+
);
95+
const stripeOf = (scope: string) => (Hash.string(scope) >>> 0) % SCOPE_STRIPES;
96+
const revisionWrites = yield* Semaphore.make(1);
9097
const digest = (key: string) =>
9198
digester.digest(Digest.sha256, new TextEncoder().encode(key)).pipe(Effect.map(Hex.encode));
9299
const revisions = yield* Cache.makeWith(
@@ -162,6 +169,12 @@ export const make = Effect.gen(function* () {
162169
PersistenceError: () => read,
163170
SchemaError: () => read,
164171
}),
172+
// A read stores its result under the revisions it started with, so an invalidation of
173+
// one of its scopes must wait for it; reads of other scopes run on.
174+
(effect) =>
175+
[...new Set(scopes.map(stripeOf))]
176+
.toSorted((left, right) => left - right)
177+
.reduceRight((inner, stripe) => stripes[stripe]!.withPermits(1)(inner), effect),
165178
lock.withPermits(1),
166179
);
167180
}, detachStackFrame),
@@ -182,7 +195,8 @@ export const make = Effect.gen(function* () {
182195
return Effect.logWarning("PR cache disabled after clearing failed");
183196
}),
184197
Effect.uninterruptible,
185-
lock.withPermits(CONCURRENT_READS),
198+
revisionWrites.withPermits(1),
199+
(effect, scope) => stripes[stripeOf(scope)]!.withPermits(CONCURRENT_READS)(effect),
186200
detachStackFrame,
187201
),
188202
});

0 commit comments

Comments
 (0)