Avoid redundant cloning on fresh session store loads#58965
Avoid redundant cloning on fresh session store loads#58965efww wants to merge 4 commits intoopenclaw:mainfrom
Conversation
Greptile SummaryThis PR eliminates a redundant
Confidence Score: 5/5Safe to merge — the optimization is logically sound, no shared object aliases are possible on the skipCache path, and the new regression test covers the key invariant. The invariant is solid: No files require special attention. Reviews (1): Last reviewed commit: "Avoid redundant cloning on fresh session..." | Re-trigger Greptile |
ef4f1a7 to
1c6a659
Compare
|
The remaining CI failures are pre-existing issues unrelated to this PR:
The original |
e27fb48 to
d2848c1
Compare
005aba8 to
e64d577
Compare
|
Superseded by #60595. Closing this older broader variant in favor of the latest clean 2-file session-store PR on top of the latest |
Summary
loadSessionStore(..., { skipCache: true })already forces a fresh disk read, but it still returnsstructuredClone(store)afterward.On this path, the parsed store is not shared with the cache, so the extra clone is unnecessary. This change returns the parsed store directly when
skipCacheis true, while preserving the defensive clone behavior for normal cached reads.Why
Session store update paths currently re-read the full store from disk with
skipCache: true, mutate one entry, and then write the full JSON back out. The full rewrite is still the dominant cost, but the extra clone adds avoidable O(n) overhead on the read side of that hot path.Benchmarks
Median local microbench results:
loadSessionStore(skipCache)2.735ms -> 1.502ms(-45%)17.209ms -> 6.561ms(-62%)35.020ms -> 13.652ms(-61%)single-entry update path
41.628ms -> 35.904ms(-14%)76.008ms -> 63.889ms(-16%)Tests
pnpm test -- src/config/sessions.test.tspnpm test -- src/commands/agent/session-store.test.tspnpm test -- src/gateway/server-chat.agent-events.test.tsAlso added a regression test covering the
skipCachepath to ensure the returned mutable object does not taint cached reads.Note
I also re-ran
src/gateway/openresponses-http.test.tsand reproduced two existing failures onorigin/main. I am tracking that separately in #58964.