From 958a7c20ed8f2f0119934356cad8d577350702eb Mon Sep 17 00:00:00 2001 From: gorhill Date: Sun, 10 Dec 2017 15:03:03 -0500 Subject: [PATCH] fix #3335 --- src/js/contentscript.js | 57 +++++++++++++++++------------------------ 1 file changed, 23 insertions(+), 34 deletions(-) diff --git a/src/js/contentscript.js b/src/js/contentscript.js index 8a68e7a25..b2175ed5b 100644 --- a/src/js/contentscript.js +++ b/src/js/contentscript.js @@ -246,7 +246,9 @@ vAPI.domWatcher = (function() { } } if ( addedNodeLists.length !== 0 || removedNodes ) { - safeObserverHandlerTimer.start(1); + safeObserverHandlerTimer.start( + addedNodeLists.length < 100 ? 1 : undefined + ); } //console.timeEnd('dom watcher/observer handler'); }; @@ -486,9 +488,10 @@ vAPI.DOMFilterer = (function() { [ ':xpath', PSelectorXpathTask ] ]); } - this.budget = 250; // I arbitrary picked a 1/4 second + this.budget = 200; // I arbitrary picked a 1/5 second this.raw = o.raw; this.cost = 0; + this.lastAllowanceTime = 0; this.selector = o.selector; this.tasks = []; var tasks = o.tasks; @@ -533,8 +536,6 @@ vAPI.DOMFilterer = (function() { this.addedSelectors = new Map(); this.addedNodes = false; this.removedNodes = false; - this.addedNodesHandlerMissCount = 0; - this.currentResultset = new Set(); this.selectors = new Map(); }; @@ -587,8 +588,7 @@ vAPI.DOMFilterer = (function() { this.addedSelectors.clear(); } - var currentResultset = this.currentResultset, - entry, nodes, i, node; + var entry, nodes, i; if ( this.addedSelectors.size !== 0 ) { //console.time('procedural selectors/filterset changed'); @@ -596,9 +596,7 @@ vAPI.DOMFilterer = (function() { nodes = entry[1].exec(); i = nodes.length; while ( i-- ) { - node = nodes[i]; - this.domFilterer.hideNode(node); - currentResultset.add(node); + this.domFilterer.hideNode(nodes[i]); } } this.addedSelectors.clear(); @@ -610,40 +608,31 @@ vAPI.DOMFilterer = (function() { this.addedNodes = this.removedNodes = false; - var afterResultset = new Set(), - t0 = Date.now(), t1, cost, pselector; + var t0 = Date.now(), + t1, pselector, allowance; for ( entry of this.selectors ) { pselector = entry[1]; + allowance = Math.floor((t0 - pselector.lastAllowanceTime) / 2000); + if ( allowance >= 1 ) { + pselector.budget += allowance * 50; + if ( pselector.budget > 200 ) { pselector.budget = 200; } + pselector.lastAllowanceTime = t0; + } if ( pselector.budget <= 0 ) { continue; } nodes = pselector.exec(); + t1 = Date.now(); + pselector.budget += t0 - t1; + if ( pselector.budget < -500 ) { + console.log('uBO: disabling %s', pselector.raw); + pselector.budget = -0x7FFFFFFF; + } + t0 = t1; i = nodes.length; while ( i-- ) { - node = nodes[i]; - this.domFilterer.hideNode(node); - afterResultset.add(node); - } - t1 = Date.now(); - cost = t1 - t0; - t0 = t1; - if ( cost <= 8 ) { continue; } - pselector.budget -= cost; - if ( pselector.budget <= 0 ) { - console.log('disabling %s', pselector.raw); + this.domFilterer.hideNode(nodes[i]); } } - if ( afterResultset.size !== currentResultset.size ) { - this.addedNodesHandlerMissCount = 0; - } else { - this.addedNodesHandlerMissCount += 1; - } - for ( node of currentResultset ) { - if ( afterResultset.has(node) === false ) { - this.domFilterer.unhideNode(node); - } - } - - this.currentResultset = afterResultset; //console.timeEnd('procedural selectors/dom layout changed'); },