mirror of
https://github.com/Youzini-afk/ST-Bionic-Memory-Ecology.git
synced 2026-06-14 02:40:45 +08:00
perf: optimize persist/load P1 hot paths
This commit is contained in:
@@ -3239,6 +3239,166 @@ result = {
|
||||
);
|
||||
}
|
||||
|
||||
{
|
||||
const chatId = "chat-idb-direct-delta-prebuilt-persist-snapshot";
|
||||
const baseGraph = createMeaningfulGraph(chatId, "direct-delta-base");
|
||||
const runtimeGraph = createMeaningfulGraph(chatId, "direct-delta-after");
|
||||
const baseSnapshot = buildSnapshotFromGraph(baseGraph, {
|
||||
chatId,
|
||||
revision: 7,
|
||||
});
|
||||
const persistSnapshot = buildSnapshotFromGraph(runtimeGraph, {
|
||||
chatId,
|
||||
revision: 8,
|
||||
baseSnapshot,
|
||||
});
|
||||
const directDelta = buildPersistDelta(baseSnapshot, persistSnapshot, {
|
||||
useNativeDelta: false,
|
||||
});
|
||||
const harness = await createGraphPersistenceHarness({
|
||||
chatId,
|
||||
globalChatId: chatId,
|
||||
chatMetadata: {
|
||||
integrity: "meta-idb-direct-delta-prebuilt-persist-snapshot",
|
||||
},
|
||||
indexedDbSnapshot: baseSnapshot,
|
||||
});
|
||||
harness.api.setCurrentGraph(runtimeGraph);
|
||||
harness.api.setGraphPersistenceState({
|
||||
loadState: "loaded",
|
||||
chatId,
|
||||
revision: 8,
|
||||
lastPersistedRevision: 0,
|
||||
writesBlocked: false,
|
||||
});
|
||||
|
||||
const originalBuildSnapshotFromGraph = harness.runtimeContext.buildSnapshotFromGraph;
|
||||
let buildSnapshotCallCount = 0;
|
||||
harness.runtimeContext.buildSnapshotFromGraph = (...args) => {
|
||||
buildSnapshotCallCount += 1;
|
||||
return originalBuildSnapshotFromGraph(...args);
|
||||
};
|
||||
|
||||
const result = await harness.api.saveGraphToIndexedDb(chatId, runtimeGraph, {
|
||||
revision: 8,
|
||||
reason: "direct-delta-prebuilt-persist-snapshot-save",
|
||||
scheduleCloudUpload: false,
|
||||
persistDelta: directDelta,
|
||||
persistSnapshot,
|
||||
});
|
||||
|
||||
assert.equal(result.saved, true);
|
||||
assert.equal(
|
||||
buildSnapshotCallCount,
|
||||
0,
|
||||
"direct-delta 且已提供 persistSnapshot 时不应再次构建 snapshot",
|
||||
);
|
||||
assert.equal(result.snapshot?.meta?.revision, 8);
|
||||
assert.equal(harness.api.getIndexedDbSnapshot()?.meta?.revision, 8);
|
||||
}
|
||||
|
||||
{
|
||||
const chatId = "chat-indexeddb-probe-empty-early-return";
|
||||
const persistedSnapshot = {
|
||||
meta: { revision: 0, chatId },
|
||||
nodes: [],
|
||||
edges: [],
|
||||
tombstones: [],
|
||||
state: {
|
||||
lastProcessedFloor: -1,
|
||||
extractionCount: 0,
|
||||
},
|
||||
};
|
||||
const harness = await createGraphPersistenceHarness({
|
||||
chatId,
|
||||
globalChatId: chatId,
|
||||
chatMetadata: {
|
||||
integrity: "meta-indexeddb-probe-empty-early-return",
|
||||
},
|
||||
indexedDbSnapshot: persistedSnapshot,
|
||||
});
|
||||
harness.runtimeContext.__globalChatId = chatId;
|
||||
harness.runtimeContext.__chatContext.chatId = chatId;
|
||||
harness.api.setChatContext({
|
||||
...harness.api.getChatContext(),
|
||||
chatId,
|
||||
chatMetadata: {
|
||||
integrity: "meta-indexeddb-probe-empty-early-return",
|
||||
},
|
||||
});
|
||||
harness.api.setCurrentGraph(
|
||||
createMeaningfulGraph(chatId, "probe-empty-runtime-current"),
|
||||
);
|
||||
harness.api.setGraphPersistenceState({
|
||||
loadState: "loaded",
|
||||
chatId,
|
||||
revision: 1,
|
||||
lastPersistedRevision: 1,
|
||||
storagePrimary: "indexeddb",
|
||||
storageMode: "indexeddb",
|
||||
writesBlocked: false,
|
||||
});
|
||||
|
||||
const originalCreateDb = harness.runtimeContext.BmeChatManager.prototype._createDb;
|
||||
let exportSnapshotCalls = 0;
|
||||
let exportProbeCalls = 0;
|
||||
harness.runtimeContext.BmeChatManager.prototype._createDb = function(dbChatId = "") {
|
||||
const baseDb = originalCreateDb.call(this, dbChatId);
|
||||
return {
|
||||
...baseDb,
|
||||
async exportSnapshot() {
|
||||
exportSnapshotCalls += 1;
|
||||
return await baseDb.exportSnapshot();
|
||||
},
|
||||
async exportSnapshotProbe() {
|
||||
exportProbeCalls += 1;
|
||||
const snapshot = harness.api.getIndexedDbSnapshotForChat(dbChatId) || {
|
||||
meta: { revision: 0, chatId: String(dbChatId || "") },
|
||||
state: { lastProcessedFloor: -1, extractionCount: 0 },
|
||||
nodes: [],
|
||||
edges: [],
|
||||
tombstones: [],
|
||||
};
|
||||
return {
|
||||
meta: {
|
||||
...(snapshot.meta || {}),
|
||||
chatId: String(dbChatId || ""),
|
||||
revision: Number(snapshot?.meta?.revision || 0),
|
||||
nodeCount: Array.isArray(snapshot?.nodes) ? snapshot.nodes.length : 0,
|
||||
edgeCount: Array.isArray(snapshot?.edges) ? snapshot.edges.length : 0,
|
||||
tombstoneCount: Array.isArray(snapshot?.tombstones)
|
||||
? snapshot.tombstones.length
|
||||
: 0,
|
||||
},
|
||||
state: {
|
||||
lastProcessedFloor: Number(snapshot?.state?.lastProcessedFloor ?? -1),
|
||||
extractionCount: Number(snapshot?.state?.extractionCount ?? 0),
|
||||
},
|
||||
nodes: [],
|
||||
edges: [],
|
||||
tombstones: [],
|
||||
__stBmeProbeOnly: true,
|
||||
__stBmeTombstonesOmitted: true,
|
||||
};
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
const result = await harness.api.loadGraphFromIndexedDb(chatId, {
|
||||
source: "probe-empty-early-return",
|
||||
attemptIndex: 0,
|
||||
});
|
||||
|
||||
assert.equal(result.loaded, false);
|
||||
assert.equal(exportProbeCalls, 1);
|
||||
assert.equal(
|
||||
exportSnapshotCalls,
|
||||
0,
|
||||
"empty/probe 早退应在 probe 阶段终止,而不是继续全量导出 snapshot",
|
||||
);
|
||||
harness.runtimeContext.BmeChatManager.prototype._createDb = originalCreateDb;
|
||||
}
|
||||
|
||||
{
|
||||
const harness = await createGraphPersistenceHarness({
|
||||
chatId: "chat-pending-persist-retry",
|
||||
|
||||
Reference in New Issue
Block a user