mirror of
https://github.com/morluto/rea.git
synced 2026-11-23 06:55:55 +00:00
perf(server): memoize tools/list schemas and index trace views (#13618)
* perf(server/views): memoize output JSON Schema projection on tools/list and index Playwright trace views * perf(trace): build indexes for requested selectors only
This commit is contained in:
1 parent
b3aaa3da52
commit
cd7db43895
1 file changed
+192
-52
@@ -31,6 +31,7 @@ import {
|
||||
completeWithinViewCoverage,
|
||||
pageViewCoverage,
|
||||
} from "./analysisViewCoverage.js";
|
||||
import { isImmutableJsonSnapshot } from "../immutableJson.js";
|
||||
import { selectViewRows } from "./selectViewRows.js";
|
||||
import { analysisViewInputError } from "./analysisViewErrors.js";
|
||||
|
||||
@@ -153,6 +154,79 @@ const eventFields = (
|
||||
};
|
||||
};
|
||||
|
||||
type TraceRowIndex = ReadonlyMap<string, readonly number[]>;
|
||||
const traceIndexes = new WeakMap<
|
||||
readonly object[],
|
||||
Map<string, TraceRowIndex>
|
||||
>();
|
||||
|
||||
const candidateIndices = <Row extends object>(
|
||||
report: PlaywrightTrace,
|
||||
rows: readonly Row[],
|
||||
key: keyof Row & string,
|
||||
filter: ReadonlySet<string>,
|
||||
): number[] | undefined => {
|
||||
if (!isImmutableJsonSnapshot(report)) return undefined;
|
||||
if (filter.size === 0) return [];
|
||||
let indexes = traceIndexes.get(rows);
|
||||
if (indexes === undefined) {
|
||||
indexes = new Map();
|
||||
traceIndexes.set(rows, indexes);
|
||||
}
|
||||
let index = indexes.get(key);
|
||||
if (index === undefined) {
|
||||
const built = new Map<string, number[]>();
|
||||
for (let i = 0; i < rows.length; i += 1) {
|
||||
const value = rows[i]?.[key];
|
||||
if (typeof value !== "string") continue;
|
||||
const bucket = built.get(value);
|
||||
if (bucket === undefined) built.set(value, [i]);
|
||||
else bucket.push(i);
|
||||
}
|
||||
index = built;
|
||||
indexes.set(key, index);
|
||||
}
|
||||
return collectCandidateIndices(index, filter);
|
||||
};
|
||||
|
||||
const collectCandidateIndices = (
|
||||
indexMap: ReadonlyMap<string, readonly number[]>,
|
||||
filter: ReadonlySet<string>,
|
||||
): number[] => {
|
||||
if (filter.size === 1) {
|
||||
const only = filter.values().next().value;
|
||||
const list = only === undefined ? undefined : indexMap.get(only);
|
||||
return list === undefined ? [] : [...list];
|
||||
}
|
||||
const seen = new Set<number>();
|
||||
for (const key of filter) {
|
||||
const list = indexMap.get(key);
|
||||
if (list === undefined) continue;
|
||||
for (let i = 0; i < list.length; i += 1) seen.add(list[i]!);
|
||||
}
|
||||
return [...seen].sort((left, right) => left - right);
|
||||
};
|
||||
|
||||
const selectCandidateRows = <Source, Row>(
|
||||
rows: readonly Source[],
|
||||
candidateIndices: readonly number[],
|
||||
view: { readonly offset: number; readonly limit: number },
|
||||
matches: (row: Source, index: number) => boolean,
|
||||
project: (row: Source, index: number) => Row,
|
||||
): { rows: Row[]; total: number } => {
|
||||
const selected: Row[] = [];
|
||||
let total = 0;
|
||||
for (let i = 0; i < candidateIndices.length; i += 1) {
|
||||
const index = candidateIndices[i]!;
|
||||
const row = rows[index]!;
|
||||
if (!matches(row, index)) continue;
|
||||
if (total >= view.offset && selected.length < view.limit)
|
||||
selected.push(project(row, index));
|
||||
total++;
|
||||
}
|
||||
return { rows: selected, total };
|
||||
};
|
||||
|
||||
const selectRows = (
|
||||
report: PlaywrightTrace,
|
||||
view: Exclude<View, { collection: "event-text" }>,
|
||||
@@ -162,70 +236,136 @@ const selectRows = (
|
||||
streams = values(view.stream_ids),
|
||||
kinds = values(view.kinds),
|
||||
calls = values(view.call_ids);
|
||||
return selectViewRows(
|
||||
report.events,
|
||||
view,
|
||||
(row) =>
|
||||
allows(ids, row.event_id) &&
|
||||
allows(streams, row.stream_id) &&
|
||||
allows(kinds, row.kind) &&
|
||||
allows(calls, row.call_id),
|
||||
(row) => eventFields(row, view),
|
||||
);
|
||||
const matchEvent = (row: PlaywrightTrace["events"][number]): boolean =>
|
||||
allows(ids, row.event_id) &&
|
||||
allows(streams, row.stream_id) &&
|
||||
allows(kinds, row.kind) &&
|
||||
allows(calls, row.call_id);
|
||||
const projectEvent = (row: PlaywrightTrace["events"][number]) =>
|
||||
eventFields(row, view);
|
||||
if (
|
||||
ids !== undefined ||
|
||||
streams !== undefined ||
|
||||
kinds !== undefined ||
|
||||
calls !== undefined
|
||||
) {
|
||||
const candidates =
|
||||
ids !== undefined
|
||||
? candidateIndices(report, report.events, "event_id", ids)
|
||||
: calls !== undefined
|
||||
? candidateIndices(report, report.events, "call_id", calls)
|
||||
: streams !== undefined
|
||||
? candidateIndices(report, report.events, "stream_id", streams)
|
||||
: candidateIndices(report, report.events, "kind", kinds!);
|
||||
if (candidates !== undefined) {
|
||||
return selectCandidateRows(
|
||||
report.events,
|
||||
candidates,
|
||||
view,
|
||||
matchEvent,
|
||||
projectEvent,
|
||||
);
|
||||
}
|
||||
}
|
||||
return selectViewRows(report.events, view, matchEvent, projectEvent);
|
||||
}
|
||||
if (view.collection === "members") {
|
||||
const ids = values(view.member_ids),
|
||||
paths = values(view.paths);
|
||||
return selectViewRows(
|
||||
report.members,
|
||||
view,
|
||||
(row) => allows(ids, row.member_id) && allows(paths, row.path),
|
||||
(row) => ({
|
||||
...fields(
|
||||
row,
|
||||
view.fields ?? ["declared_bytes", "declared_compressed_bytes"],
|
||||
),
|
||||
member_id: row.member_id,
|
||||
path: row.path,
|
||||
logical_path: row.logical_path,
|
||||
kind: row.kind,
|
||||
encrypted: row.encrypted,
|
||||
decoded: row.decoded,
|
||||
limitations: row.limitations,
|
||||
}),
|
||||
);
|
||||
const matchMember = (row: PlaywrightTrace["members"][number]): boolean =>
|
||||
allows(ids, row.member_id) && allows(paths, row.path);
|
||||
const projectMember = (row: PlaywrightTrace["members"][number]) => ({
|
||||
...fields(
|
||||
row,
|
||||
view.fields ?? ["declared_bytes", "declared_compressed_bytes"],
|
||||
),
|
||||
member_id: row.member_id,
|
||||
path: row.path,
|
||||
logical_path: row.logical_path,
|
||||
kind: row.kind,
|
||||
encrypted: row.encrypted,
|
||||
decoded: row.decoded,
|
||||
limitations: row.limitations,
|
||||
});
|
||||
if (ids !== undefined || paths !== undefined) {
|
||||
const candidates =
|
||||
ids !== undefined
|
||||
? candidateIndices(report, report.members, "member_id", ids)
|
||||
: candidateIndices(report, report.members, "path", paths!);
|
||||
if (candidates !== undefined) {
|
||||
return selectCandidateRows(
|
||||
report.members,
|
||||
candidates,
|
||||
view,
|
||||
matchMember,
|
||||
projectMember,
|
||||
);
|
||||
}
|
||||
}
|
||||
return selectViewRows(report.members, view, matchMember, projectMember);
|
||||
}
|
||||
if (view.collection === "streams") {
|
||||
const ids = values(view.stream_ids);
|
||||
return selectViewRows(
|
||||
report.streams,
|
||||
view,
|
||||
(row) => allows(ids, row.stream_id),
|
||||
(row) => ({
|
||||
...fields(
|
||||
row,
|
||||
view.fields ?? ["producer", "lines", "supported_events"],
|
||||
),
|
||||
stream_id: row.stream_id,
|
||||
member_id: row.member_id,
|
||||
role: row.role,
|
||||
version: row.version,
|
||||
state: row.state,
|
||||
}),
|
||||
);
|
||||
const matchStream = (row: PlaywrightTrace["streams"][number]): boolean =>
|
||||
allows(ids, row.stream_id);
|
||||
const projectStream = (row: PlaywrightTrace["streams"][number]) => ({
|
||||
...fields(row, view.fields ?? ["producer", "lines", "supported_events"]),
|
||||
stream_id: row.stream_id,
|
||||
member_id: row.member_id,
|
||||
role: row.role,
|
||||
version: row.version,
|
||||
state: row.state,
|
||||
});
|
||||
if (ids !== undefined) {
|
||||
const candidates = candidateIndices(
|
||||
report,
|
||||
report.streams,
|
||||
"stream_id",
|
||||
ids,
|
||||
);
|
||||
if (candidates !== undefined)
|
||||
return selectCandidateRows(
|
||||
report.streams,
|
||||
candidates,
|
||||
view,
|
||||
matchStream,
|
||||
projectStream,
|
||||
);
|
||||
}
|
||||
return selectViewRows(report.streams, view, matchStream, projectStream);
|
||||
}
|
||||
const codes = values(view.codes);
|
||||
const matchDiagnostic = (
|
||||
row: PlaywrightTrace["diagnostics"][number],
|
||||
): boolean => allows(codes, row.code);
|
||||
const projectDiagnostic = (row: PlaywrightTrace["diagnostics"][number]) => ({
|
||||
...fields(row, view.fields ?? ["message"]),
|
||||
diagnostic_id: row.diagnostic_id,
|
||||
member_id: row.member_id,
|
||||
line: row.line,
|
||||
code: row.code,
|
||||
});
|
||||
if (codes !== undefined) {
|
||||
const candidates = candidateIndices(
|
||||
report,
|
||||
report.diagnostics,
|
||||
"code",
|
||||
codes,
|
||||
);
|
||||
if (candidates !== undefined)
|
||||
return selectCandidateRows(
|
||||
report.diagnostics,
|
||||
candidates,
|
||||
view,
|
||||
matchDiagnostic,
|
||||
projectDiagnostic,
|
||||
);
|
||||
}
|
||||
return selectViewRows(
|
||||
report.diagnostics,
|
||||
view,
|
||||
(row) => allows(codes, row.code),
|
||||
(row) => ({
|
||||
...fields(row, view.fields ?? ["message"]),
|
||||
diagnostic_id: row.diagnostic_id,
|
||||
member_id: row.member_id,
|
||||
line: row.line,
|
||||
code: row.code,
|
||||
}),
|
||||
matchDiagnostic,
|
||||
projectDiagnostic,
|
||||
);
|
||||
};
|
||||
const selectEventText = (
|
||||
|
||||
Reference in new issue
Block a user