Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache match elements to speed up compiler #1831

Merged
merged 2 commits into from
Feb 1, 2014
Merged
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
56 changes: 41 additions & 15 deletions lib/less/tree/selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,33 +32,59 @@ tree.Selector.prototype = {
match: function (other) {
var elements = this.elements,
len = elements.length,
oelements, olen, i;

oelements = other.elements.map( function(v) {
return v.combinator.value + (v.value.value || v.value);
}).join("").match(/[,&#\.\w-]([\w-]|(\\.))*/g);
// ^ regexp could be more simple but see test/less/css-escapes.less:17, doh!

if (!oelements) {
return 0;
}
olen, i;

if (oelements[0] === "&") {
oelements.shift();
}
other.CacheElements();

olen = oelements.length;
olen = other._elements.length;
if (olen === 0 || len < olen) {
return 0;
} else {
for (i = 0; i < olen; i++) {
if (elements[i].value !== oelements[i]) {
if (elements[i].value !== other._elements[i]) {
return 0;
}
}
}

return olen; // return number of matched elements
},
CacheElements: function(){
var css = '', len, v, i;

if( !this._elements ){

len = this.elements.length;
for(i = 0; i < len; i++){
Copy link
Member

Choose a reason for hiding this comment

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

Btw, var i is not declared here (so grunt test fails).


v = this.elements[i];
css += v.combinator.value;

if( !v.value.value ){
css += v.value;
continue;
}

if( typeof v.value.value !== "string" ){
Copy link
Member

Choose a reason for hiding this comment

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

This make sense. But unfortunately there're too many possible value types beyond a string (e.g. an array, an expression etc.) so it still remains broken for "complex" variable values... Ideally we should use element.genCSS() instead of simple element.value expansion for interpolated things everywhere.


I was also thinking of something like that (i.e. caching + proper @{var} expansion) but I thought it would make sense to fix #1196 first which means this whole thing to be completely rewritten from scratch (because we will need to switch from the "element by element" to a "string/substring" matching method or a sort of).

css = '';
break;
}
css += v.value.value;
}

this._elements = css.match(/[,&#\.\w-]([\w-]|(\\.))*/g);

if (this._elements) {
if (this._elements[0] === "&") {
this._elements.shift();
}

}else{
this._elements = [];
}

}
},
eval: function (env) {
var evaldCondition = this.condition && this.condition.eval(env),
elements = this.elements, extendList = this.extendList;
Expand Down