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

Commit 69034ea

Browse files
committed
fine tuning abp filters performance
1 parent c7a4a5a commit 69034ea

File tree

3 files changed

+46
-38
lines changed

3 files changed

+46
-38
lines changed

js/abp-filters.js

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,14 @@ var FilterEntry = function(token) {
8888
this.suffix = '';
8989
};
9090

91+
FilterEntry.prototype.matchString = function(s, tokenBeg, tokenEnd) {
92+
if ( s.indexOf(this.suffix, tokenEnd) !== tokenEnd ) {
93+
return false;
94+
}
95+
tokenBeg -= this.prefix.length;
96+
return s.indexOf(this.prefix, tokenBeg) === tokenBeg;
97+
};
98+
9199
/******************************************************************************/
92100

93101
// Reset all, thus reducing to a minimum memory footprint of the context.
@@ -311,41 +319,42 @@ var freeze = function() {
311319

312320
/******************************************************************************/
313321

314-
var matchFromIdList = function(s, tokenBeg, tokenEnd, list) {
315-
if ( list === undefined ) {
316-
return false;
317-
}
322+
var matchFromFilterIndex = function(s, tokenBeg, tokenEnd, index) {
323+
return filterDict[index].matchString(s, tokenBeg, tokenEnd);
324+
};
318325

319-
var f;
320-
if ( typeof list === 'number' ) {
321-
f = filterDict[list];
322-
if ( s.lastIndexOf(f.prefix, tokenBeg) !== (tokenBeg - f.prefix.length) ) {
323-
return false;
326+
/******************************************************************************/
327+
328+
var matchFromFilterIndices = function(s, tokenBeg, tokenEnd, indices) {
329+
var indicesEnd = indices.length;
330+
var indexBeg = 0, indexEnd;
331+
while ( indexBeg < indicesEnd ) {
332+
indexEnd = indices.indexOf(' ', indexBeg);
333+
if ( indexEnd < 0 ) {
334+
indexEnd = indicesEnd;
324335
}
325-
if ( s.indexOf(f.suffix, tokenEnd) !== tokenEnd ) {
326-
return false;
336+
if ( filterDict[indices.slice(indexBeg, indexEnd)].matchString(s, tokenBeg, tokenEnd) ) {
337+
return true;
327338
}
328-
// console.log('HTTPBA.filters.matchFromIdList(): "%s" matches "%s"', f.prefix + f.token + f.suffix, s);
329-
return true;
339+
indexBeg = indexEnd + 1;
330340
}
341+
return false;
342+
};
331343

332-
var idListEnd = list.length;
333-
var idBeg = 0, idEnd;
334-
while ( idBeg < idListEnd ) {
335-
idEnd = list.indexOf(' ', idBeg);
336-
if ( idEnd < 0 ) {
337-
idEnd = idListEnd;
338-
}
339-
f = filterDict[list.slice(idBeg, idEnd)];
340-
idBeg = idEnd + 1;
341-
if ( s.lastIndexOf(f.prefix, tokenBeg) !== (tokenBeg - f.prefix.length) ) {
342-
continue;
343-
}
344-
if ( s.indexOf(f.suffix, tokenEnd) !== tokenEnd ) {
345-
continue;
346-
}
347-
// console.log('HTTPBA.filters.matchFromIdList(): "%s" matches "%s"', f.prefix + f.token + f.suffix, s);
348-
return true;
344+
/******************************************************************************/
345+
346+
var matchFromSomething = function(s, tokenBeg, tokenEnd, something) {
347+
if ( something === undefined ) {
348+
return false;
349+
}
350+
if ( typeof something === 'number') {
351+
return filterDict[something].matchString(s, tokenBeg, tokenEnd);
352+
}
353+
if ( typeof something === 'string') {
354+
return matchFromFilterIndices(s, tokenBeg, tokenEnd, something);
355+
}
356+
if ( something instanceof FilterEntry ) {
357+
return something.matchString(s, tokenBeg, tokenEnd);
349358
}
350359
return false;
351360
};
@@ -372,20 +381,20 @@ var matchString = function(s) {
372381
tokenBeg = matches.index;
373382
tokenEnd = reToken.lastIndex;
374383
if ( typeof tokenEntry !== 'object' ) {
375-
if ( matchFromIdList(s, tokenBeg, tokenEnd, tokenEntry) ) {
384+
if ( matchFromSomething(s, tokenBeg, tokenEnd, tokenEntry) ) {
376385
return true;
377386
}
378387
continue;
379388
}
380389
prefixKey = tokenBeg > 0 ? s.charAt(matches.index-1) : '0';
381390
suffixKey = tokenEnd < s.length ? s.charAt(tokenEnd) : '0';
382-
if ( matchFromIdList(s, tokenBeg, tokenEnd, tokenEntry[prefixKey + suffixKey]) ) {
391+
if ( matchFromSomething(s, tokenBeg, tokenEnd, tokenEntry[prefixKey + suffixKey]) ) {
383392
return true;
384393
}
385-
if ( matchFromIdList(s, tokenBeg, tokenEnd, tokenEntry[prefixKey + '0']) ) {
394+
if ( matchFromSomething(s, tokenBeg, tokenEnd, tokenEntry[prefixKey + '0']) ) {
386395
return true;
387396
}
388-
if ( matchFromIdList(s, tokenBeg, tokenEnd, tokenEntry['0' + suffixKey]) ) {
397+
if ( matchFromSomething(s, tokenBeg, tokenEnd, tokenEntry['0' + suffixKey]) ) {
389398
return true;
390399
}
391400
}

js/profiler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function Profiler() {
2525
this.time = 0;
2626
this.count = -3;
2727
this._start = 0;
28-
this._lastlog = 0;
28+
this._lastlog = Date.now();
2929
}
3030

3131
Profiler.prototype.reset = function() {

js/traffic.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,17 +157,16 @@ function onBeforeRequestHandler(details) {
157157
return;
158158
}
159159

160-
// quickProfiler.start();
161-
162160
var httpsb = HTTPSB;
163161

164162
// Don't block chrome extensions
165163
if ( requestURL.indexOf(httpsb.chromeExtensionURLPrefix) === 0 ) {
166164
onBeforeChromeExtensionRequestHandler(details);
167-
// quickProfiler.stop('onBeforeRequestHandler');
168165
return;
169166
}
170167

168+
// quickProfiler.start();
169+
171170
var canEvaluate = true;
172171
var tabId = details.tabId;
173172

0 commit comments

Comments
 (0)