Skip to content
This repository was archived by the owner on Nov 15, 2017. It is now read-only.

Commit 656a580

Browse files
committed
using liquid dict for pure 3rd-party hostname filters
1 parent 0b9ccc1 commit 656a580

File tree

2 files changed

+37
-19
lines changed

2 files changed

+37
-19
lines changed

doc/img/onbeforerequest-perf.png

602 Bytes
Loading

js/abp-filters.js

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
/******************************************************************************/
2626

27-
(function(){
27+
HTTPSB.abpFilters = (function(){
2828

2929
/******************************************************************************/
3030

@@ -63,7 +63,7 @@ var pageHostname = '';
6363

6464
var reIgnoreEmpty = /^\s+$/;
6565
var reIgnoreComment = /^\[|^!/;
66-
var reHostnameRule = /^[0-9a-z.-]+[0-9a-z]$/;
66+
var reHostnameRule = /^[0-9a-z][0-9a-z.-]+[0-9a-z]$/;
6767
var reHostnameToken = /^[0-9a-z]+/g;
6868
var reGoodToken = /[%0-9a-z]{2,}/g;
6969

@@ -679,6 +679,9 @@ var FilterContainer = function() {
679679
this.allowFilterCount = 0;
680680
this.blockFilterCount = 0;
681681

682+
// This is for filters which are strictly a 3rd-party hostname
683+
this.blocked3rdPartyHostnames = new HTTPSB.LiquidDict();
684+
682685
// Used during URL matching
683686
this.categoryBuckets = new Array(8);
684687
this.reAnyToken = /[%0-9a-z]+/g;
@@ -736,11 +739,18 @@ FilterContainer.prototype.add = function(s) {
736739
this.supportedFilterCount += 1;
737740

738741
// Ignore optionless hostname rules, these will be taken care of by HTTPSB.
739-
if ( parsed.hostname && !parsed.fopts && parsed.action === BlockAction && reHostnameRule.test(parsed.f) ) {
742+
if ( parsed.hostname && parsed.fopts === '' && parsed.action === BlockAction && reHostnameRule.test(parsed.f) ) {
740743
return false;
741744
}
742745

743-
if ( this.addFilter(parsed) === false ) {
746+
// Pure third-party hostnames, use more efficient liquid dict
747+
var r;
748+
if ( parsed.hostname && parsed.fopts === 'third-party' && parsed.action === BlockAction && reHostnameRule.test(parsed.f) ) {
749+
r = this.blocked3rdPartyHostnames.add(parsed.f);
750+
} else {
751+
r = this.addFilter(parsed);
752+
}
753+
if ( r === false ) {
744754
return false;
745755
}
746756

@@ -848,6 +858,7 @@ FilterContainer.prototype.reset = function() {
848858
this.allowFilterCount = 0;
849859
this.blockFilterCount = 0;
850860
this.categories = {};
861+
this.blocked3rdPartyHostnames.reset();
851862
};
852863

853864
/******************************************************************************/
@@ -1048,6 +1059,7 @@ with the new code.
10481059

10491060
FilterContainer.prototype.freeze = function() {
10501061
// histogram('allFilters', this.categories);
1062+
this.blocked3rdPartyHostnames.freeze();
10511063
};
10521064

10531065
/******************************************************************************/
@@ -1151,26 +1163,32 @@ FilterContainer.prototype.matchString = function(pageStats, url, requestType, re
11511163
ThirdParty;
11521164
var domainParty = this.toDomainBits(pageDomain);
11531165
var type = typeNameToTypeValue[requestType];
1166+
var categories = this.categories;
1167+
var categoryBuckets = this.categoryBuckets;
11541168

11551169
// This will be used by hostname-based filter
11561170
pageHostname = pageStats.pageHostname;
11571171

1158-
var categories = this.categories;
1159-
var categoryBuckets = this.categoryBuckets;
1172+
var bf = false;
1173+
if ( party === ThirdParty && this.blocked3rdPartyHostnames.test(requestHostname) ) {
1174+
bf = '||' + requestHostname + '^$third-party';
1175+
}
11601176

11611177
// Test against block filters
1162-
categoryBuckets[7] = categories[this.makeCategoryKey(BlockAnyTypeAnyParty)];
1163-
categoryBuckets[6] = categories[this.makeCategoryKey(BlockAnyType | party)];
1164-
categoryBuckets[5] = categories[this.makeCategoryKey(BlockAnyTypeOneParty | domainParty)];
1165-
categoryBuckets[4] = categories[this.makeCategoryKey(BlockAnyTypeOtherParties | domainParty)];
1166-
categoryBuckets[3] = categories[this.makeCategoryKey(BlockAnyParty | type)];
1167-
categoryBuckets[2] = categories[this.makeCategoryKey(BlockAction | type | party)];
1168-
categoryBuckets[1] = categories[this.makeCategoryKey(BlockOneParty | type | domainParty)];
1169-
categoryBuckets[0] = categories[this.makeCategoryKey(BlockOtherParties | type | domainParty)];
1170-
1171-
var bf = this.matchTokens();
1172-
1173-
// If there was no block filter, no need to test against allow filters
1178+
if ( bf === false ) {
1179+
categoryBuckets[7] = categories[this.makeCategoryKey(BlockAnyTypeAnyParty)];
1180+
categoryBuckets[6] = categories[this.makeCategoryKey(BlockAnyType | party)];
1181+
categoryBuckets[5] = categories[this.makeCategoryKey(BlockAnyTypeOneParty | domainParty)];
1182+
categoryBuckets[4] = categories[this.makeCategoryKey(BlockAnyTypeOtherParties | domainParty)];
1183+
categoryBuckets[3] = categories[this.makeCategoryKey(BlockAnyParty | type)];
1184+
categoryBuckets[2] = categories[this.makeCategoryKey(BlockAction | type | party)];
1185+
categoryBuckets[1] = categories[this.makeCategoryKey(BlockOneParty | type | domainParty)];
1186+
categoryBuckets[0] = categories[this.makeCategoryKey(BlockOtherParties | type | domainParty)];
1187+
1188+
// If there is no block filter, no need to test against allow filters
1189+
bf = this.matchTokens();
1190+
}
1191+
11741192
if ( bf === false ) {
11751193
return false;
11761194
}
@@ -1200,7 +1218,7 @@ FilterContainer.prototype.getFilterCount = function() {
12001218

12011219
/******************************************************************************/
12021220

1203-
HTTPSB.abpFilters = new FilterContainer();
1221+
return new FilterContainer();
12041222

12051223
/******************************************************************************/
12061224

0 commit comments

Comments
 (0)