Skip to content

Commit

Permalink
Merge pull request #2686 from Polymer/micro-perf
Browse files Browse the repository at this point in the history
Micro-optimizations to Polymer's core performance
  • Loading branch information
kevinpschaaf committed Nov 12, 2015
2 parents b15e5b9 + 061e1b3 commit cb68ce5
Show file tree
Hide file tree
Showing 47 changed files with 1,266 additions and 546 deletions.
4 changes: 2 additions & 2 deletions polymer-micro.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@
_registerFeatures: function() {
// identity
this._prepIs();
// attributes
this._prepAttributes();
// shared behaviors
this._prepBehaviors();
// factory
this._prepConstructor();
// fast access to property info
this._prepPropertyInfo();
},

_prepBehavior: function(b) {
Expand Down
23 changes: 13 additions & 10 deletions polymer-mini.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
_registerFeatures: function() {
// identity
this._prepIs();
// attributes
this._prepAttributes();
// shared behaviors
this._prepBehaviors();
// factory
Expand All @@ -34,21 +32,26 @@
this._prepTemplate();
// dom encapsulation
this._prepShady();
// fast access to property info
this._prepPropertyInfo();
},

_prepBehavior: function(b) {
this._addHostAttributes(b.hostAttributes);
},

_initFeatures: function() {
// manage local dom
this._poolContent();
// host stack
this._pushHost();
// instantiate template
this._stampTemplate();
// host stack
this._popHost();
this._registerHost();
if (this._template) {
// manage local dom
this._poolContent();
// host stack
this._beginHosting();
// instantiate template
this._stampTemplate();
// host stack
this._endHosting();
}
// install host attributes
this._marshalHostAttributes();
// setup debouncers
Expand Down
41 changes: 26 additions & 15 deletions polymer.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
_registerFeatures: function() {
// identity
this._prepIs();
// attributes
this._prepAttributes();
// factory
this._prepConstructor();
// template
Expand All @@ -44,6 +42,8 @@
this._prepEffects();
// shared behaviors
this._prepBehaviors();
// fast access to property info
this._prepPropertyInfo();
// accessors part 2
this._prepBindings();
// dom encapsulation
Expand All @@ -57,28 +57,37 @@
},

_initFeatures: function() {
// manage local dom
this._poolContent();
// manage configuration
this._setupConfigure();
// setup style properties
this._setupStyleProperties();
// host stack
this._pushHost();
// instantiate template
this._stampTemplate();
// host stack
this._popHost();
// concretize template references
this._marshalAnnotationReferences();
// setup debouncers
this._setupDebouncers();
this._registerHost();
if (this._template) {
// manage local dom
this._poolContent();
// host stack
this._beginHosting();
// instantiate template
this._stampTemplate();
// host stack
this._endHosting();
// concretize template references
this._marshalAnnotationReferences();
}
// concretize effects on instance
this._marshalInstanceEffects();
// install host attributes
this._marshalHostAttributes();
// acquire instance behaviors
this._marshalBehaviors();
/*
TODO(sorvell): It's *slightly() more efficient to marshal attributes prior
to installing hostAttributes, but then hostAttributes must be separately
funneled to configure, which is cumbersome.
Since this delta seems hard to measure we will not bother atm.
*/
// install host attributes
this._marshalHostAttributes();
// acquire initial instance attribute values
this._marshalAttributes();
// top-down initial distribution, configuration, & ready callback
Expand All @@ -87,7 +96,9 @@

_marshalBehavior: function(b) {
// establish listeners on instance
this._listenListeners(b.listeners);
if (b.listeners) {
this._listenListeners(b.listeners);
}
}

});
Expand Down
43 changes: 30 additions & 13 deletions src/lib/annotations/annotations.html
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,18 @@
parseAnnotations: function(template) {
var list = [];
var content = template._content || template.content;
this._parseNodeAnnotations(content, list);
this._parseNodeAnnotations(content, list,
template.hasAttribute('strip-whitespace'));
return list;
},

// add annotations gleaned from subtree at `node` to `list`
_parseNodeAnnotations: function(node, list) {
_parseNodeAnnotations: function(node, list, stripWhiteSpace) {
return node.nodeType === Node.TEXT_NODE ?
this._parseTextNodeAnnotation(node, list) :
// TODO(sjmiles): are there other nodes we may encounter
// that are not TEXT_NODE but also not ELEMENT?
this._parseElementAnnotations(node, list);
this._parseElementAnnotations(node, list, stripWhiteSpace);
},

_bindingRegex: /([^{[]*)(\{\{|\[\[)(?!\}\}|\]\])(.+?)(?:\]\]|\}\})/g,
Expand Down Expand Up @@ -181,15 +182,15 @@
},

