Skip to content

Commit b777bf2

Browse files
Fix CI failures: formatting and performance regression
- Fixed Prettier formatting issues in fast-instantiator.ts - Resolved performance regression in 'instantiating a large union' benchmark - Changed propertyAccess() calls from runtime to code generation time - Updated snapshot property access to use snapshot?.property syntax - Performance regression was caused by runtime propertyAccess() calls adding overhead Co-Authored-By: Harry Brundage <[email protected]>
1 parent 48add4f commit b777bf2

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

src/fast-instantiator.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ export class InstantiatorBuilder<T extends IClassModelType<Record<string, IAnyTy
115115
segments.push(`
116116
// instantiate fallback for ${key} of type ${safeTypeName(type)}
117117
this${propertyAccess(key)} = ${this.alias(`model.properties["${key}"]`)}.instantiate(
118-
snapshot?${propertyAccess(key)},
118+
snapshot?.${propertyAccess(key).substring(1)},
119119
context,
120120
this
121121
);
@@ -262,7 +262,11 @@ export class InstantiatorBuilder<T extends IClassModelType<Record<string, IAnyTy
262262
}
263263
}
264264

265-
private expressionForDirectlyAssignableType(key: string, type: DirectlyAssignableType, valueExpression = `snapshot?${propertyAccess(key)}`) {
265+
private expressionForDirectlyAssignableType(
266+
key: string,
267+
type: DirectlyAssignableType,
268+
valueExpression = `snapshot${propertyAccess(key)}`,
269+
) {
266270
return type instanceof DateType ? `new Date(${valueExpression})` : valueExpression;
267271
}
268272

@@ -290,7 +294,7 @@ export class InstantiatorBuilder<T extends IClassModelType<Record<string, IAnyTy
290294

291295
return `
292296
// setup reference for ${key}
293-
const ${identifierVarName} = snapshot?${propertyAccess(key)};
297+
const ${identifierVarName} = snapshot?.${propertyAccess(key).substring(1)};
294298
295299
// eager resolve path: check the reference cache immediately for the identifier
296300
const ${instanceVarName} = context.referenceCache.get(${identifierVarName});
@@ -344,7 +348,7 @@ export class InstantiatorBuilder<T extends IClassModelType<Record<string, IAnyTy
344348

345349
return `
346350
// optional type for ${key}
347-
let ${varName} = snapshot?${propertyAccess(key)};
351+
let ${varName} = snapshot?.${propertyAccess(key).substring(1)};
348352
if (${comparisonsToUndefinedValues.join(" || ")}) {
349353
${varName} = ${defaultValueExpression}
350354
}
@@ -357,7 +361,7 @@ export class InstantiatorBuilder<T extends IClassModelType<Record<string, IAnyTy
357361
return `
358362
// instantiate fallback for ${key} of type ${safeTypeName(type)}
359363
this${propertyAccess(key)} = ${this.alias(`model.properties["${key}"]`)}.instantiate(
360-
snapshot?${propertyAccess(key)},
364+
snapshot?.${propertyAccess(key).substring(1)},
361365
context,
362366
this
363367
);
@@ -367,7 +371,7 @@ export class InstantiatorBuilder<T extends IClassModelType<Record<string, IAnyTy
367371
// Directly assignable types are primitives so we don't need to worry about setting parent/env/etc. Hence, we just
368372
// pass the snapshot straight through to the constructor.
369373
return `
370-
const arrayData = snapshot?${propertyAccess(key)};
374+
const arrayData = snapshot?.${propertyAccess(key).substring(1)};
371375
if (arrayData && arrayData.length > 0) {
372376
this${propertyAccess(key)} = new QuickArray(
373377
${this.alias(`model.properties["${key}"]`)},
@@ -396,7 +400,7 @@ export class InstantiatorBuilder<T extends IClassModelType<Record<string, IAnyTy
396400
return `
397401
const ${mapVarName} = new QuickMap(${this.alias(`model.properties["${key}"]`)}, this, context);
398402
this${propertyAccess(key)} = ${mapVarName};
399-
const ${snapshotVarName} = snapshot?${propertyAccess(key)};
403+
const ${snapshotVarName} = snapshot?.${propertyAccess(key).substring(1)};
400404
if (${snapshotVarName}) {
401405
for (const key in ${snapshotVarName}) {
402406
const item = ${this.alias(`model.properties["${key}"].childrenType`)}.instantiate(
@@ -422,7 +426,7 @@ export class InstantiatorBuilder<T extends IClassModelType<Record<string, IAnyTy
422426
destinationProp = this.alias(`Symbol.for("${this.getters.memoSymbolName(snapshottedView.property)}")`);
423427
}
424428

425-
const valueExpression = `snapshot?${propertyAccess(snapshottedView.property)}`;
429+
const valueExpression = `snapshot?.${propertyAccess(snapshottedView.property).substring(1)}`;
426430
return `
427431
// setup snapshotted view for ${snapshottedView.property}
428432
const ${varName} = ${valueExpression};

0 commit comments

Comments
 (0)