Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

perf(ngmodel/compile): improve ngmodel and general compile/controller performance (#9609) #9772

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions benchmarks/largetable-bp/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<div>interpolation + fnInvocation: <input type="radio" ng-model="benchmarkType" value="interpolationFn"></div>
<div>ngBind + filter: <input type="radio" ng-model="benchmarkType" value="ngBindFilter"></div>
<div>interpolation + filter: <input type="radio" ng-model="benchmarkType" value="interpolationFilter"></div>
<div>ngModel: <input type="radio" ng-model="benchmarkType" value="ngModel"></div>

<ng-switch on="benchmarkType">
<baseline-binding-table ng-switch-when="baselineBinding">
Expand Down Expand Up @@ -84,6 +85,13 @@ <h2>interpolation with filter</h2>
<span ng-repeat="column in row">{{column.i | noop}}:{{column.j | noop}}|</span>
</div>
</div>
<div ng-switch-when="ngModel">
<h2>ngModels</h2>
<div ng-repeat="row in data">
<input type="text" ng-model="row.i" name="constName" />
<input type="text" ng-model="row.j" />
</div>
</div>
</ng-switch>
</div>
</div>
Expand Down
141 changes: 69 additions & 72 deletions src/ng/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -1614,7 +1614,6 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
var terminalPriority = -Number.MAX_VALUE,
newScopeDirective,
controllerDirectives = previousCompileContext.controllerDirectives,
controllers,
newIsolateScopeDirective = previousCompileContext.newIsolateScopeDirective,
templateDirective = previousCompileContext.templateDirective,
nonTlbTranscludeDirective = previousCompileContext.nonTlbTranscludeDirective,
Expand Down Expand Up @@ -1672,7 +1671,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {

if (!directive.templateUrl && directive.controller) {
directiveValue = directive.controller;
controllerDirectives = controllerDirectives || {};
controllerDirectives = controllerDirectives || createMap();
assertNoDuplicate("'" + directiveName + "' controller",
controllerDirectives[directiveName], directive, $compileNode);
controllerDirectives[directiveName] = directive;
Expand Down Expand Up @@ -1840,49 +1839,75 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {


function getControllers(directiveName, require, $element, elementControllers) {
var value, retrievalMethod = 'data', optional = false;
var $searchElement = $element;
var match;
if (isString(require)) {
match = require.match(REQUIRE_PREFIX_REGEXP);
require = require.substring(match[0].length);

if (match[3]) {
if (match[1]) match[3] = null;
else match[1] = match[3];
}
if (match[1] === '^') {
retrievalMethod = 'inheritedData';
} else if (match[1] === '^^') {
retrievalMethod = 'inheritedData';
$searchElement = $element.parent();
}
if (match[2] === '?') {
optional = true;
}
var i, value;

value = null;
if (typeof require === 'string') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty minor, but using isString() is more readable imo.

var match = require.match(REQUIRE_PREFIX_REGEXP);
var name = require.substring(match[0].length);
var type = match[1] || match[3];

if (elementControllers && retrievalMethod === 'data') {
if (value = elementControllers[require]) {
value = value.instance;
}
//If only parents then start at the parent element
//Otherwise attempt getting the controller from elementControllers to avoid .data
if (type === '^^') {
$element = $element.parent();
} else {
value = elementControllers && elementControllers[name];
value = value && value.instance;
}

if (!value) {
var dataName = '$' + name + 'Controller';
value = type ? $element.inheritedData(dataName) : $element.data(dataName);
}
value = value || $searchElement[retrievalMethod]('$' + require + 'Controller');

if (!value && !optional) {
if (!value && match[2] !== '?') {
throw $compileMinErr('ctreq',
"Controller '{0}', required by directive '{1}', can't be found!",
require, directiveName);
name, directiveName);
}

return value || null;
} else if (isArray(require)) {
value = [];
forEach(require, function(require) {
value.push(getControllers(directiveName, require, $element, elementControllers));
});
}
return value;

if (isArray(require)) {
value = new Array(i = require.length);
while (i--) {
value[i] = getControllers(directiveName, require[i], $element, elementControllers);
}
return value;
}
}

function setupControllers(scope, isolateScope, $element, attrs, transcludeFn, elementControllers) {
// For directives with element transclusion the element is a comment,
// but jQuery .data doesn't support attaching data to comment nodes as it's hard to
// clean up (http://bugs.jquery.com/ticket/8335).
// Instead, we save the controllers for the element in a local hash and attach to .data
// later, once we have the actual element.
var controllerData = !hasElementTranscludeDirective && $element.data();

for (var directiveName in controllerDirectives) {
var directive = controllerDirectives[directiveName];

var locals = {
$scope: directive === newIsolateScopeDirective || directive.$$isolateScope ? isolateScope : scope,
$element: $element,
$attrs: attrs,
$transclude: transcludeFn
};

var directiveController = directive.controller;
if (directiveController === '@') {
directiveController = attrs[directive.name];
}

var controllerInstance = $controller(directiveController, locals, true, directive.controllerAs);

elementControllers[directive.name] = controllerInstance;
if (controllerData) {
controllerData['$' + directive.name + 'Controller'] = controllerInstance.instance;
}
}
}


Expand Down Expand Up @@ -1911,36 +1936,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
}

if (controllerDirectives) {
// TODO: merge `controllers` and `elementControllers` into single object.
controllers = {};
elementControllers = {};
forEach(controllerDirectives, function(directive) {
var locals = {
$scope: directive === newIsolateScopeDirective || directive.$$isolateScope ? isolateScope : scope,
$element: $element,
$attrs: attrs,
$transclude: transcludeFn
}, controllerInstance;

controller = directive.controller;
if (controller == '@') {
controller = attrs[directive.name];
}

controllerInstance = $controller(controller, locals, true, directive.controllerAs);

// For directives with element transclusion the element is a comment,
// but jQuery .data doesn't support attaching data to comment nodes as it's hard to
// clean up (http://bugs.jquery.com/ticket/8335).
// Instead, we save the controllers for the element in a local hash and attach to .data
// later, once we have the actual element.
elementControllers[directive.name] = controllerInstance;
if (!hasElementTranscludeDirective) {
$element.data('$' + directive.name + 'Controller', controllerInstance.instance);
}

controllers[directive.name] = controllerInstance;
});
setupControllers(scope, isolateScope, $element, attrs, transcludeFn, elementControllers = createMap());
}

if (newIsolateScopeDirective) {
Expand All @@ -1954,14 +1950,14 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
isolateScope.$$isolateBindings,
newIsolateScopeDirective, isolateScope);
}
if (controllers) {
if (elementControllers) {
// Initialize bindToController bindings for new/isolate scopes
var scopeDirective = newIsolateScopeDirective || newScopeDirective;
var bindings;
var controllerForBindings;
if (scopeDirective && controllers[scopeDirective.name]) {
if (scopeDirective && elementControllers[scopeDirective.name]) {
bindings = scopeDirective.$$bindings.bindToController;
controller = controllers[scopeDirective.name];
controller = elementControllers[scopeDirective.name];

if (controller && controller.identifier && bindings) {
controllerForBindings = controller;
Expand All @@ -1970,7 +1966,9 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
bindings, scopeDirective);
}
}
forEach(controllers, function(controller) {
// Initialize the controllers before linking
for (i in elementControllers) {
controller = elementControllers[i];
var result = controller();
if (result !== controller.instance &&
controller === controllerForBindings) {
Expand All @@ -1980,8 +1978,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
initializeDirectiveBindings(scope, attrs, result,
bindings, scopeDirective);
}
});
controllers = null;
}
}

// PRELINKING
Expand Down