refactor(core): don’t use switch fall through but rather multiple `if`s

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( export function checkAndUpdateElementInline(
view: ViewData, def: NodeDef, v0: any, v1: any, v2: any, v3: any, v4: any, v5: any, v6: any, view: ViewData, def: NodeDef, v0: any, v1: any, v2: any, v3: any, v4: any, v5: any, v6: any,
v7: any, v8: any, v9: any) { v7: any, v8: any, v9: any) {
// Note: fallthrough is intended! const bindLen = def.bindings.length;
switch (def.bindings.length) { if (bindLen > 0) checkAndUpdateElementValue(view, def, 0, v0);
case 10: if (bindLen > 1) checkAndUpdateElementValue(view, def, 1, v1);
checkAndUpdateElementValue(view, def, 9, v9); if (bindLen > 2) checkAndUpdateElementValue(view, def, 2, v2);
case 9: if (bindLen > 3) checkAndUpdateElementValue(view, def, 3, v3);
checkAndUpdateElementValue(view, def, 8, v8); if (bindLen > 4) checkAndUpdateElementValue(view, def, 4, v4);
case 8: if (bindLen > 5) checkAndUpdateElementValue(view, def, 5, v5);
checkAndUpdateElementValue(view, def, 7, v7); if (bindLen > 6) checkAndUpdateElementValue(view, def, 6, v6);
case 7: if (bindLen > 7) checkAndUpdateElementValue(view, def, 7, v7);
checkAndUpdateElementValue(view, def, 6, v6); if (bindLen > 8) checkAndUpdateElementValue(view, def, 8, v8);
case 6: if (bindLen > 9) checkAndUpdateElementValue(view, def, 9, v9);
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);
}
} }
export function checkAndUpdateElementDynamic(view: ViewData, def: NodeDef, values: any[]) { 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 providerData = asProviderData(view, def.index);
const directive = providerData.instance; const directive = providerData.instance;
let changes: SimpleChanges; let changes: SimpleChanges;
// Note: fallthrough is intended! const bindLen = def.bindings.length;
switch (def.bindings.length) { if (bindLen > 0) changes = checkAndUpdateProp(view, providerData, def, 0, v0, changes);
case 10: if (bindLen > 1) changes = checkAndUpdateProp(view, providerData, def, 1, v1, changes);
changes = checkAndUpdateProp(view, providerData, def, 9, v9, changes); if (bindLen > 2) changes = checkAndUpdateProp(view, providerData, def, 2, v2, changes);
case 9: if (bindLen > 3) changes = checkAndUpdateProp(view, providerData, def, 3, v3, changes);
changes = checkAndUpdateProp(view, providerData, def, 8, v8, changes); if (bindLen > 4) changes = checkAndUpdateProp(view, providerData, def, 4, v4, changes);
case 8: if (bindLen > 5) changes = checkAndUpdateProp(view, providerData, def, 5, v5, changes);
changes = checkAndUpdateProp(view, providerData, def, 7, v7, changes); if (bindLen > 6) changes = checkAndUpdateProp(view, providerData, def, 6, v6, changes);
case 7: if (bindLen > 7) changes = checkAndUpdateProp(view, providerData, def, 7, v7, changes);
changes = checkAndUpdateProp(view, providerData, def, 6, v6, changes); if (bindLen > 8) changes = checkAndUpdateProp(view, providerData, def, 8, v8, changes);
case 6: if (bindLen > 9) changes = checkAndUpdateProp(view, providerData, def, 9, v9, changes);
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);
}
if (changes) { if (changes) {
directive.ngOnChanges(changes); directive.ngOnChanges(changes);
} }

View File

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

View File

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

View File

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