Skip to content

Commit c837f32

Browse files
committed
performance work, got rid of some bad assumptions re. dictionary
1 parent 1abf304 commit c837f32

File tree

2 files changed

+107
-142
lines changed

2 files changed

+107
-142
lines changed

js/abp-filters.js

Lines changed: 80 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,9 @@ var badTokens = {
731731
'images': true,
732732
'img': true,
733733
'js': true,
734+
'net': true,
734735
'news': true,
736+
'social': true,
735737
'www': true
736738
};
737739

@@ -1011,15 +1013,7 @@ var FilterContainer = function() {
10111013

10121014
// Used during URL matching
10131015
this.reAnyToken = /[%0-9a-z]+/g;
1014-
this.matches = null;
1015-
this.bucket0 = undefined;
1016-
this.bucket1 = undefined;
1017-
this.bucket2 = undefined;
1018-
this.bucket3 = undefined;
1019-
this.bucket4 = undefined;
1020-
this.bucket5 = undefined;
1021-
this.bucket6 = undefined;
1022-
this.bucket7 = undefined;
1016+
this.buckets = new Array(8);
10231017
};
10241018

10251019
/******************************************************************************/
@@ -1188,9 +1182,7 @@ FilterContainer.prototype.addFilter = function(parsed) {
11881182

11891183
FilterContainer.prototype.addFilterEntry = function(filter, parsed, party, tokenBeg, tokenEnd) {
11901184
var s = parsed.f;
1191-
var prefixKey = trimChar(s.substring(tokenBeg - 1, tokenBeg), '*');
1192-
var suffixKey = trimChar(s.substring(tokenEnd, tokenEnd + 1), '*');
1193-
var tokenKey = prefixKey + s.slice(tokenBeg, tokenEnd) + suffixKey;
1185+
var tokenKey = s.slice(tokenBeg, tokenEnd);
11941186
if ( parsed.types.length === 0 ) {
11951187
this.addToCategory(parsed.action | AnyType | party, tokenKey, filter);
11961188
return;
@@ -1240,7 +1232,7 @@ FilterContainer.prototype.reset = function() {
12401232
/******************************************************************************/
12411233

12421234
FilterContainer.prototype.freeze = function() {
1243-
// histogram('allFilters', this.categories);
1235+
//histogram('allFilters', this.categories);
12441236
this.blockedAnyPartyHostnames.freeze();
12451237
this.blocked3rdPartyHostnames.freeze();
12461238
this.duplicates = {};
@@ -1249,76 +1241,72 @@ FilterContainer.prototype.freeze = function() {
12491241

12501242
/******************************************************************************/
12511243

1252-
FilterContainer.prototype.matchToken = function(bucket) {
1253-
var url = this.url;
1254-
var beg = this.matches.index;
1255-
var end = this.reAnyToken.lastIndex;
1256-
var f;
1257-
if ( end !== url.length ) {
1258-
if ( beg !== 0 ) {
1259-
f = bucket[url.slice(beg-1, end+1)];
1260-
if ( f !== undefined && f.match(url, beg) !== false ) {
1261-
return f.s;
1262-
}
1263-
}
1264-
f = bucket[url.slice(beg, end+1)];
1265-
if ( f !== undefined && f.match(url, beg) !== false ) {
1266-
return f.s;
1267-
}
1268-
}
1269-
if ( beg !== 0 ) {
1270-
f = bucket[url.slice(beg-1, end)];
1271-
if ( f !== undefined && f.match(url, beg) !== false ) {
1272-
return f.s;
1273-
}
1274-
}
1275-
f = bucket[url.slice(beg, end)];
1276-
if ( f !== undefined && f.match(url, beg) !== false ) {
1277-
return f.s;
1278-
}
1279-
return false;
1280-
};
1281-
1282-
/******************************************************************************/
1283-
12841244
FilterContainer.prototype.matchTokens = function() {
12851245
var url = this.url;
12861246
var re = this.reAnyToken;
1287-
var r;
1247+
var matches, beg, token;
1248+
var buckets = this.buckets;
1249+
var bucket0 = buckets[0];
1250+
var bucket1 = buckets[1];
1251+
var bucket2 = buckets[2];
1252+
var bucket3 = buckets[3];
1253+
var bucket4 = buckets[4];
1254+
var bucket5 = buckets[5];
1255+
var bucket6 = buckets[6];
1256+
var bucket7 = buckets[7];
1257+
var f;
12881258

12891259
re.lastIndex = 0;
1290-
while ( this.matches = re.exec(url) ) {
1291-
if ( this.bucket0 ) {
1292-
r = this.matchToken(this.bucket0);
1293-
if ( r !== false ) { return r; }
1260+
while ( matches = re.exec(url) ) {
1261+
beg = matches.index;
1262+
token = url.slice(beg, re.lastIndex);
1263+
if ( bucket0 !== undefined ) {
1264+
f = bucket0[token];
1265+
if ( f !== undefined && f.match(url, beg) !== false ) {
1266+
return f.s;
1267+
}
12941268
}
1295-
if ( this.bucket1 ) {
1296-
r = this.matchToken(this.bucket1);
1297-
if ( r !== false ) { return r; }
1269+
if ( bucket1 !== undefined ) {
1270+
f = bucket1[token];
1271+
if ( f !== undefined && f.match(url, beg) !== false ) {
1272+
return f.s;
1273+
}
12981274
}
1299-
if ( this.bucket2 ) {
1300-
r = this.matchToken(this.bucket2);
1301-
if ( r !== false ) { return r; }
1275+
if ( bucket2 !== undefined ) {
1276+
f = bucket2[token];
1277+
if ( f !== undefined && f.match(url, beg) !== false ) {
1278+
return f.s;
1279+
}
13021280
}
1303-
if ( this.bucket3 ) {
1304-
r = this.matchToken(this.bucket3);
1305-
if ( r !== false ) { return r; }
1281+
if ( bucket3 !== undefined ) {
1282+
f = bucket3[token];
1283+
if ( f !== undefined && f.match(url, beg) !== false ) {
1284+
return f.s;
1285+
}
13061286
}
1307-
if ( this.bucket4 ) {
1308-
r = this.matchToken(this.bucket4);
1309-
if ( r !== false ) { return r; }
1287+
if ( bucket4 !== undefined ) {
1288+
f = bucket4[token];
1289+
if ( f !== undefined && f.match(url, beg) !== false ) {
1290+
return f.s;
1291+
}
13101292
}
1311-
if ( this.bucket5 ) {
1312-
r = this.matchToken(this.bucket5);
1313-
if ( r !== false ) { return r; }
1293+
if ( bucket5 !== undefined ) {
1294+
f = bucket5[token];
1295+
if ( f !== undefined && f.match(url, beg) !== false ) {
1296+
return f.s;
1297+
}
13141298
}
1315-
if ( this.bucket6 ) {
1316-
r = this.matchToken(this.bucket6);
1317-
if ( r !== false ) { return r; }
1299+
if ( bucket6 !== undefined ) {
1300+
f = bucket6[token];
1301+
if ( f !== undefined && f.match(url, beg) !== false ) {
1302+
return f.s;
1303+
}
13181304
}
1319-
if ( this.bucket7 ) {
1320-
r = this.matchToken(this.bucket7);
1321-
if ( r !== false ) { return r; }
1305+
if ( bucket7 !== undefined ) {
1306+
f = bucket7[token];
1307+
if ( f !== undefined && f.match(url, beg) !== false ) {
1308+
return f.s;
1309+
}
13221310
}
13231311
}
13241312
return false;
@@ -1410,9 +1398,6 @@ FilterContainer.prototype.matchString = function(pageDetails, url, requestType,
14101398
var party = requestHostname.slice(-pageDomain.length) === pageDomain ?
14111399
FirstParty :
14121400
ThirdParty;
1413-
var domainParty = this.toDomainBits(pageDomain);
1414-
var type = typeNameToTypeValue[requestType];
1415-
var categories = this.categories;
14161401

14171402
// Test hostname-based block filters
14181403
var br = this.matchAnyPartyHostname(requestHostname);
@@ -1423,17 +1408,21 @@ FilterContainer.prototype.matchString = function(pageDetails, url, requestType,
14231408
// This will be used by hostname-based filters
14241409
pageHostname = pageDetails.pageHostname || '';
14251410

1411+
var domainParty = this.toDomainBits(pageDomain);
1412+
var type = typeNameToTypeValue[requestType];
1413+
var categories = this.categories;
1414+
var buckets = this.buckets;
1415+
14261416
// Test against block filters
14271417
if ( br === false ) {
1428-
this.bucket0 = categories[this.makeCategoryKey(BlockAnyTypeAnyParty)];
1429-
this.bucket1 = categories[this.makeCategoryKey(BlockAnyType | party)];
1430-
this.bucket2 = categories[this.makeCategoryKey(BlockAnyTypeOneParty | domainParty)];
1431-
this.bucket3 = categories[this.makeCategoryKey(BlockAnyTypeOtherParties)];
1432-
this.bucket4 = categories[this.makeCategoryKey(BlockAnyParty | type)];
1433-
this.bucket5 = categories[this.makeCategoryKey(BlockAction | type | party)];
1434-
this.bucket6 = categories[this.makeCategoryKey(BlockOneParty | type | domainParty)];
1435-
this.bucket7 = categories[this.makeCategoryKey(BlockOtherParties | type)];
1436-
1418+
buckets[0] = categories[this.makeCategoryKey(BlockAnyTypeAnyParty)];
1419+
buckets[1] = categories[this.makeCategoryKey(BlockAnyType | party)];
1420+
buckets[2] = categories[this.makeCategoryKey(BlockAnyTypeOneParty | domainParty)];
1421+
buckets[3] = categories[this.makeCategoryKey(BlockAnyTypeOtherParties)];
1422+
buckets[4] = categories[this.makeCategoryKey(BlockAnyParty | type)];
1423+
buckets[5] = categories[this.makeCategoryKey(BlockAction | type | party)];
1424+
buckets[6] = categories[this.makeCategoryKey(BlockOneParty | type | domainParty)];
1425+
buckets[7] = categories[this.makeCategoryKey(BlockOtherParties | type)];
14371426
br = this.matchTokens();
14381427
}
14391428

@@ -1443,15 +1432,14 @@ FilterContainer.prototype.matchString = function(pageDetails, url, requestType,
14431432
}
14441433

14451434
// Test against allow filters
1446-
this.bucket0 = categories[this.makeCategoryKey(AllowAnyTypeAnyParty)];
1447-
this.bucket1 = categories[this.makeCategoryKey(AllowAnyType | party)];
1448-
this.bucket2 = categories[this.makeCategoryKey(AllowAnyTypeOneParty | domainParty)];
1449-
this.bucket3 = categories[this.makeCategoryKey(AllowAnyTypeOtherParties | domainParty)];
1450-
this.bucket4 = categories[this.makeCategoryKey(AllowAnyParty | type)];
1451-
this.bucket5 = categories[this.makeCategoryKey(AllowAction | type | party)];
1452-
this.bucket6 = categories[this.makeCategoryKey(AllowOneParty | type | domainParty)];
1453-
this.bucket7 = categories[this.makeCategoryKey(AllowOtherParties | type | domainParty)];
1454-
1435+
buckets[0] = categories[this.makeCategoryKey(AllowAnyTypeAnyParty)];
1436+
buckets[1] = categories[this.makeCategoryKey(AllowAnyType | party)];
1437+
buckets[2] = categories[this.makeCategoryKey(AllowAnyTypeOneParty | domainParty)];
1438+
buckets[3] = categories[this.makeCategoryKey(AllowAnyTypeOtherParties | domainParty)];
1439+
buckets[4] = categories[this.makeCategoryKey(AllowAnyParty | type)];
1440+
buckets[5] = categories[this.makeCategoryKey(AllowAction | type | party)];
1441+
buckets[6] = categories[this.makeCategoryKey(AllowOneParty | type | domainParty)];
1442+
buckets[7] = categories[this.makeCategoryKey(AllowOtherParties | type | domainParty)];
14551443
var ar = this.matchTokens();
14561444
if ( ar !== false ) {
14571445
return '@@' + ar;

js/profiler.js

Lines changed: 27 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -22,61 +22,38 @@
2222
/******************************************************************************/
2323

2424
var quickProfiler = (function() {
25-
26-
/******************************************************************************/
27-
28-
var timer = performance;
29-
var time = 0;
30-
var count = -3;
31-
var tstart = 0;
32-
var lastlog = timer.now();
33-
var prompt = '';
34-
35-
/******************************************************************************/
36-
37-
var reset = function() {
38-
time = 0;
39-
count = -3;
40-
tstart = 0;
41-
};
42-
43-
/******************************************************************************/
44-
45-
var avg = function() {
46-
return count > 0 ? time / count : 0;
47-
};
48-
49-
/******************************************************************************/
50-
51-
var start = function(s) {
52-
prompt = s || '';
53-
tstart = timer.now();
54-
};
55-
56-
/******************************************************************************/
57-
58-
var stop = function() {
59-
count += 1;
60-
if ( count > 0 ) {
25+
var timer = performance;
26+
var time = 0;
27+
var count = 0;
28+
var tstart = 0;
29+
var lastlog = timer.now();
30+
var prompt = '';
31+
var reset = function() {
32+
time = 0;
33+
count = 0;
34+
tstart = 0;
35+
};
36+
var avg = function() {
37+
return count > 0 ? time / count : 0;
38+
};
39+
var start = function(s) {
40+
prompt = s || '';
41+
tstart = timer.now();
42+
};
43+
var stop = function() {
6144
var now = timer.now();
45+
count += 1;
6246
time += (now - tstart);
6347
if ( (now - lastlog) > 10000 ) {
64-
console.log('µBlock() > %s: %s ms', prompt, avg().toFixed(3));
48+
console.log('µBlock() > %s: %s ms (%d samples)', prompt, avg().toFixed(3), count);
6549
lastlog = now;
6650
}
67-
}
68-
};
69-
70-
/******************************************************************************/
71-
72-
return {
73-
reset: reset,
74-
start: start,
75-
stop: stop
76-
};
77-
78-
/******************************************************************************/
79-
51+
};
52+
return {
53+
reset: reset,
54+
start: start,
55+
stop: stop
56+
};
8057
})();
8158

8259
/******************************************************************************/

0 commit comments

Comments
 (0)