Skip to content

Commit 03c9746

Browse files
committed
Cache id constraint on RuntimeModelSpec to avoid redundant iteration
Compute resolveModelIdConstraint once during collectRuntimeModelSpecs and store it on the spec. resolveRelationAnchorFields and resolveSemanticModelNode now read the cached value instead of re-scanning all field builders per call.
1 parent d116062 commit 03c9746

1 file changed

Lines changed: 13 additions & 5 deletions

File tree

packages/2-sql/2-authoring/contract-ts/src/staged-contract-lowering.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ type RuntimeModelSpec = {
4747
readonly relations: Record<string, RelationBuilder<StagedRelationState>>;
4848
readonly attributesSpec: ModelAttributesSpec | undefined;
4949
readonly sqlSpec: SqlStageSpec | undefined;
50+
readonly idConstraint: IdConstraint | undefined;
5051
};
5152

5253
type RuntimeStagedCollection = {
@@ -119,7 +120,9 @@ function mapFieldNamesToColumnNames(
119120
});
120121
}
121122

122-
function resolveInlineIdConstraint(spec: RuntimeModelSpec): IdConstraint | undefined {
123+
function resolveInlineIdConstraint(
124+
spec: Pick<RuntimeModelSpec, 'modelName' | 'fieldBuilders'>,
125+
): IdConstraint | undefined {
123126
const inlineIdFields: string[] = [];
124127
let idName: string | undefined;
125128

@@ -176,7 +179,9 @@ function collectInlineUniqueConstraints(spec: RuntimeModelSpec): readonly Unique
176179
return constraints;
177180
}
178181

179-
function resolveModelIdConstraint(spec: RuntimeModelSpec): IdConstraint | undefined {
182+
function resolveModelIdConstraint(
183+
spec: Pick<RuntimeModelSpec, 'modelName' | 'fieldBuilders' | 'attributesSpec'>,
184+
): IdConstraint | undefined {
180185
const inlineId = resolveInlineIdConstraint(spec);
181186
const attributeId = spec.attributesSpec?.id;
182187

@@ -245,7 +250,7 @@ function resolveRelationForeignKeys(
245250
}
246251

247252
function resolveRelationAnchorFields(spec: RuntimeModelSpec): readonly string[] {
248-
const idFields = resolveModelIdConstraint(spec)?.fields;
253+
const idFields = spec.idConstraint?.fields;
249254
if (idFields && idFields.length > 0) {
250255
return idFields;
251256
}
@@ -508,7 +513,7 @@ function resolveSemanticModelNode(
508513
});
509514
}
510515

511-
const idConstraint = resolveModelIdConstraint(spec);
516+
const { idConstraint } = spec;
512517
const uniques = resolveModelUniqueConstraints(spec).map((unique) => ({
513518
columns: mapFieldNamesToColumnNames(spec.modelName, unique.fields, spec.fieldToColumn),
514519
...(unique.name ? { name: unique.name } : {}),
@@ -592,14 +597,17 @@ function collectRuntimeModelSpecs(definition: StagedContractInput): RuntimeStage
592597
fieldToColumn[fieldName] = columnName;
593598
}
594599

600+
const fieldBuilders = modelDefinition.stageOne.fields;
601+
const idConstraint = resolveModelIdConstraint({ modelName, fieldBuilders, attributesSpec });
595602
modelSpecs.set(modelName, {
596603
modelName,
597604
tableName,
598-
fieldBuilders: modelDefinition.stageOne.fields,
605+
fieldBuilders,
599606
fieldToColumn,
600607
relations: modelDefinition.stageOne.relations,
601608
attributesSpec,
602609
sqlSpec,
610+
idConstraint,
603611
});
604612
}
605613

0 commit comments

Comments
 (0)