perf(javascript): compare scalar semantic query seeds directly

Avoid repeatedly canonicalizing scalar seeds for every literal node. Valid literal metadata without a scalar value now remains unmatched instead of throwing.

Flameox identified canonicalization as the hotspot. Alternating warm query benchmarks over 2,002 nodes reduced the 35.5 KB seed median from 36.34 ms to 0.158 ms; a short seed improved from 0.304 ms to 0.128 ms with identical result digests. These measurements cover the query function, not end-to-end analysis.

Validation: 38 domain query/traversal tests and focused compiled CLI/MCP consumers; static, architecture and documentation checks passed.
This commit is contained in:
morluto committed 2026-10-11 03:53:50 +08:00
1 parent 5100a1a1a5
commit 2014ee7d68
2 files changed
+46 -6

No files matched your search

@@ -2,6 +2,7 @@ import { expect, it } from "vitest";
import { z } from "zod";
import { digestCanonicalValue } from "../canonicalDigest.js";
import type { JsonValue } from "../jsonValue.js";
import type { ApplicationGraphEvidence } from "./javascriptApplicationEvidenceSchemas.js";
import {
JAVASCRIPT_SEMANTIC_RELATION_FAMILY,
@@ -105,7 +106,7 @@ const unknownEvidence = (): ApplicationGraphEvidence => ({
const node = (
kind: (typeof JAVASCRIPT_SEMANTIC_NODE_KINDS)[number],
role: string,
properties: Record<string, string | number | boolean | null> = {},
properties: Record<string, JsonValue> = {},
evidenceContexts: JavaScriptSemanticEvidenceContextRegistry,
options: {
readonly functionNodeId?: string | null;
@@ -399,6 +400,49 @@ it("counts candidate edges by query direction and relation filter", () => {
expect(query("backward-provenance", ["captures"]).status).toBe("found");
});
it("matches scalar literal seeds and skips non-scalar literal metadata", () => {
const input = fixtureInput();
const metadataContexts = new JavaScriptSemanticEvidenceContextRegistry();
const literals = [
node("literal", "string-seven", { value: "7" }, metadataContexts),
node("literal", "number-seven", { value: 7 }, metadataContexts),
node("literal", "boolean-true", { value: true }, metadataContexts),
node("literal", "null", { value: null }, metadataContexts),
node("literal", "empty-string", { value: "" }, metadataContexts),
node("literal", "zero", { value: 0 }, metadataContexts),
node("literal", "negative-zero", { value: -0 }, metadataContexts),
node("literal", "missing-value", {}, metadataContexts),
node(
"literal",
"structured-value",
{ value: { nested: "metadata" } },
metadataContexts,
),
];
input.nodes.push(...literals);
const graph = createJavaScriptSemanticGraph(input);
const query = (value: string | number | boolean | null) =>
queryJavaScriptSemanticGraph(graph, {
seed: { kind: "literal", value },
direction: "forward-influence",
}).seed_node_ids;
const nodeId = (role: string) => {
const literal = literals.find(({ identity }) => identity.role_key === role);
if (literal === undefined) throw new Error(`Missing literal ${role}`);
return literal.node_id;
};
expect(query("7")).toEqual([nodeId("string-seven")]);
expect(query(7)).toEqual([nodeId("number-seven")]);
expect(query(true)).toEqual([nodeId("boolean-true")]);
expect(query(null)).toEqual([nodeId("null")]);
expect(query("")).toEqual([nodeId("empty-string")]);
expect(query(0)).toEqual(
[nodeId("zero"), nodeId("negative-zero")].toSorted(),
);
expect(query("unmatched")).toEqual([]);
});
it("counts candidate ownership cycles once and traverses them finitely", () => {
const graph = graphWithCandidateRelations();
const binding = graph.nodes.find(({ label }) => label === "binding");