Skip to content

Commit d05c186

Browse files
committed
perf(metadata-protocol): share a root property's reference-site walk within one derivation
Adding object_extension made deriveReferenceSites() walk the FieldSchema record and the validation-rule array a second time: object and object_extension declare the same `fields` and `validations`, and the `validations` walk alone is most of a type's cost. The derivation test's 5s budget went red in CI (5532ms). A root property's walk now runs once per derivation for every source type declaring the same property, and later types replay its bindings. The key is the property's schema less its prose (string `description`/`title`, which no step reads), its trail and the root's `$defs`. The cache lives in one derivation, so two calls still agree; the index equals the uncached walk's exactly, and against main it adds only object_extension's four sites (field, fields, object, picklist). Measured, ms per derivation, 6 runs per round, shared box: - main: 431 423 425 462 535 473 / 407 422 396 461 377 380 - head before: 680 875 865 783 759 603 / 666 664 726 674 698 853 - head after: 447 473 408 482 379 373 / 415 401 390 420 413 381 - main, same session as "after": 416 402 377 417 369 380 / 477 571 401 424 419 402 No timeout changed and no test skipped. Co-Authored-By: Claude <noreply@anthropic.com>
1 parent ccfa089 commit d05c186

1 file changed

Lines changed: 58 additions & 2 deletions

File tree

‎packages/metadata-protocol/src/reference-sites.ts‎

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -532,9 +532,16 @@ function collectSitesFromSchema(
532532
singular: ReadonlyMap<string, string>,
533533
plural: ReadonlyMap<string, string>,
534534
into: Map<string, ReferenceSite>,
535+
sharedWalks?: SharedWalkCache,
535536
): void {
536537
const defs = (root.$defs as Record<string, JsonSchemaNode> | undefined) ?? {};
537538
const visited = new Set<string>();
539+
// Lazily, and once per root: the `$defs` a property's `$ref`s resolve
540+
// through are part of what the property means.
541+
let defsKey: string | undefined;
542+
// The bindings found below the root property being walked, so its walk
543+
// can be replayed under another source type.
544+
let frame: Array<readonly [string, string]> | undefined;
538545

539546
const walk = (node: unknown, trail: string, depth: number): void => {
540547
if (depth > 12) return;
@@ -548,11 +555,13 @@ function collectSitesFromSchema(
548555
if (target && carriesNames(child, defs, pluralTarget !== undefined)) {
549556
const site: ReferenceSite = { fromType, property: key, target };
550557
into.set(`${fromType}|${key}|${target}`, site);
558+
frame?.push([key, target]);
551559
}
552560
const branch = `${trail}.${key}`;
553561
if (visited.has(branch)) continue;
554562
visited.add(branch);
555-
walk(child, branch, depth + 1);
563+
if (depth === 0) walkRootProperty(child, branch);
564+
else walk(child, branch, depth + 1);
556565
}
557566
}
558567
if (alt.items) walk(alt.items, `${trail}[]`, depth + 1);
@@ -562,9 +571,55 @@ function collectSitesFromSchema(
562571
}
563572
};
564573

574+
// [#28574] A root property's walk, done once per derivation for every
575+
// source type that declares the same property. `object` and
576+
// `object_extension` declare the same `fields` record and `validations`
577+
// array, and the `validations` walk alone is most of a type's cost; the
578+
// second source type replays the bindings the first walk found. The key
579+
// is everything the walk below the property reads: the property's schema
580+
// less its prose (`description` and `title` text, which no step reads and
581+
// which is all that tells the two `validations` apart), its trail and the
582+
// root's `$defs`. No branch below a root property's trail is visited
583+
// before its walk starts (every such branch extends it), so a replay
584+
// records exactly what a walk would.
585+
const walkRootProperty = (child: unknown, branch: string): void => {
586+
if (!sharedWalks) {
587+
walk(child, branch, 1);
588+
return;
589+
}
590+
defsKey ??= JSON.stringify(defs, withoutProse);
591+
const cacheKey = `${branch}|${defsKey}|${JSON.stringify(child, withoutProse)}`;
592+
const replay = sharedWalks.get(cacheKey);
593+
if (replay) {
594+
for (const [key, target] of replay) {
595+
into.set(`${fromType}|${key}|${target}`, { fromType, property: key, target });
596+
}
597+
return;
598+
}
599+
frame = [];
600+
try {
601+
walk(child, branch, 1);
602+
sharedWalks.set(cacheKey, frame);
603+
} finally {
604+
frame = undefined;
605+
}
606+
};
607+
565608
walk(root, '', 0);
566609
}
567610

611+
/**
612+
* A `JSON.stringify` replacer dropping a schema's prose. Only a string-valued
613+
* `description` or `title` is prose: a property NAMED `description` maps to a
614+
* schema object and is kept.
615+
*/
616+
function withoutProse(key: string, value: unknown): unknown {
617+
return (key === 'description' || key === 'title') && typeof value === 'string' ? undefined : value;
618+
}
619+
620+
/** One derivation's root-property walks ({@link collectSitesFromSchema}); never shared across derivations. */
621+
type SharedWalkCache = Map<string, ReadonlyArray<readonly [string, string]>>;
622+
568623
/**
569624
* Build the reference-site index from the metadata type schemas.
570625
*
@@ -586,6 +641,7 @@ export function deriveReferenceSites(): ReferenceSiteIndex {
586641

587642
const sites = new Map<string, ReferenceSite>();
588643
const unwalkable: string[] = [];
644+
const sharedWalks: SharedWalkCache = new Map();
589645

590646
// Limb 1 — every declared type's own schema.
591647
for (const type of declaredTypes) {
@@ -601,7 +657,7 @@ export function deriveReferenceSites(): ReferenceSiteIndex {
601657
unwalkable.push(type);
602658
continue;
603659
}
604-
collectSitesFromSchema(type, json, singular, plural, sites);
660+
collectSitesFromSchema(type, json, singular, plural, sites, sharedWalks);
605661
}
606662

607663
// Limb 2 — flow node configs. See the module header: `FlowSchema` declares

0 commit comments

Comments
 (0)