perf(domain): order JSON shapes through their structural tree
Traverse sorted siblings instead of comparing complete shared path prefixes. Return the typed producer result directly while preserving independent path segments and schema validation at unknown-input boundaries. Validation: 12 domain/CDP boundary tests, build, and check:fast passed. Six paired Flameox blocks retained identical full-output digests across deep, mixed, Unicode, and wide controls. Depth-512 inference median fell from 10.78 ms to 1.51 ms (120 samples per variant); whole harness median fell from 518.6 ms to 327.5 ms. Flameox evidence: f4a93424f34e5bfea254ada4ef278dc9bbb6c21047c977e20da130e11a8899e9. Timings describe this bounded workload only.
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
import { describe, expect, it } from "vitest";
|
import { describe, expect, it } from "vitest";
|
||||||
|
|
||||||
import { inferJsonShape } from "./jsonShape.js";
|
import { inferJsonShape, jsonShapeSchema } from "./jsonShape.js";
|
||||||
|
|
||||||
const property = (name: string) => ({ kind: "property" as const, name });
|
const property = (name: string) => ({ kind: "property" as const, name });
|
||||||
const element = { kind: "array-element" as const };
|
const element = { kind: "array-element" as const };
|
||||||
@@ -68,6 +68,40 @@ describe("inferJsonShape", () => {
|
|||||||
it("rejects malformed JSON", () => {
|
it("rejects malformed JSON", () => {
|
||||||
expect(inferJsonShape("not-json")).toBeNull();
|
expect(inferJsonShape("not-json")).toBeNull();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("orders mixed array and property descendants with parents first", () => {
|
||||||
|
const shape = inferJsonShape('[{"z":1,"a":{"x":true}},[null],{"a":2}]');
|
||||||
|
|
||||||
|
expect(shape?.properties.map(({ path }) => path)).toEqual([
|
||||||
|
[element],
|
||||||
|
[element, element],
|
||||||
|
[element, property("a")],
|
||||||
|
[element, property("a"), property("x")],
|
||||||
|
[element, property("z")],
|
||||||
|
]);
|
||||||
|
expect(shape?.properties[2]).toMatchObject({
|
||||||
|
types: ["number", "object"],
|
||||||
|
observations: 2,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("retains deep coverage and independently owned path segments", () => {
|
||||||
|
const depth = 512;
|
||||||
|
const shape = inferJsonShape("[".repeat(depth) + "1" + "]".repeat(depth));
|
||||||
|
expect(shape).not.toBeNull();
|
||||||
|
if (shape === null) return;
|
||||||
|
|
||||||
|
expect(shape.node_count).toBe(depth + 1);
|
||||||
|
expect(shape.max_depth_observed).toBe(depth);
|
||||||
|
expect(shape.properties).toHaveLength(depth);
|
||||||
|
expect(shape.properties.at(-1)).toEqual({
|
||||||
|
path: Array.from({ length: depth }, () => element),
|
||||||
|
types: ["number"],
|
||||||
|
observations: 1,
|
||||||
|
});
|
||||||
|
expect(jsonShapeSchema.parse(shape)).toEqual(shape);
|
||||||
|
expect(shape.properties[0]?.path[0]).not.toBe(shape.properties[1]?.path[0]);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("JSON shape completeness and ordering", () => {
|
describe("JSON shape completeness and ordering", () => {
|
||||||
|
|||||||
+30
-28
@@ -52,7 +52,6 @@ export const inferJsonShape = (text: string): JsonShape | null => {
|
|||||||
void cause;
|
void cause;
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
const properties: ShapeNode[] = [];
|
|
||||||
const rootShape: ShapeNode = {
|
const rootShape: ShapeNode = {
|
||||||
path: [],
|
path: [],
|
||||||
types: new Set(),
|
types: new Set(),
|
||||||
@@ -77,7 +76,6 @@ export const inferJsonShape = (text: string): JsonShape | null => {
|
|||||||
observations: 0,
|
observations: 0,
|
||||||
};
|
};
|
||||||
children.set(key, node);
|
children.set(key, node);
|
||||||
properties.push(node);
|
|
||||||
return node;
|
return node;
|
||||||
};
|
};
|
||||||
const pending: Array<{
|
const pending: Array<{
|
||||||
@@ -109,18 +107,26 @@ export const inferJsonShape = (text: string): JsonShape | null => {
|
|||||||
pending.push({ value, shape: childShape(current.shape, name) });
|
pending.push({ value, shape: childShape(current.shape, name) });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return jsonShapeSchema.parse({
|
// Preorder over sorted siblings gives canonical paths without repeatedly
|
||||||
|
// comparing their shared prefixes. Each returned path owns its segments.
|
||||||
|
const properties: JsonShape["properties"] = [];
|
||||||
|
const shapes = orderedChildren(rootShape);
|
||||||
|
while (shapes.length > 0) {
|
||||||
|
const shape = shapes.pop();
|
||||||
|
if (shape === undefined) break;
|
||||||
|
properties.push({
|
||||||
|
path: shape.path.map((segment) => ({ ...segment })),
|
||||||
|
types: [...shape.types].sort(),
|
||||||
|
observations: shape.observations,
|
||||||
|
});
|
||||||
|
for (const child of orderedChildren(shape)) shapes.push(child);
|
||||||
|
}
|
||||||
|
return {
|
||||||
root_type: jsonValueType(root),
|
root_type: jsonValueType(root),
|
||||||
node_count: nodeCount,
|
node_count: nodeCount,
|
||||||
max_depth_observed: maxDepthObserved,
|
max_depth_observed: maxDepthObserved,
|
||||||
properties: properties
|
properties,
|
||||||
.map((value) => ({
|
};
|
||||||
path: value.path,
|
|
||||||
types: [...value.types].sort(),
|
|
||||||
observations: value.observations,
|
|
||||||
}))
|
|
||||||
.sort((left, right) => compareShapePaths(left.path, right.path)),
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const jsonValueType = (value: unknown): JsonValueType => {
|
const jsonValueType = (value: unknown): JsonValueType => {
|
||||||
@@ -141,20 +147,16 @@ const jsonValueType = (value: unknown): JsonValueType => {
|
|||||||
const isRecord = (value: unknown): value is Readonly<Record<string, unknown>> =>
|
const isRecord = (value: unknown): value is Readonly<Record<string, unknown>> =>
|
||||||
typeof value === "object" && value !== null && !Array.isArray(value);
|
typeof value === "object" && value !== null && !Array.isArray(value);
|
||||||
|
|
||||||
const compareShapePaths = (
|
/** Reverse sibling order for the traversal stack; array elements sort first. */
|
||||||
left: readonly JsonShapePathSegment[],
|
const orderedChildren = (node: ShapeNode): ShapeNode[] =>
|
||||||
right: readonly JsonShapePathSegment[],
|
[...(node.children ?? [])]
|
||||||
): number => {
|
.sort(([left], [right]) =>
|
||||||
for (let index = 0; index < Math.min(left.length, right.length); index += 1) {
|
left === right
|
||||||
const a = left[index];
|
? 0
|
||||||
const b = right[index];
|
: left === arrayElementKey
|
||||||
if (a === undefined || b === undefined) break;
|
? 1
|
||||||
const order =
|
: right === arrayElementKey
|
||||||
compareUnicodeCodePoints(a.kind, b.kind) ||
|
? -1
|
||||||
(a.kind === "property" && b.kind === "property"
|
: compareUnicodeCodePoints(right, left),
|
||||||
? compareUnicodeCodePoints(a.name, b.name)
|
)
|
||||||
: 0);
|
.map(([, child]) => child);
|
||||||
if (order !== 0) return order;
|
|
||||||
}
|
|
||||||
return left.length - right.length;
|
|
||||||
};
|
|
||||||
|
|||||||
Reference in New Issue
Block a user