Skip to content

Commit

Permalink
fixup! feat(ivy): memoize array literals in render3
Browse files Browse the repository at this point in the history
  • Loading branch information
kara committed Feb 1, 2018
1 parent b4793f2 commit a16c5db
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 61 deletions.
10 changes: 5 additions & 5 deletions packages/core/src/render3/instructions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,8 @@ export function createTView(): TView {
contentCheckHooks: null,
viewHooks: null,
viewCheckHooks: null,
destroyHooks: null
destroyHooks: null,
objectLiterals: null
};
}

Expand Down Expand Up @@ -698,8 +699,7 @@ function createTNode(
initialInputs: undefined,
inputs: undefined,
outputs: undefined,
data: data,
objectLiterals: null
data: data
};
}

Expand Down Expand Up @@ -1864,8 +1864,8 @@ export function getRenderer(): Renderer3 {
return renderer;
}

export function getTNode(index: number): TNode {
return tData[index] as TNode;
export function getTView(): TView {
return currentView.tView;
}

export function getDirectiveInstance<T>(instanceOrArray: T | [T]): T {
Expand Down
7 changes: 0 additions & 7 deletions packages/core/src/render3/interfaces/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,13 +274,6 @@ export interface TNode {
* If this TNode corresponds to an LElementNode, data will be null.
*/
data: TContainer|null;

/**
* Contains copies of array literals that were passed as bindings for this node.
*
* Note on name: will soon contain both object and array literals.
*/
objectLiterals: any[][]|null;
}

/** Static data for an LElementNode */
Expand Down
7 changes: 7 additions & 0 deletions packages/core/src/render3/interfaces/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,13 @@ export interface TView {
* Odd indices: Hook function
*/
destroyHooks: HookData|null;

/**
* Contains copies of array literals that were passed as bindings in this view.
*
* Note on name: will soon contain both object and array literals.
*/
objectLiterals: any[][]|null;
}

/**
Expand Down
71 changes: 35 additions & 36 deletions packages/core/src/render3/object_literal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

import {NO_CHANGE, bind, getTNode} from './instructions';
import {NO_CHANGE, bind, getTView} from './instructions';


/**
Expand Down Expand Up @@ -41,12 +41,12 @@ function updateBinding4(
}


function getObjectCopy(nodeIndex: number, propIndex: number, arr: any[]): any[] {
const tNode = getTNode(nodeIndex);
const objectLiterals = tNode.objectLiterals;
return objectLiterals && propIndex < objectLiterals.length ?
objectLiterals[propIndex] :
(objectLiterals || (tNode.objectLiterals = []))[propIndex] = arr.slice();
function getObjectCopy(index: number, arr: any[]): any[] {
const tView = getTView();
const objectLiterals = tView.objectLiterals;
return objectLiterals && index < objectLiterals.length ?
objectLiterals[index] :
(objectLiterals || (tView.objectLiterals = []))[index] = arr.slice();
}

/**
Expand All @@ -58,9 +58,9 @@ function getObjectCopy(nodeIndex: number, propIndex: number, arr: any[]): any[]
* @param exp Expression to set at index
* @returns A copy of the array or NO_CHANGE
*/
export function objectLiteral1(
nodeIndex: number, propIndex: number, arr: any[], index: number, exp: any): any[]|NO_CHANGE {
arr = getObjectCopy(nodeIndex, propIndex, arr);
export function objectLiteral1(objectIndex: number, arr: any[], index: number, exp: any): any[]|
NO_CHANGE {
arr = getObjectCopy(objectIndex, arr);
if (bind(exp) === NO_CHANGE) {
return NO_CHANGE;
} else {
Expand All @@ -81,9 +81,9 @@ export function objectLiteral1(
* @returns A copy of the array or NO_CHANGE
*/
export function objectLiteral2(
nodeIndex: number, propIndex: number, arr: any[], index1: number, exp1: any, index2: number,
exp2: any): any[]|NO_CHANGE {
arr = getObjectCopy(nodeIndex, propIndex, arr);
objectIndex: number, arr: any[], index1: number, exp1: any, index2: number, exp2: any): any[]|
NO_CHANGE {
arr = getObjectCopy(objectIndex, arr);
return updateBinding2(arr, index1, exp1, index2, exp2) ? arr.slice() : NO_CHANGE;
}

Expand All @@ -100,9 +100,9 @@ export function objectLiteral2(
* @returns A copy of the array or NO_CHANGE
*/
export function objectLiteral3(
nodeIndex: number, propIndex: number, arr: any[], index1: number, exp1: any, index2: number,
exp2: any, index3: number, exp3: any): any[]|NO_CHANGE {
arr = getObjectCopy(nodeIndex, propIndex, arr);
objectIndex: number, arr: any[], index1: number, exp1: any, index2: number, exp2: any,
index3: number, exp3: any): any[]|NO_CHANGE {
arr = getObjectCopy(objectIndex, arr);
let different = updateBinding2(arr, index1, exp1, index2, exp2);
return updateArrBinding(arr, index3, exp3) || different ? arr.slice() : NO_CHANGE;
}
Expand All @@ -122,9 +122,9 @@ export function objectLiteral3(
* @returns A copy of the array or NO_CHANGE
*/
export function objectLiteral4(
nodeIndex: number, propIndex: number, arr: any[], index1: number, exp1: any, index2: number,
exp2: any, index3: number, exp3: any, index4: number, exp4: any): any[]|NO_CHANGE {
arr = getObjectCopy(nodeIndex, propIndex, arr);
objectIndex: number, arr: any[], index1: number, exp1: any, index2: number, exp2: any,
index3: number, exp3: any, index4: number, exp4: any): any[]|NO_CHANGE {
arr = getObjectCopy(objectIndex, arr);
return updateBinding4(arr, index1, exp1, index2, exp2, index3, exp3, index4, exp4) ? arr.slice() :
NO_CHANGE;
}
Expand All @@ -146,10 +146,10 @@ export function objectLiteral4(
* @returns A copy of the array or NO_CHANGE
*/
export function objectLiteral5(
nodeIndex: number, propIndex: number, arr: any[], index1: number, exp1: any, index2: number,
exp2: any, index3: number, exp3: any, index4: number, exp4: any, index5: number,
exp5: any): any[]|NO_CHANGE {
arr = getObjectCopy(nodeIndex, propIndex, arr);
objectIndex: number, arr: any[], index1: number, exp1: any, index2: number, exp2: any,
index3: number, exp3: any, index4: number, exp4: any, index5: number, exp5: any): any[]|
NO_CHANGE {
arr = getObjectCopy(objectIndex, arr);
let different = updateBinding4(arr, index1, exp1, index2, exp2, index3, exp3, index4, exp4);
return updateArrBinding(arr, index5, exp5) || different ? arr.slice() : NO_CHANGE;
}
Expand All @@ -173,10 +173,10 @@ export function objectLiteral5(
* @returns A copy of the array or NO_CHANGE
*/
export function objectLiteral6(
nodeIndex: number, propIndex: number, arr: any[], index1: number, exp1: any, index2: number,
exp2: any, index3: number, exp3: any, index4: number, exp4: any, index5: number, exp5: any,
index6: number, exp6: any): any[]|NO_CHANGE {
arr = getObjectCopy(nodeIndex, propIndex, arr);
objectIndex: number, arr: any[], index1: number, exp1: any, index2: number, exp2: any,
index3: number, exp3: any, index4: number, exp4: any, index5: number, exp5: any, index6: number,
exp6: any): any[]|NO_CHANGE {
arr = getObjectCopy(objectIndex, arr);
let different = updateBinding4(arr, index1, exp1, index2, exp2, index3, exp3, index4, exp4);
return updateBinding2(arr, index5, exp5, index6, exp6) || different ? arr.slice() : NO_CHANGE;
}
Expand All @@ -202,10 +202,10 @@ export function objectLiteral6(
* @returns A copy of the array or NO_CHANGE
*/
export function objectLiteral7(
nodeIndex: number, propIndex: number, arr: any[], index1: number, exp1: any, index2: number,
exp2: any, index3: number, exp3: any, index4: number, exp4: any, index5: number, exp5: any,
index6: number, exp6: any, index7: number, exp7: any): any[]|NO_CHANGE {
arr = getObjectCopy(nodeIndex, propIndex, arr);
objectIndex: number, arr: any[], index1: number, exp1: any, index2: number, exp2: any,
index3: number, exp3: any, index4: number, exp4: any, index5: number, exp5: any, index6: number,
exp6: any, index7: number, exp7: any): any[]|NO_CHANGE {
arr = getObjectCopy(objectIndex, arr);
let different = updateBinding4(arr, index1, exp1, index2, exp2, index3, exp3, index4, exp4);
different = updateBinding2(arr, index5, exp5, index6, exp6) || different;
return updateArrBinding(arr, index7, exp7) || different ? arr.slice() : NO_CHANGE;
Expand Down Expand Up @@ -234,11 +234,10 @@ export function objectLiteral7(
* @returns A copy of the array or NO_CHANGE
*/
export function objectLiteral8(
nodeIndex: number, propIndex: number, arr: any[], index1: number, exp1: any, index2: number,
exp2: any, index3: number, exp3: any, index4: number, exp4: any, index5: number, exp5: any,
index6: number, exp6: any, index7: number, exp7: any, index8: number, exp8: any): any[]|
NO_CHANGE {
arr = getObjectCopy(nodeIndex, propIndex, arr);
objectIndex: number, arr: any[], index1: number, exp1: any, index2: number, exp2: any,
index3: number, exp3: any, index4: number, exp4: any, index5: number, exp5: any, index6: number,
exp6: any, index7: number, exp7: any, index8: number, exp8: any): any[]|NO_CHANGE {
arr = getObjectCopy(objectIndex, arr);
let different = updateBinding4(arr, index1, exp1, index2, exp2, index3, exp3, index4, exp4);
return updateBinding4(arr, index5, exp5, index6, exp6, index7, exp7, index8, exp8) || different ?
arr.slice() :
Expand Down
2 changes: 1 addition & 1 deletion packages/core/test/render3/compiler_canonical_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ describe('compiler specification', () => {
r3.E(0, MyComp);
r3.e();
}
r3.p(0, 'names', r3.o1(0, 0, e0_literal, 1, ctx.customName));
r3.p(0, 'names', r3.o1(0, e0_literal, 1, ctx.customName));
MyComp.ngComponentDef.h(1, 0);
r3.r(1, 0);
}
Expand Down
1 change: 0 additions & 1 deletion packages/core/test/render3/node_selector_matcher_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ function testLStaticData(tagName: string, attrs: string[] | null): TNode {
inputs: undefined,
outputs: undefined,
data: null,
objectLiterals: null
};
}

Expand Down
22 changes: 11 additions & 11 deletions packages/core/test/render3/object_literal_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('array literals', () => {
E(0, MyComp);
e();
}
p(0, 'names', o1(0, 0, e0_literal, 1, ctx.customName));
p(0, 'names', o1(0, e0_literal, 1, ctx.customName));
MyComp.ngComponentDef.h(1, 0);
r(1, 0);
}
Expand Down Expand Up @@ -77,8 +77,8 @@ describe('array literals', () => {
E(0, ManyPropComp);
e();
}
p(0, 'names1', o1(0, 0, e0_literal, 1, ctx.customName));
p(0, 'names2', o1(0, 1, e0_literal_1, 0, ctx.customName2));
p(0, 'names1', o1(0, e0_literal, 1, ctx.customName));
p(0, 'names2', o1(1, e0_literal_1, 0, ctx.customName2));
ManyPropComp.ngComponentDef.h(1, 0);
r(1, 0);
}
Expand Down Expand Up @@ -107,7 +107,7 @@ describe('array literals', () => {
E(0, MyComp);
e();
}
p(0, 'names', o2(0, 0, e0_literal, 1, ctx.customName, 3, ctx.customName2));
p(0, 'names', o2(0, e0_literal, 1, ctx.customName, 3, ctx.customName2));
MyComp.ngComponentDef.h(1, 0);
r(1, 0);
}
Expand Down Expand Up @@ -159,14 +159,14 @@ describe('array literals', () => {
o8Comp = m(11);
e();
}
p(0, 'names', o3(0, 0, e0_literal, 5, c[5], 6, c[6], 7, c[7]));
p(2, 'names', o4(2, 0, e2_literal, 4, c[4], 5, c[5], 6, c[6], 7, c[7]));
p(4, 'names', o5(4, 0, e4_literal, 3, c[3], 4, c[4], 5, c[5], 6, c[6], 7, c[7]));
p(6, 'names', o6(6, 0, e6_literal, 2, c[2], 3, c[3], 4, c[4], 5, c[5], 6, c[6], 7, c[7]));
p(0, 'names', o3(0, e0_literal, 5, c[5], 6, c[6], 7, c[7]));
p(2, 'names', o4(1, e2_literal, 4, c[4], 5, c[5], 6, c[6], 7, c[7]));
p(4, 'names', o5(2, e4_literal, 3, c[3], 4, c[4], 5, c[5], 6, c[6], 7, c[7]));
p(6, 'names', o6(3, e6_literal, 2, c[2], 3, c[3], 4, c[4], 5, c[5], 6, c[6], 7, c[7]));
p(8, 'names',
o7(8, 0, e8_literal, 1, c[1], 2, c[2], 3, c[3], 4, c[4], 5, c[5], 6, c[6], 7, c[7]));
p(10, 'names', o8(10, 0, e10_literal, 0, c[0], 1, c[1], 2, c[2], 3, c[3], 4, c[4], 5, c[5], 6,
c[6], 7, c[7]));
o7(4, e8_literal, 1, c[1], 2, c[2], 3, c[3], 4, c[4], 5, c[5], 6, c[6], 7, c[7]));
p(10, 'names',
o8(5, e10_literal, 0, c[0], 1, c[1], 2, c[2], 3, c[3], 4, c[4], 5, c[5], 6, c[6], 7, c[7]));
MyComp.ngComponentDef.h(1, 0);
r(1, 0);
MyComp.ngComponentDef.h(3, 2);
Expand Down

0 comments on commit a16c5db

Please sign in to comment.