// add annotations gleaned from Element `node` to `list`
_parseElementAnnotations: function(element, list) {
_parseElementAnnotations: function(element, list, stripWhiteSpace) {
var annote = {
bindings: [],
events: []
};
if (element.localName === 'content') {
list._hasContent = true;
}
this._parseChildNodesAnnotations(element, annote, list);
this._parseChildNodesAnnotations(element, annote, list, stripWhiteSpace);
// TODO(sjmiles): is this for non-ELEMENT nodes? If so, we should
// change the contract of this method, or filter these out above.
if (element.attributes) {
Expand All @@ -210,9 +211,12 @@

// add annotations gleaned from children of `root` to `list`, `root`'s
// `annote` is supplied as it is the annote.parent of added annotations
_parseChildNodesAnnotations: function(root, annote, list, callback) {
_parseChildNodesAnnotations: function(root, annote, list, stripWhiteSpace) {
if (root.firstChild) {
for (var i=0, node=root.firstChild; node; node=node.nextSibling, i++) {
var node = root.firstChild;
var i = 0;
while (node) {
var next = node.nextSibling;
if (node.localName === 'template' &&
!node.hasAttribute('preserve-content')) {
this._parseTemplate(node, i, list, annote);
Expand All @@ -222,18 +226,31 @@
// note that root.normalize() should work but does not so we do this
// manually.
if (node.nodeType === Node.TEXT_NODE) {
var n = node.nextSibling;
var n = next;
while (n && (n.nodeType === Node.TEXT_NODE)) {
node.textContent += n.textContent;
next = n.nextSibling;
root.removeChild(n);
n = n.nextSibling;
n = next;
}
// optionally strip whitespace
if (stripWhiteSpace && !node.textContent.trim()) {
root.removeChild(node);
// decrement index since node is removed
i--;
}
}
var childAnnotation = this._parseNodeAnnotations(node, list, callback);
if (childAnnotation) {
childAnnotation.parent = annote;
childAnnotation.index = i;
// if this node didn't get evacipated, parse it.
if (node.parentNode) {
var childAnnotation = this._parseNodeAnnotations(node, list,
stripWhiteSpace);
if (childAnnotation) {
childAnnotation.parent = annote;
childAnnotation.index = i;
}
}
node = next;
i++;
}
}
},
Expand Down
18 changes: 11 additions & 7 deletions src/lib/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@
// https://code.google.com/p/chromium/issues/detail?id=516550
// To allow querying style/layout data in attached, we defer it
// until we are sure rendering is ready.
var self = this;
Polymer.RenderStatus.whenReady(function() {
this.isAttached = true;
this._doBehavior('attached'); // abstract
}.bind(this));
self.isAttached = true;
self._doBehavior('attached'); // abstract
});
},

// reserved for canonical behavior
Expand All @@ -57,9 +58,11 @@
},

// reserved for canonical behavior
attributeChangedCallback: function(name) {
attributeChangedCallback: function(name, oldValue, newValue) {
// TODO(sorvell): consider filtering out changes to host attributes
// note: this was barely measurable with 3 host attributes.
this._attributeChangedImpl(name); // abstract
this._doBehavior('attributeChanged', arguments); // abstract
this._doBehavior('attributeChanged', [name, oldValue, newValue]); // abstract
},

_attributeChangedImpl: function(name) {
Expand All @@ -77,9 +80,10 @@
*/
extend: function(prototype, api) {
if (prototype && api) {
Object.getOwnPropertyNames(api).forEach(function(n) {
var n$ = Object.getOwnPropertyNames(api);
for (var i=0, n; (i<n$.length) && (n=n$[i]); i++) {
this.copyOwnProperty(n, api, prototype);
}, this);
}
}
return prototype || api;
},
Expand Down

0 comments on commit cb68ce5

Please sign in to comment.