perf(snapshot): avoid redundant result copies and serialization
This commit is contained in:
@@ -13,6 +13,7 @@ import { createAnalysisExecution } from "../AnalysisProvider.js";
|
||||
import {
|
||||
createAnalysisSnapshotWorkflowEntry,
|
||||
parseAnalysisSnapshot,
|
||||
serializeAnalysisSnapshot,
|
||||
snapshotBinding,
|
||||
snapshotTarget,
|
||||
} from "../../domain/analysisSnapshot.js";
|
||||
@@ -30,6 +31,89 @@ import {
|
||||
isSnapshotCacheable,
|
||||
} from "./AnalysisSnapshotCache.js";
|
||||
|
||||
it("exports independent nested query, workflow and Evidence payloads", () => {
|
||||
const cache = new AnalysisSnapshotCache();
|
||||
const target = ANALYSIS_SNAPSHOT_TARGET;
|
||||
const profile = ANALYSIS_SNAPSHOT_PROFILE;
|
||||
const result = { nested: [{ text: "ret" }], name: "main" };
|
||||
const parameters = { address: "0x1000", document: "fixture" };
|
||||
const workflowProfile = workflowAnalysisProfile(profile);
|
||||
cache.record({
|
||||
target,
|
||||
profile,
|
||||
operation: "procedure_info",
|
||||
parameters,
|
||||
execution: createAnalysisExecution(result, ANALYSIS_SNAPSHOT_PROVIDER, {
|
||||
subject: target,
|
||||
analysisProfile: profile,
|
||||
rawResult: result,
|
||||
}),
|
||||
});
|
||||
cache.recordWorkflow({
|
||||
target,
|
||||
profile,
|
||||
operation: "analyze_function",
|
||||
parameters,
|
||||
execution: {
|
||||
...createAnalysisExecution(result, REA_WORKFLOW_PROVIDER, {
|
||||
subject: target,
|
||||
rawResult: result,
|
||||
}),
|
||||
analysisProfile: workflowProfile,
|
||||
},
|
||||
});
|
||||
const bundle = createEvidenceBundle([
|
||||
createEvidence(target, ANALYSIS_SNAPSHOT_PROVIDER, {
|
||||
operation: "procedure_info",
|
||||
parameters,
|
||||
result,
|
||||
rawResult: result,
|
||||
analysisProfile: profile,
|
||||
}),
|
||||
createEvidence(target, REA_WORKFLOW_PROVIDER, {
|
||||
operation: "analyze_function",
|
||||
parameters,
|
||||
result,
|
||||
rawResult: result,
|
||||
analysisProfile: workflowProfile,
|
||||
}),
|
||||
]);
|
||||
const exported = cache.export(target, profile, bundle);
|
||||
if (!exported.ok) throw exported.error;
|
||||
const expected = serializeAnalysisSnapshot(exported.value);
|
||||
const changed = JSON.stringify(exported.value).replaceAll(
|
||||
'"ret"',
|
||||
'"changed"',
|
||||
);
|
||||
// Mutate the returned graph itself, including every duplicated JSON result.
|
||||
for (const payload of [
|
||||
...exported.value.entries.flatMap(({ execution }) => [
|
||||
execution.result,
|
||||
execution.raw_result,
|
||||
]),
|
||||
...(exported.value.workflow_entries ?? []).flatMap(({ execution }) => [
|
||||
execution.result,
|
||||
execution.raw_result,
|
||||
]),
|
||||
...exported.value.evidence_bundle.records.flatMap((record) => [
|
||||
record.normalized_result,
|
||||
record.raw_result,
|
||||
]),
|
||||
]) {
|
||||
if (
|
||||
typeof payload !== "object" ||
|
||||
payload === null ||
|
||||
Array.isArray(payload)
|
||||
)
|
||||
throw new Error("Expected nested fixture result");
|
||||
Reflect.set(payload, "nested", [{ text: "changed" }]);
|
||||
}
|
||||
expect(JSON.stringify(exported.value)).toBe(changed);
|
||||
const fresh = cache.export(target, profile, bundle);
|
||||
if (!fresh.ok) throw fresh.error;
|
||||
expect(serializeAnalysisSnapshot(fresh.value)).toBe(expected);
|
||||
});
|
||||
|
||||
describe("analysis snapshot cache capacity", () => {
|
||||
it("shares capacity across query kinds while allowing replacements and fresh partitions", () => {
|
||||
const cache = new AnalysisSnapshotCache();
|
||||
|
||||
@@ -143,16 +143,21 @@ export class AnalysisSnapshotCache {
|
||||
try {
|
||||
const snapshotTargetIdentity = snapshotTarget(target);
|
||||
const binding = snapshotBinding(profile);
|
||||
const retainedEvidence = structuredClone(evidenceBundle);
|
||||
const entries = [...this.#entries.values()].sort((left, right) =>
|
||||
left.query_id.localeCompare(right.query_id),
|
||||
);
|
||||
const workflows = [...this.#workflowEntries.values()].sort(
|
||||
(left, right) => left.query_id.localeCompare(right.query_id),
|
||||
);
|
||||
// Parsing owns the returned JSON and metadata; pre-cloning the same
|
||||
// payloads here only adds another full materialization.
|
||||
return ok(
|
||||
parseAnalysisSnapshot({
|
||||
target: snapshotTargetIdentity,
|
||||
binding,
|
||||
entries: this.entries(),
|
||||
...(this.workflowEntries().length === 0
|
||||
? {}
|
||||
: { workflow_entries: this.workflowEntries() }),
|
||||
evidence_bundle: retainedEvidence,
|
||||
entries,
|
||||
...(workflows.length === 0 ? {} : { workflow_entries: workflows }),
|
||||
evidence_bundle: evidenceBundle,
|
||||
}),
|
||||
);
|
||||
} catch (cause: unknown) {
|
||||
|
||||
Reference in New Issue
Block a user