diff options
| author | Adam <[email protected]> | 2026-03-12 11:31:52 -0500 |
|---|---|---|
| committer | GitHub <[email protected]> | 2026-03-12 11:31:52 -0500 |
| commit | 0e077f748352df6d44c811829baff3c26b3436ac (patch) | |
| tree | 1239dd7f492a528037c92ef695b31ef90b5c86cc /packages/app/src/context/global-sync | |
| parent | 776e7a9c15f3e352c5abf0b0949a5d7b767adfa3 (diff) | |
| download | opencode-0e077f748352df6d44c811829baff3c26b3436ac.tar.gz opencode-0e077f748352df6d44c811829baff3c26b3436ac.zip | |
feat: session load perf (#17186)
Diffstat (limited to 'packages/app/src/context/global-sync')
| -rw-r--r-- | packages/app/src/context/global-sync/session-prefetch.test.ts | 63 | ||||
| -rw-r--r-- | packages/app/src/context/global-sync/session-prefetch.ts | 85 |
2 files changed, 148 insertions, 0 deletions
diff --git a/packages/app/src/context/global-sync/session-prefetch.test.ts b/packages/app/src/context/global-sync/session-prefetch.test.ts new file mode 100644 index 000000000..f039b02ca --- /dev/null +++ b/packages/app/src/context/global-sync/session-prefetch.test.ts @@ -0,0 +1,63 @@ +import { describe, expect, test } from "bun:test" +import { + clearSessionPrefetch, + clearSessionPrefetchDirectory, + getSessionPrefetch, + runSessionPrefetch, + setSessionPrefetch, +} from "./session-prefetch" + +describe("session prefetch", () => { + test("stores and clears message metadata by directory", () => { + clearSessionPrefetch("/tmp/a", ["ses_1"]) + clearSessionPrefetch("/tmp/b", ["ses_1"]) + + setSessionPrefetch({ + directory: "/tmp/a", + sessionID: "ses_1", + limit: 200, + complete: false, + at: 123, + }) + + expect(getSessionPrefetch("/tmp/a", "ses_1")).toEqual({ limit: 200, complete: false, at: 123 }) + expect(getSessionPrefetch("/tmp/b", "ses_1")).toBeUndefined() + + clearSessionPrefetch("/tmp/a", ["ses_1"]) + + expect(getSessionPrefetch("/tmp/a", "ses_1")).toBeUndefined() + }) + + test("dedupes inflight work", async () => { + clearSessionPrefetch("/tmp/c", ["ses_2"]) + + let calls = 0 + const run = () => + runSessionPrefetch({ + directory: "/tmp/c", + sessionID: "ses_2", + task: async () => { + calls += 1 + return { limit: 100, complete: true, at: 456 } + }, + }) + + const [a, b] = await Promise.all([run(), run()]) + + expect(calls).toBe(1) + expect(a).toEqual({ limit: 100, complete: true, at: 456 }) + expect(b).toEqual({ limit: 100, complete: true, at: 456 }) + }) + + test("clears a whole directory", () => { + setSessionPrefetch({ directory: "/tmp/d", sessionID: "ses_1", limit: 10, complete: true, at: 1 }) + setSessionPrefetch({ directory: "/tmp/d", sessionID: "ses_2", limit: 20, complete: false, at: 2 }) + setSessionPrefetch({ directory: "/tmp/e", sessionID: "ses_1", limit: 30, complete: true, at: 3 }) + + clearSessionPrefetchDirectory("/tmp/d") + + expect(getSessionPrefetch("/tmp/d", "ses_1")).toBeUndefined() + expect(getSessionPrefetch("/tmp/d", "ses_2")).toBeUndefined() + expect(getSessionPrefetch("/tmp/e", "ses_1")).toEqual({ limit: 30, complete: true, at: 3 }) + }) +}) diff --git a/packages/app/src/context/global-sync/session-prefetch.ts b/packages/app/src/context/global-sync/session-prefetch.ts new file mode 100644 index 000000000..10877b063 --- /dev/null +++ b/packages/app/src/context/global-sync/session-prefetch.ts @@ -0,0 +1,85 @@ +const key = (directory: string, sessionID: string) => `${directory}\n${sessionID}` + +export const SESSION_PREFETCH_TTL = 15_000 + +type Meta = { + limit: number + complete: boolean + at: number +} + +const cache = new Map<string, Meta>() +const inflight = new Map<string, Promise<Meta | undefined>>() +const rev = new Map<string, number>() + +const version = (id: string) => rev.get(id) ?? 0 + +export function getSessionPrefetch(directory: string, sessionID: string) { + return cache.get(key(directory, sessionID)) +} + +export function getSessionPrefetchPromise(directory: string, sessionID: string) { + return inflight.get(key(directory, sessionID)) +} + +export function clearSessionPrefetchInflight() { + inflight.clear() +} + +export function isSessionPrefetchCurrent(directory: string, sessionID: string, value: number) { + return version(key(directory, sessionID)) === value +} + +export function runSessionPrefetch(input: { + directory: string + sessionID: string + task: (value: number) => Promise<Meta | undefined> +}) { + const id = key(input.directory, input.sessionID) + const pending = inflight.get(id) + if (pending) return pending + + const value = version(id) + + const promise = input.task(value).finally(() => { + if (inflight.get(id) === promise) inflight.delete(id) + }) + + inflight.set(id, promise) + return promise +} + +export function setSessionPrefetch(input: { + directory: string + sessionID: string + limit: number + complete: boolean + at?: number +}) { + cache.set(key(input.directory, input.sessionID), { + limit: input.limit, + complete: input.complete, + at: input.at ?? Date.now(), + }) +} + +export function clearSessionPrefetch(directory: string, sessionIDs: Iterable<string>) { + for (const sessionID of sessionIDs) { + if (!sessionID) continue + const id = key(directory, sessionID) + rev.set(id, version(id) + 1) + cache.delete(id) + inflight.delete(id) + } +} + +export function clearSessionPrefetchDirectory(directory: string) { + const prefix = `${directory}\n` + const keys = new Set([...cache.keys(), ...inflight.keys()]) + for (const id of keys) { + if (!id.startsWith(prefix)) continue + rev.set(id, version(id) + 1) + cache.delete(id) + inflight.delete(id) + } +} |
