From c837f32fa728e73e6498a6395efb6acfcf9d1ac6 Mon Sep 17 00:00:00 2001 From: gorhill Date: Mon, 7 Jul 2014 22:09:29 -0400 Subject: [PATCH] performance work, got rid of some bad assumptions re. dictionary --- js/abp-filters.js | 172 +++++++++++++++++++++------------------------- js/profiler.js | 77 ++++++++------------- 2 files changed, 107 insertions(+), 142 deletions(-) diff --git a/js/abp-filters.js b/js/abp-filters.js index 7a64a9b9641aa..e03d22682c2e6 100644 --- a/js/abp-filters.js +++ b/js/abp-filters.js @@ -731,7 +731,9 @@ var badTokens = { 'images': true, 'img': true, 'js': true, + 'net': true, 'news': true, + 'social': true, 'www': true }; @@ -1011,15 +1013,7 @@ var FilterContainer = function() { // Used during URL matching this.reAnyToken = /[%0-9a-z]+/g; - this.matches = null; - this.bucket0 = undefined; - this.bucket1 = undefined; - this.bucket2 = undefined; - this.bucket3 = undefined; - this.bucket4 = undefined; - this.bucket5 = undefined; - this.bucket6 = undefined; - this.bucket7 = undefined; + this.buckets = new Array(8); }; /******************************************************************************/ @@ -1188,9 +1182,7 @@ FilterContainer.prototype.addFilter = function(parsed) { FilterContainer.prototype.addFilterEntry = function(filter, parsed, party, tokenBeg, tokenEnd) { var s = parsed.f; - var prefixKey = trimChar(s.substring(tokenBeg - 1, tokenBeg), '*'); - var suffixKey = trimChar(s.substring(tokenEnd, tokenEnd + 1), '*'); - var tokenKey = prefixKey + s.slice(tokenBeg, tokenEnd) + suffixKey; + var tokenKey = s.slice(tokenBeg, tokenEnd); if ( parsed.types.length === 0 ) { this.addToCategory(parsed.action | AnyType | party, tokenKey, filter); return; @@ -1240,7 +1232,7 @@ FilterContainer.prototype.reset = function() { /******************************************************************************/ FilterContainer.prototype.freeze = function() { - // histogram('allFilters', this.categories); + //histogram('allFilters', this.categories); this.blockedAnyPartyHostnames.freeze(); this.blocked3rdPartyHostnames.freeze(); this.duplicates = {}; @@ -1249,76 +1241,72 @@ FilterContainer.prototype.freeze = function() { /******************************************************************************/ -FilterContainer.prototype.matchToken = function(bucket) { - var url = this.url; - var beg = this.matches.index; - var end = this.reAnyToken.lastIndex; - var f; - if ( end !== url.length ) { - if ( beg !== 0 ) { - f = bucket[url.slice(beg-1, end+1)]; - if ( f !== undefined && f.match(url, beg) !== false ) { - return f.s; - } - } - f = bucket[url.slice(beg, end+1)]; - if ( f !== undefined && f.match(url, beg) !== false ) { - return f.s; - } - } - if ( beg !== 0 ) { - f = bucket[url.slice(beg-1, end)]; - if ( f !== undefined && f.match(url, beg) !== false ) { - return f.s; - } - } - f = bucket[url.slice(beg, end)]; - if ( f !== undefined && f.match(url, beg) !== false ) { - return f.s; - } - return false; -}; - -/******************************************************************************/ - FilterContainer.prototype.matchTokens = function() { var url = this.url; var re = this.reAnyToken; - var r; + var matches, beg, token; + var buckets = this.buckets; + var bucket0 = buckets[0]; + var bucket1 = buckets[1]; + var bucket2 = buckets[2]; + var bucket3 = buckets[3]; + var bucket4 = buckets[4]; + var bucket5 = buckets[5]; + var bucket6 = buckets[6]; + var bucket7 = buckets[7]; + var f; re.lastIndex = 0; - while ( this.matches = re.exec(url) ) { - if ( this.bucket0 ) { - r = this.matchToken(this.bucket0); - if ( r !== false ) { return r; } + while ( matches = re.exec(url) ) { + beg = matches.index; + token = url.slice(beg, re.lastIndex); + if ( bucket0 !== undefined ) { + f = bucket0[token]; + if ( f !== undefined && f.match(url, beg) !== false ) { + return f.s; + } } - if ( this.bucket1 ) { - r = this.matchToken(this.bucket1); - if ( r !== false ) { return r; } + if ( bucket1 !== undefined ) { + f = bucket1[token]; + if ( f !== undefined && f.match(url, beg) !== false ) { + return f.s; + } } - if ( this.bucket2 ) { - r = this.matchToken(this.bucket2); - if ( r !== false ) { return r; } + if ( bucket2 !== undefined ) { + f = bucket2[token]; + if ( f !== undefined && f.match(url, beg) !== false ) { + return f.s; + } } - if ( this.bucket3 ) { - r = this.matchToken(this.bucket3); - if ( r !== false ) { return r; } + if ( bucket3 !== undefined ) { + f = bucket3[token]; + if ( f !== undefined && f.match(url, beg) !== false ) { + return f.s; + } } - if ( this.bucket4 ) { - r = this.matchToken(this.bucket4); - if ( r !== false ) { return r; } + if ( bucket4 !== undefined ) { + f = bucket4[token]; + if ( f !== undefined && f.match(url, beg) !== false ) { + return f.s; + } } - if ( this.bucket5 ) { - r = this.matchToken(this.bucket5); - if ( r !== false ) { return r; } + if ( bucket5 !== undefined ) { + f = bucket5[token]; + if ( f !== undefined && f.match(url, beg) !== false ) { + return f.s; + } } - if ( this.bucket6 ) { - r = this.matchToken(this.bucket6); - if ( r !== false ) { return r; } + if ( bucket6 !== undefined ) { + f = bucket6[token]; + if ( f !== undefined && f.match(url, beg) !== false ) { + return f.s; + } } - if ( this.bucket7 ) { - r = this.matchToken(this.bucket7); - if ( r !== false ) { return r; } + if ( bucket7 !== undefined ) { + f = bucket7[token]; + if ( f !== undefined && f.match(url, beg) !== false ) { + return f.s; + } } } return false; @@ -1410,9 +1398,6 @@ FilterContainer.prototype.matchString = function(pageDetails, url, requestType, var party = requestHostname.slice(-pageDomain.length) === pageDomain ? FirstParty : ThirdParty; - var domainParty = this.toDomainBits(pageDomain); - var type = typeNameToTypeValue[requestType]; - var categories = this.categories; // Test hostname-based block filters var br = this.matchAnyPartyHostname(requestHostname); @@ -1423,17 +1408,21 @@ FilterContainer.prototype.matchString = function(pageDetails, url, requestType, // This will be used by hostname-based filters pageHostname = pageDetails.pageHostname || ''; + var domainParty = this.toDomainBits(pageDomain); + var type = typeNameToTypeValue[requestType]; + var categories = this.categories; + var buckets = this.buckets; + // Test against block filters if ( br === false ) { - this.bucket0 = categories[this.makeCategoryKey(BlockAnyTypeAnyParty)]; - this.bucket1 = categories[this.makeCategoryKey(BlockAnyType | party)]; - this.bucket2 = categories[this.makeCategoryKey(BlockAnyTypeOneParty | domainParty)]; - this.bucket3 = categories[this.makeCategoryKey(BlockAnyTypeOtherParties)]; - this.bucket4 = categories[this.makeCategoryKey(BlockAnyParty | type)]; - this.bucket5 = categories[this.makeCategoryKey(BlockAction | type | party)]; - this.bucket6 = categories[this.makeCategoryKey(BlockOneParty | type | domainParty)]; - this.bucket7 = categories[this.makeCategoryKey(BlockOtherParties | type)]; - + buckets[0] = categories[this.makeCategoryKey(BlockAnyTypeAnyParty)]; + buckets[1] = categories[this.makeCategoryKey(BlockAnyType | party)]; + buckets[2] = categories[this.makeCategoryKey(BlockAnyTypeOneParty | domainParty)]; + buckets[3] = categories[this.makeCategoryKey(BlockAnyTypeOtherParties)]; + buckets[4] = categories[this.makeCategoryKey(BlockAnyParty | type)]; + buckets[5] = categories[this.makeCategoryKey(BlockAction | type | party)]; + buckets[6] = categories[this.makeCategoryKey(BlockOneParty | type | domainParty)]; + buckets[7] = categories[this.makeCategoryKey(BlockOtherParties | type)]; br = this.matchTokens(); } @@ -1443,15 +1432,14 @@ FilterContainer.prototype.matchString = function(pageDetails, url, requestType, } // Test against allow filters - this.bucket0 = categories[this.makeCategoryKey(AllowAnyTypeAnyParty)]; - this.bucket1 = categories[this.makeCategoryKey(AllowAnyType | party)]; - this.bucket2 = categories[this.makeCategoryKey(AllowAnyTypeOneParty | domainParty)]; - this.bucket3 = categories[this.makeCategoryKey(AllowAnyTypeOtherParties | domainParty)]; - this.bucket4 = categories[this.makeCategoryKey(AllowAnyParty | type)]; - this.bucket5 = categories[this.makeCategoryKey(AllowAction | type | party)]; - this.bucket6 = categories[this.makeCategoryKey(AllowOneParty | type | domainParty)]; - this.bucket7 = categories[this.makeCategoryKey(AllowOtherParties | type | domainParty)]; - + buckets[0] = categories[this.makeCategoryKey(AllowAnyTypeAnyParty)]; + buckets[1] = categories[this.makeCategoryKey(AllowAnyType | party)]; + buckets[2] = categories[this.makeCategoryKey(AllowAnyTypeOneParty | domainParty)]; + buckets[3] = categories[this.makeCategoryKey(AllowAnyTypeOtherParties | domainParty)]; + buckets[4] = categories[this.makeCategoryKey(AllowAnyParty | type)]; + buckets[5] = categories[this.makeCategoryKey(AllowAction | type | party)]; + buckets[6] = categories[this.makeCategoryKey(AllowOneParty | type | domainParty)]; + buckets[7] = categories[this.makeCategoryKey(AllowOtherParties | type | domainParty)]; var ar = this.matchTokens(); if ( ar !== false ) { return '@@' + ar; diff --git a/js/profiler.js b/js/profiler.js index 8eeecc84a3a9a..36caedbafa6b2 100644 --- a/js/profiler.js +++ b/js/profiler.js @@ -22,61 +22,38 @@ /******************************************************************************/ var quickProfiler = (function() { - -/******************************************************************************/ - -var timer = performance; -var time = 0; -var count = -3; -var tstart = 0; -var lastlog = timer.now(); -var prompt = ''; - -/******************************************************************************/ - -var reset = function() { - time = 0; - count = -3; - tstart = 0; -}; - -/******************************************************************************/ - -var avg = function() { - return count > 0 ? time / count : 0; -}; - -/******************************************************************************/ - -var start = function(s) { - prompt = s || ''; - tstart = timer.now(); -}; - -/******************************************************************************/ - -var stop = function() { - count += 1; - if ( count > 0 ) { + var timer = performance; + var time = 0; + var count = 0; + var tstart = 0; + var lastlog = timer.now(); + var prompt = ''; + var reset = function() { + time = 0; + count = 0; + tstart = 0; + }; + var avg = function() { + return count > 0 ? time / count : 0; + }; + var start = function(s) { + prompt = s || ''; + tstart = timer.now(); + }; + var stop = function() { var now = timer.now(); + count += 1; time += (now - tstart); if ( (now - lastlog) > 10000 ) { - console.log('µBlock() > %s: %s ms', prompt, avg().toFixed(3)); + console.log('µBlock() > %s: %s ms (%d samples)', prompt, avg().toFixed(3), count); lastlog = now; } - } -}; - -/******************************************************************************/ - -return { - reset: reset, - start: start, - stop: stop -}; - -/******************************************************************************/ - + }; + return { + reset: reset, + start: start, + stop: stop + }; })(); /******************************************************************************/