refactor(compiler): generate less code for bindings to DOM elements
Detailed changes: - remove `UNINITIALIZED`, initialize change detection fields with `undefined`. * we use `view.numberOfChecks === 0` now everywhere as indicator whether we are in the first change detection cycle (previously we used this only in a couple of places). * we keep the initialization itself as change detection get slower without it. - remove passing around `throwOnChange` in various generated calls, and store it on the view as property instead. - change generated code for bindings to DOM elements as follows: Before: ``` var currVal_10 = self.context.bgColor; if (jit_checkBinding15(self.throwOnChange,self._expr_10,currVal_10)) { self.renderer.setElementStyle(self._el_0,'backgroundColor',((self.viewUtils.sanitizer.sanitize(jit_21,currVal_10) == null)? null: self.viewUtils.sanitizer.sanitize(jit_21,currVal_10).toString())); self._expr_10 = currVal_10; } var currVal_11 = jit_inlineInterpolate16(1,' ',self.context.data.value,' '); if (jit_checkBinding15(self.throwOnChange,self._expr_11,currVal_11)) { self.renderer.setText(self._text_1,currVal_11); self._expr_11 = currVal_11; } ```, After: ``` var currVal_10 = self.context.bgColor; jit_checkRenderStyle14(self,self._el_0,'backgroundColor',null,self._expr_10,self._expr_10=currVal_10,false,jit_21); var currVal_11 = jit_inlineInterpolate15(1,' ',self.context.data.value,' '); jit_checkRenderText16(self,self._text_1,self._expr_11,self._expr_11=currVal_11,false); ``` Performance impact: - None seen (checked against internal latency lab) Part of #13651
This commit is contained in:
@ -21,7 +21,7 @@ import {AttrAst, BoundDirectivePropertyAst, BoundElementPropertyAst, BoundEventA
|
||||
|
||||
import {CompileElement, CompileNode} from './compile_element';
|
||||
import {CompileView, CompileViewRootNode, CompileViewRootNodeType} from './compile_view';
|
||||
import {ChangeDetectorStatusEnum, DetectChangesVars, InjectMethodVars, ViewConstructorVars, ViewEncapsulationEnum, ViewProperties, ViewTypeEnum} from './constants';
|
||||
import {ChangeDetectorStatusEnum, InjectMethodVars, ViewConstructorVars, ViewEncapsulationEnum, ViewProperties, ViewTypeEnum} from './constants';
|
||||
import {ComponentFactoryDependency, ComponentViewDependency, DirectiveWrapperDependency} from './deps';
|
||||
|
||||
const IMPLICIT_TEMPLATE_VAR = '\$implicit';
|
||||
@ -483,9 +483,7 @@ function createViewClass(
|
||||
],
|
||||
addReturnValuefNotEmpty(view.injectorGetMethod.finish(), InjectMethodVars.notFoundResult),
|
||||
o.DYNAMIC_TYPE),
|
||||
new o.ClassMethod(
|
||||
'detectChangesInternal', [new o.FnParam(DetectChangesVars.throwOnChange.name, o.BOOL_TYPE)],
|
||||
generateDetectChangesMethod(view)),
|
||||
new o.ClassMethod('detectChangesInternal', [], generateDetectChangesMethod(view)),
|
||||
new o.ClassMethod('dirtyParentQueriesInternal', [], view.dirtyParentQueriesMethod.finish()),
|
||||
new o.ClassMethod('destroyInternal', [], generateDestroyMethod(view)),
|
||||
new o.ClassMethod('detachInternal', [], view.detachMethod.finish()),
|
||||
@ -569,36 +567,26 @@ function generateDetectChangesMethod(view: CompileView): o.Statement[] {
|
||||
stmts.push(...view.detectChangesInInputsMethod.finish());
|
||||
view.viewContainers.forEach((viewContainer) => {
|
||||
stmts.push(
|
||||
viewContainer.callMethod('detectChangesInNestedViews', [DetectChangesVars.throwOnChange])
|
||||
viewContainer.callMethod('detectChangesInNestedViews', [ViewProperties.throwOnChange])
|
||||
.toStmt());
|
||||
});
|
||||
const afterContentStmts = view.updateContentQueriesMethod.finish().concat(
|
||||
view.afterContentLifecycleCallbacksMethod.finish());
|
||||
if (afterContentStmts.length > 0) {
|
||||
stmts.push(new o.IfStmt(o.not(DetectChangesVars.throwOnChange), afterContentStmts));
|
||||
stmts.push(new o.IfStmt(o.not(ViewProperties.throwOnChange), afterContentStmts));
|
||||
}
|
||||
stmts.push(...view.detectChangesRenderPropertiesMethod.finish());
|
||||
view.viewChildren.forEach((viewChild) => {
|
||||
stmts.push(
|
||||
viewChild.callMethod('internalDetectChanges', [DetectChangesVars.throwOnChange]).toStmt());
|
||||
viewChild.callMethod('internalDetectChanges', [ViewProperties.throwOnChange]).toStmt());
|
||||
});
|
||||
const afterViewStmts =
|
||||
view.updateViewQueriesMethod.finish().concat(view.afterViewLifecycleCallbacksMethod.finish());
|
||||
if (afterViewStmts.length > 0) {
|
||||
stmts.push(new o.IfStmt(o.not(DetectChangesVars.throwOnChange), afterViewStmts));
|
||||
stmts.push(new o.IfStmt(o.not(ViewProperties.throwOnChange), afterViewStmts));
|
||||
}
|
||||
|
||||
const varStmts: any[] = [];
|
||||
const readVars = o.findReadVarNames(stmts);
|
||||
if (readVars.has(DetectChangesVars.changed.name)) {
|
||||
varStmts.push(DetectChangesVars.changed.set(o.literal(true)).toDeclStmt(o.BOOL_TYPE));
|
||||
}
|
||||
if (readVars.has(DetectChangesVars.changes.name)) {
|
||||
varStmts.push(
|
||||
DetectChangesVars.changes.set(o.NULL_EXPR)
|
||||
.toDeclStmt(new o.MapType(o.importType(createIdentifier(Identifiers.SimpleChange)))));
|
||||
}
|
||||
varStmts.push(...createSharedBindingVariablesIfNeeded(stmts));
|
||||
const varStmts = createSharedBindingVariablesIfNeeded(stmts);
|
||||
return varStmts.concat(stmts);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user