mirror of
https://github.com/gorhill/uBlock.git
synced 2024-09-05 18:49:39 +02:00
speed boost for processing high-high generics
This commit is contained in:
parent
c28568f9b0
commit
fc981aef29
@ -93,25 +93,22 @@ var messager = vAPI.messaging.channel('contentscript-end.js');
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//var tStart = window.performance.now();
|
||||||
|
|
||||||
var queriedSelectors = {};
|
var queriedSelectors = {};
|
||||||
var injectedSelectors = {};
|
var injectedSelectors = {};
|
||||||
var classSelectors = null;
|
var lowGenericSelectors = [];
|
||||||
var idSelectors = null;
|
|
||||||
var highGenerics = null;
|
var highGenerics = null;
|
||||||
var contextNodes = [document];
|
var contextNodes = [document];
|
||||||
var nullArray = { push: function(){} };
|
var nullArray = { push: function(){} };
|
||||||
|
|
||||||
var retrieveGenericSelectors = function() {
|
var retrieveGenericSelectors = function() {
|
||||||
var selectors = classSelectors !== null ? Object.keys(classSelectors) : [];
|
if ( lowGenericSelectors.length > 0 || highGenerics === null ) {
|
||||||
if ( idSelectors !== null ) {
|
//console.log('µBlock> ABP cosmetic filters: retrieving CSS rules using %d selectors', lowGenericSelectors.length);
|
||||||
selectors = selectors.concat(idSelectors);
|
|
||||||
}
|
|
||||||
if ( selectors.length > 0 || highGenerics === null ) {
|
|
||||||
//console.log('µBlock> ABP cosmetic filters: retrieving CSS rules using %d selectors', selectors.length);
|
|
||||||
messager.send({
|
messager.send({
|
||||||
what: 'retrieveGenericCosmeticSelectors',
|
what: 'retrieveGenericCosmeticSelectors',
|
||||||
pageURL: window.location.href,
|
pageURL: window.location.href,
|
||||||
selectors: selectors,
|
selectors: lowGenericSelectors,
|
||||||
highGenerics: highGenerics === null
|
highGenerics: highGenerics === null
|
||||||
},
|
},
|
||||||
retrieveHandler
|
retrieveHandler
|
||||||
@ -122,8 +119,7 @@ var messager = vAPI.messaging.channel('contentscript-end.js');
|
|||||||
} else {
|
} else {
|
||||||
otherRetrieveHandler(null);
|
otherRetrieveHandler(null);
|
||||||
}
|
}
|
||||||
idSelectors = null;
|
lowGenericSelectors = [];
|
||||||
classSelectors = null;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// https://github.com/gorhill/uBlock/issues/452
|
// https://github.com/gorhill/uBlock/issues/452
|
||||||
@ -139,7 +135,7 @@ var messager = vAPI.messaging.channel('contentscript-end.js');
|
|||||||
var selectors = vAPI.hideCosmeticFilters;
|
var selectors = vAPI.hideCosmeticFilters;
|
||||||
if ( typeof selectors === 'object' ) {
|
if ( typeof selectors === 'object' ) {
|
||||||
injectedSelectors = selectors;
|
injectedSelectors = selectors;
|
||||||
hideElements(Object.keys(selectors).join(','));
|
//hideElements(Object.keys(selectors));
|
||||||
}
|
}
|
||||||
// Add exception filters into injected filters collection, in order
|
// Add exception filters into injected filters collection, in order
|
||||||
// to force them to be seen as "already injected".
|
// to force them to be seen as "already injected".
|
||||||
@ -158,6 +154,7 @@ var messager = vAPI.messaging.channel('contentscript-end.js');
|
|||||||
};
|
};
|
||||||
|
|
||||||
var otherRetrieveHandler = function(selectors) {
|
var otherRetrieveHandler = function(selectors) {
|
||||||
|
//var tStart = window.performance.now();
|
||||||
//console.debug('µBlock> contextNodes = %o', contextNodes);
|
//console.debug('µBlock> contextNodes = %o', contextNodes);
|
||||||
if ( selectors && selectors.highGenerics ) {
|
if ( selectors && selectors.highGenerics ) {
|
||||||
highGenerics = selectors.highGenerics;
|
highGenerics = selectors.highGenerics;
|
||||||
@ -196,6 +193,7 @@ var messager = vAPI.messaging.channel('contentscript-end.js');
|
|||||||
addStyleTag(hideSelectors);
|
addStyleTag(hideSelectors);
|
||||||
}
|
}
|
||||||
contextNodes.length = 0;
|
contextNodes.length = 0;
|
||||||
|
//console.debug('%f: uBlock: CSS injection time', window.performance.now() - tStart);
|
||||||
};
|
};
|
||||||
|
|
||||||
var retrieveHandler = firstRetrieveHandler;
|
var retrieveHandler = firstRetrieveHandler;
|
||||||
@ -206,10 +204,10 @@ var messager = vAPI.messaging.channel('contentscript-end.js');
|
|||||||
// - Injecting a style tag
|
// - Injecting a style tag
|
||||||
|
|
||||||
var addStyleTag = function(selectors) {
|
var addStyleTag = function(selectors) {
|
||||||
hideElements(selectors);
|
//hideElements(selectors);
|
||||||
var style = document.createElement('style');
|
var style = document.createElement('style');
|
||||||
// The linefeed before the style block is very important: do no remove!
|
// The linefeed before the style block is very important: do no remove!
|
||||||
style.appendChild(document.createTextNode(selectors.join(',\n') + '\n{display:none !important;}'));
|
style.appendChild(document.createTextNode(selectors.toString() + '\n{display:none !important;}'));
|
||||||
var parent = document.body || document.documentElement;
|
var parent = document.body || document.documentElement;
|
||||||
if ( parent ) {
|
if ( parent ) {
|
||||||
parent.appendChild(style);
|
parent.appendChild(style);
|
||||||
@ -349,12 +347,46 @@ var messager = vAPI.messaging.channel('contentscript-end.js');
|
|||||||
// requests to process high-high generics into as few requests as possible.
|
// requests to process high-high generics into as few requests as possible.
|
||||||
// The gain is *significant* on bloated pages.
|
// The gain is *significant* on bloated pages.
|
||||||
|
|
||||||
|
// Speed boost (uBlock 0.8.9.2+):
|
||||||
|
// Element.matches is MUCH faster than document.querySelector().
|
||||||
|
//
|
||||||
|
// Example:
|
||||||
|
// gmail.com, a page rich in mutation events, which causes many calls
|
||||||
|
// to processHighHighGenerics(). Cumulative time of 24 calls to
|
||||||
|
// matchesSelector() (default filter lists):
|
||||||
|
// Chromium 40:
|
||||||
|
// - document.querySelector() = 44.79 ms
|
||||||
|
// - document.body.matches() = 6.06 ms
|
||||||
|
// Firefox 36:
|
||||||
|
// - document.querySelector() = 14.23 ms
|
||||||
|
// - document.body.matches() = 2.97 ms
|
||||||
|
//
|
||||||
|
// However, Element.matches() is available from Chromium 34/Firefox 34. So
|
||||||
|
// we fall back to slower document.querySelector() when Element.matches()
|
||||||
|
// is not available.
|
||||||
|
var matchesSelector = (function() {
|
||||||
|
if ( document.body && document.body.matches ) {
|
||||||
|
return function(selector) {
|
||||||
|
return document.body.matches(selector);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return function(selector) {
|
||||||
|
return document.querySelector(selector) !== null;
|
||||||
|
};
|
||||||
|
})();
|
||||||
|
|
||||||
var processHighHighGenericsTimer = null;
|
var processHighHighGenericsTimer = null;
|
||||||
|
|
||||||
var processHighHighGenerics = function() {
|
var processHighHighGenerics = function() {
|
||||||
processHighHighGenericsTimer = null;
|
processHighHighGenericsTimer = null;
|
||||||
if ( injectedSelectors.hasOwnProperty('{{highHighGenerics}}') ) { return; }
|
if ( injectedSelectors.hasOwnProperty('{{highHighGenerics}}') ) {
|
||||||
if ( document.querySelector(highGenerics.hideHigh) === null ) { return; }
|
return;
|
||||||
|
}
|
||||||
|
//var tStart = window.performance.now();
|
||||||
|
if ( matchesSelector(highGenerics.hideHigh) === false ) {
|
||||||
|
//console.debug('%f: uBlock: high-high generic time', window.performance.now() - tStart);
|
||||||
|
return;
|
||||||
|
}
|
||||||
injectedSelectors['{{highHighGenerics}}'] = true;
|
injectedSelectors['{{highHighGenerics}}'] = true;
|
||||||
// We need to filter out possible exception cosmetic filters from
|
// We need to filter out possible exception cosmetic filters from
|
||||||
// high-high generics selectors.
|
// high-high generics selectors.
|
||||||
@ -388,11 +420,8 @@ var messager = vAPI.messaging.channel('contentscript-end.js');
|
|||||||
if ( !nodes || !nodes.length ) {
|
if ( !nodes || !nodes.length ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if ( idSelectors === null ) {
|
|
||||||
idSelectors = [];
|
|
||||||
}
|
|
||||||
var qq = queriedSelectors;
|
var qq = queriedSelectors;
|
||||||
var ii = idSelectors;
|
var ll = lowGenericSelectors;
|
||||||
var node, v;
|
var node, v;
|
||||||
var i = nodes.length;
|
var i = nodes.length;
|
||||||
while ( i-- ) {
|
while ( i-- ) {
|
||||||
@ -404,8 +433,8 @@ var messager = vAPI.messaging.channel('contentscript-end.js');
|
|||||||
v = v.trim();
|
v = v.trim();
|
||||||
if ( v === '' ) { continue; }
|
if ( v === '' ) { continue; }
|
||||||
v = '#' + v;
|
v = '#' + v;
|
||||||
if ( qq[v] ) { continue; }
|
if ( qq.hasOwnProperty(v) ) { continue; }
|
||||||
ii.push(v);
|
ll.push(v);
|
||||||
qq[v] = true;
|
qq[v] = true;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -417,11 +446,8 @@ var messager = vAPI.messaging.channel('contentscript-end.js');
|
|||||||
if ( !nodes || !nodes.length ) {
|
if ( !nodes || !nodes.length ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if ( classSelectors === null ) {
|
|
||||||
classSelectors = {};
|
|
||||||
}
|
|
||||||
var qq = queriedSelectors;
|
var qq = queriedSelectors;
|
||||||
var cc = classSelectors;
|
var ll = lowGenericSelectors;
|
||||||
var node, v, vv, j;
|
var node, v, vv, j;
|
||||||
var i = nodes.length;
|
var i = nodes.length;
|
||||||
while ( i-- ) {
|
while ( i-- ) {
|
||||||
@ -433,8 +459,8 @@ var messager = vAPI.messaging.channel('contentscript-end.js');
|
|||||||
v = vv[j];
|
v = vv[j];
|
||||||
if ( typeof v !== 'string' ) { continue; }
|
if ( typeof v !== 'string' ) { continue; }
|
||||||
v = '.' + v;
|
v = '.' + v;
|
||||||
if ( qq[v] ) { continue; }
|
if ( qq.hasOwnProperty(v) ) { continue; }
|
||||||
cc[v] = true;
|
ll.push(v);
|
||||||
qq[v] = true;
|
qq[v] = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -446,6 +472,8 @@ var messager = vAPI.messaging.channel('contentscript-end.js');
|
|||||||
classesFromNodeList(document.querySelectorAll('[class]'));
|
classesFromNodeList(document.querySelectorAll('[class]'));
|
||||||
retrieveGenericSelectors();
|
retrieveGenericSelectors();
|
||||||
|
|
||||||
|
//console.debug('%f: uBlock: survey time', window.performance.now() - tStart);
|
||||||
|
|
||||||
// Below this point is the code which takes care to observe changes in
|
// Below this point is the code which takes care to observe changes in
|
||||||
// the page and to add if needed relevant CSS rules as a result of the
|
// the page and to add if needed relevant CSS rules as a result of the
|
||||||
// changes.
|
// changes.
|
||||||
@ -528,7 +556,6 @@ var messager = vAPI.messaging.channel('contentscript-end.js');
|
|||||||
(function() {
|
(function() {
|
||||||
// https://github.com/gorhill/uBlock/issues/683
|
// https://github.com/gorhill/uBlock/issues/683
|
||||||
// Instead of a closure we use a map to remember the element to collapse
|
// Instead of a closure we use a map to remember the element to collapse
|
||||||
// or hide.
|
|
||||||
var filterRequestId = 1;
|
var filterRequestId = 1;
|
||||||
var filterRequests = {};
|
var filterRequests = {};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user