refactor(core): don’t use switch fall through but rather multiple ifs

V8 does this internally any ways. This way, we can also keep the existing
dirty checking order, which some apps sadly rely on.
This commit is contained in:
Tobias Bosch 2017-02-20 15:06:23 -08:00 committed by Igor Minar
parent 90226f7714
commit bb0460b93b
6 changed files with 114 additions and 215 deletions

View File

@ -199,29 +199,17 @@ function renderEventHandlerClosure(view: ViewData, index: number, eventName: str
export function checkAndUpdateElementInline(
view: ViewData, def: NodeDef, v0: any, v1: any, v2: any, v3: any, v4: any, v5: any, v6: any,
v7: any, v8: any, v9: any) {
// Note: fallthrough is intended!
switch (def.bindings.length) {
case 10:
checkAndUpdateElementValue(view, def, 9, v9);
case 9:
checkAndUpdateElementValue(view, def, 8, v8);
case 8:
checkAndUpdateElementValue(view, def, 7, v7);
case 7:
checkAndUpdateElementValue(view, def, 6, v6);
case 6:
checkAndUpdateElementValue(view, def, 5, v5);
case 5:
checkAndUpdateElementValue(view, def, 4, v4);
case 4:
checkAndUpdateElementValue(view, def, 3, v3);
case 3:
checkAndUpdateElementValue(view, def, 2, v2);
case 2:
checkAndUpdateElementValue(view, def, 1, v1);
case 1:
checkAndUpdateElementValue(view, def, 0, v0);
}
const bindLen = def.bindings.length;
if (bindLen > 0) checkAndUpdateElementValue(view, def, 0, v0);
if (bindLen > 1) checkAndUpdateElementValue(view, def, 1, v1);
if (bindLen > 2) checkAndUpdateElementValue(view, def, 2, v2);
if (bindLen > 3) checkAndUpdateElementValue(view, def, 3, v3);
if (bindLen > 4) checkAndUpdateElementValue(view, def, 4, v4);
if (bindLen > 5) checkAndUpdateElementValue(view, def, 5, v5);
if (bindLen > 6) checkAndUpdateElementValue(view, def, 6, v6);
if (bindLen > 7) checkAndUpdateElementValue(view, def, 7, v7);
if (bindLen > 8) checkAndUpdateElementValue(view, def, 8, v8);
if (bindLen > 9) checkAndUpdateElementValue(view, def, 9, v9);
}
export function checkAndUpdateElementDynamic(view: ViewData, def: NodeDef, values: any[]) {

View File

@ -175,29 +175,17 @@ export function checkAndUpdateDirectiveInline(
const providerData = asProviderData(view, def.index);
const directive = providerData.instance;
let changes: SimpleChanges;
// Note: fallthrough is intended!
switch (def.bindings.length) {
case 10:
changes = checkAndUpdateProp(view, providerData, def, 9, v9, changes);
case 9:
changes = checkAndUpdateProp(view, providerData, def, 8, v8, changes);
case 8:
changes = checkAndUpdateProp(view, providerData, def, 7, v7, changes);
case 7:
changes = checkAndUpdateProp(view, providerData, def, 6, v6, changes);
case 6:
changes = checkAndUpdateProp(view, providerData, def, 5, v5, changes);
case 5:
changes = checkAndUpdateProp(view, providerData, def, 4, v4, changes);
case 4:
changes = checkAndUpdateProp(view, providerData, def, 3, v3, changes);
case 3:
changes = checkAndUpdateProp(view, providerData, def, 2, v2, changes);
case 2:
changes = checkAndUpdateProp(view, providerData, def, 1, v1, changes);
case 1:
changes = checkAndUpdateProp(view, providerData, def, 0, v0, changes);
}
const bindLen = def.bindings.length;
if (bindLen > 0) changes = checkAndUpdateProp(view, providerData, def, 0, v0, changes);
if (bindLen > 1) changes = checkAndUpdateProp(view, providerData, def, 1, v1, changes);
if (bindLen > 2) changes = checkAndUpdateProp(view, providerData, def, 2, v2, changes);
if (bindLen > 3) changes = checkAndUpdateProp(view, providerData, def, 3, v3, changes);
if (bindLen > 4) changes = checkAndUpdateProp(view, providerData, def, 4, v4, changes);
if (bindLen > 5) changes = checkAndUpdateProp(view, providerData, def, 5, v5, changes);
if (bindLen > 6) changes = checkAndUpdateProp(view, providerData, def, 6, v6, changes);
if (bindLen > 7) changes = checkAndUpdateProp(view, providerData, def, 7, v7, changes);
if (bindLen > 8) changes = checkAndUpdateProp(view, providerData, def, 8, v8, changes);
if (bindLen > 9) changes = checkAndUpdateProp(view, providerData, def, 9, v9, changes);
if (changes) {
directive.ngOnChanges(changes);
}

View File

@ -72,29 +72,17 @@ export function checkAndUpdatePureExpressionInline(
v7: any, v8: any, v9: any) {
const bindings = def.bindings;
let changed = false;
// Note: fallthrough is intended!
switch (bindings.length) {
case 10:
if (checkAndUpdateBinding(view, def, 9, v9)) changed = true;
case 9:
if (checkAndUpdateBinding(view, def, 8, v8)) changed = true;
case 8:
if (checkAndUpdateBinding(view, def, 7, v7)) changed = true;
case 7:
if (checkAndUpdateBinding(view, def, 6, v6)) changed = true;
case 6:
if (checkAndUpdateBinding(view, def, 5, v5)) changed = true;
case 5:
if (checkAndUpdateBinding(view, def, 4, v4)) changed = true;
case 4:
if (checkAndUpdateBinding(view, def, 3, v3)) changed = true;
case 3:
if (checkAndUpdateBinding(view, def, 2, v2)) changed = true;
case 2:
if (checkAndUpdateBinding(view, def, 1, v1)) changed = true;
case 1:
if (checkAndUpdateBinding(view, def, 0, v0)) changed = true;
}
const bindLen = bindings.length;
if (bindLen > 0 && checkAndUpdateBinding(view, def, 0, v0)) changed = true;
if (bindLen > 1 && checkAndUpdateBinding(view, def, 1, v1)) changed = true;
if (bindLen > 2 && checkAndUpdateBinding(view, def, 2, v2)) changed = true;
if (bindLen > 3 && checkAndUpdateBinding(view, def, 3, v3)) changed = true;
if (bindLen > 4 && checkAndUpdateBinding(view, def, 4, v4)) changed = true;
if (bindLen > 5 && checkAndUpdateBinding(view, def, 5, v5)) changed = true;
if (bindLen > 6 && checkAndUpdateBinding(view, def, 6, v6)) changed = true;
if (bindLen > 7 && checkAndUpdateBinding(view, def, 7, v7)) changed = true;
if (bindLen > 8 && checkAndUpdateBinding(view, def, 8, v8)) changed = true;
if (bindLen > 9 && checkAndUpdateBinding(view, def, 9, v9)) changed = true;
const data = asPureExpressionData(view, def.index);
if (changed) {
@ -102,88 +90,62 @@ export function checkAndUpdatePureExpressionInline(
switch (def.pureExpression.type) {
case PureExpressionType.Array:
value = new Array(bindings.length);
// Note: fallthrough is intended!
switch (bindings.length) {
case 10:
value[9] = v9;
case 9:
value[8] = v8;
case 8:
value[7] = v7;
case 7:
value[6] = v6;
case 6:
value[5] = v5;
case 5:
value[4] = v4;
case 4:
value[3] = v3;
case 3:
value[2] = v2;
case 2:
value[1] = v1;
case 1:
value[0] = v0;
}
if (bindLen > 0) value[0] = v0;
if (bindLen > 1) value[1] = v1;
if (bindLen > 2) value[2] = v2;
if (bindLen > 3) value[3] = v3;
if (bindLen > 4) value[4] = v4;
if (bindLen > 5) value[5] = v5;
if (bindLen > 6) value[6] = v6;
if (bindLen > 7) value[7] = v7;
if (bindLen > 8) value[8] = v8;
if (bindLen > 9) value[9] = v9;
break;
case PureExpressionType.Object:
value = {};
// Note: fallthrough is intended!
switch (bindings.length) {
case 10:
value[bindings[9].name] = v9;
case 9:
value[bindings[8].name] = v8;
case 8:
value[bindings[7].name] = v7;
case 7:
value[bindings[6].name] = v6;
case 6:
value[bindings[5].name] = v5;
case 5:
value[bindings[4].name] = v4;
case 4:
value[bindings[3].name] = v3;
case 3:
value[bindings[2].name] = v2;
case 2:
value[bindings[1].name] = v1;
case 1:
value[bindings[0].name] = v0;
}
if (bindLen > 0) value[bindings[0].name] = v0;
if (bindLen > 1) value[bindings[1].name] = v1;
if (bindLen > 2) value[bindings[2].name] = v2;
if (bindLen > 3) value[bindings[3].name] = v3;
if (bindLen > 4) value[bindings[4].name] = v4;
if (bindLen > 5) value[bindings[5].name] = v5;
if (bindLen > 6) value[bindings[6].name] = v6;
if (bindLen > 7) value[bindings[7].name] = v7;
if (bindLen > 8) value[bindings[8].name] = v8;
if (bindLen > 9) value[bindings[9].name] = v9;
break;
case PureExpressionType.Pipe:
const pipe = v0;
switch (bindings.length) {
case 10:
value = pipe.transform(v1, v2, v3, v4, v5, v6, v7, v8, v9);
break;
case 9:
value = pipe.transform(v1, v2, v3, v4, v5, v6, v7, v8);
break;
case 8:
value = pipe.transform(v1, v2, v3, v4, v5, v6, v7);
break;
case 7:
value = pipe.transform(v1, v2, v3, v4, v5, v6);
break;
case 6:
value = pipe.transform(v1, v2, v3, v4, v5);
break;
case 5:
value = pipe.transform(v1, v2, v3, v4);
break;
case 4:
value = pipe.transform(v1, v2, v3);
break;
case 3:
value = pipe.transform(v1, v2);
switch (bindLen) {
case 1:
value = pipe.transform(v0);
break;
case 2:
value = pipe.transform(v1);
break;
case 1:
value = pipe.transform(v0);
case 3:
value = pipe.transform(v1, v2);
break;
case 4:
value = pipe.transform(v1, v2, v3);
break;
case 5:
value = pipe.transform(v1, v2, v3, v4);
break;
case 6:
value = pipe.transform(v1, v2, v3, v4, v5);
break;
case 7:
value = pipe.transform(v1, v2, v3, v4, v5, v6);
break;
case 8:
value = pipe.transform(v1, v2, v3, v4, v5, v6, v7);
break;
case 9:
value = pipe.transform(v1, v2, v3, v4, v5, v6, v7, v8);
break;
case 10:
value = pipe.transform(v1, v2, v3, v4, v5, v6, v7, v8, v9);
break;
}
break;

View File

@ -67,58 +67,32 @@ export function createText(view: ViewData, renderHost: any, def: NodeDef): TextD
export function checkAndUpdateTextInline(
view: ViewData, def: NodeDef, v0: any, v1: any, v2: any, v3: any, v4: any, v5: any, v6: any,
v7: any, v8: any, v9: any) {
const bindings = def.bindings;
let changed = false;
// Note: fallthrough is intended!
switch (bindings.length) {
case 10:
if (checkAndUpdateBinding(view, def, 9, v9)) changed = true;
case 9:
if (checkAndUpdateBinding(view, def, 8, v8)) changed = true;
case 8:
if (checkAndUpdateBinding(view, def, 7, v7)) changed = true;
case 7:
if (checkAndUpdateBinding(view, def, 6, v6)) changed = true;
case 6:
if (checkAndUpdateBinding(view, def, 5, v5)) changed = true;
case 5:
if (checkAndUpdateBinding(view, def, 4, v4)) changed = true;
case 4:
if (checkAndUpdateBinding(view, def, 3, v3)) changed = true;
case 3:
if (checkAndUpdateBinding(view, def, 2, v2)) changed = true;
case 2:
if (checkAndUpdateBinding(view, def, 1, v1)) changed = true;
case 1:
if (checkAndUpdateBinding(view, def, 0, v0)) changed = true;
}
const bindings = def.bindings;
const bindLen = bindings.length;
if (bindLen > 0 && checkAndUpdateBinding(view, def, 0, v0)) changed = true;
if (bindLen > 1 && checkAndUpdateBinding(view, def, 1, v1)) changed = true;
if (bindLen > 2 && checkAndUpdateBinding(view, def, 2, v2)) changed = true;
if (bindLen > 3 && checkAndUpdateBinding(view, def, 3, v3)) changed = true;
if (bindLen > 4 && checkAndUpdateBinding(view, def, 4, v4)) changed = true;
if (bindLen > 5 && checkAndUpdateBinding(view, def, 5, v5)) changed = true;
if (bindLen > 6 && checkAndUpdateBinding(view, def, 6, v6)) changed = true;
if (bindLen > 7 && checkAndUpdateBinding(view, def, 7, v7)) changed = true;
if (bindLen > 8 && checkAndUpdateBinding(view, def, 8, v8)) changed = true;
if (bindLen > 9 && checkAndUpdateBinding(view, def, 9, v9)) changed = true;
if (changed) {
let value = '';
// Note: fallthrough is intended!
switch (bindings.length) {
case 10:
value = _addInterpolationPart(v9, bindings[9]);
case 9:
value = _addInterpolationPart(v8, bindings[8]) + value;
case 8:
value = _addInterpolationPart(v7, bindings[7]) + value;
case 7:
value = _addInterpolationPart(v6, bindings[6]) + value;
case 6:
value = _addInterpolationPart(v5, bindings[5]) + value;
case 5:
value = _addInterpolationPart(v4, bindings[4]) + value;
case 4:
value = _addInterpolationPart(v3, bindings[3]) + value;
case 3:
value = _addInterpolationPart(v2, bindings[2]) + value;
case 2:
value = _addInterpolationPart(v1, bindings[1]) + value;
case 1:
value = _addInterpolationPart(v0, bindings[0]) + value;
}
value = def.text.prefix + value;
let value = def.text.prefix;
if (bindLen > 0) value += _addInterpolationPart(v0, bindings[0]);
if (bindLen > 1) value += _addInterpolationPart(v1, bindings[1]);
if (bindLen > 2) value += _addInterpolationPart(v2, bindings[2]);
if (bindLen > 3) value += _addInterpolationPart(v3, bindings[3]);
if (bindLen > 4) value += _addInterpolationPart(v4, bindings[4]);
if (bindLen > 5) value += _addInterpolationPart(v5, bindings[5]);
if (bindLen > 6) value += _addInterpolationPart(v6, bindings[6]);
if (bindLen > 7) value += _addInterpolationPart(v7, bindings[7]);
if (bindLen > 8) value += _addInterpolationPart(v8, bindings[8]);
if (bindLen > 9) value += _addInterpolationPart(v9, bindings[9]);
const renderNode = asTextData(view, def.index).renderText;
view.renderer.setValue(renderNode, value);
}

View File

@ -431,29 +431,17 @@ function checkNoChangesNodeInline(
view: ViewData, nodeIndex: number, v0: any, v1: any, v2: any, v3: any, v4: any, v5: any,
v6: any, v7: any, v8: any, v9: any): void {
const nodeDef = view.def.nodes[nodeIndex];
// Note: fallthrough is intended!
switch (nodeDef.bindings.length) {
case 10:
checkBindingNoChanges(view, nodeDef, 9, v9);
case 9:
checkBindingNoChanges(view, nodeDef, 8, v8);
case 8:
checkBindingNoChanges(view, nodeDef, 7, v7);
case 7:
checkBindingNoChanges(view, nodeDef, 6, v6);
case 6:
checkBindingNoChanges(view, nodeDef, 5, v5);
case 5:
checkBindingNoChanges(view, nodeDef, 4, v4);
case 4:
checkBindingNoChanges(view, nodeDef, 3, v3);
case 3:
checkBindingNoChanges(view, nodeDef, 2, v2);
case 2:
checkBindingNoChanges(view, nodeDef, 1, v1);
case 1:
checkBindingNoChanges(view, nodeDef, 0, v0);
}
const bindLen = nodeDef.bindings.length;
if (bindLen > 0) checkBindingNoChanges(view, nodeDef, 0, v0);
if (bindLen > 1) checkBindingNoChanges(view, nodeDef, 1, v1);
if (bindLen > 2) checkBindingNoChanges(view, nodeDef, 2, v2);
if (bindLen > 3) checkBindingNoChanges(view, nodeDef, 3, v3);
if (bindLen > 4) checkBindingNoChanges(view, nodeDef, 4, v4);
if (bindLen > 5) checkBindingNoChanges(view, nodeDef, 5, v5);
if (bindLen > 6) checkBindingNoChanges(view, nodeDef, 6, v6);
if (bindLen > 7) checkBindingNoChanges(view, nodeDef, 7, v7);
if (bindLen > 8) checkBindingNoChanges(view, nodeDef, 8, v8);
if (bindLen > 9) checkBindingNoChanges(view, nodeDef, 9, v9);
return nodeDef.type === NodeType.PureExpression ? asPureExpressionData(view, nodeIndex).value :
undefined;
}

View File

@ -8,8 +8,7 @@
"moduleResolution": "node",
"outDir": "../dist/all/",
"noImplicitAny": true,
// Attention: This is only set to false for @angular/core.
"noFallthroughCasesInSwitch": false,
"noFallthroughCasesInSwitch": true,
"paths": {
"selenium-webdriver": ["../node_modules/@types/selenium-webdriver/index.d.ts"],
"rxjs/*": ["../node_modules/rxjs/*"],