this hopefully fixes #134

This commit is contained in:
gorhill 2015-04-26 18:31:51 -04:00
parent 3de335291e
commit af5aeeda6c
2 changed files with 24 additions and 18 deletions

View File

@ -833,13 +833,21 @@ FilterHostnameDict.prototype.meltBucket = function(len, bucket) {
} else { } else {
var offset = 0; var offset = 0;
while ( offset < bucket.length ) { while ( offset < bucket.length ) {
map[bucket.substring(offset, len)] = true; map[bucket.substr(offset, len)] = true;
offset += len; offset += len;
} }
} }
return map; return map;
}; };
FilterHostnameDict.prototype.freezeBucket = function(bucket) {
var hostnames = Object.keys(bucket);
if ( hostnames[0].length * hostnames.length < this.cutoff ) {
return ' ' + hostnames.join(' ') + ' ';
}
return hostnames.sort().join('');
};
// How the key is derived dictates the number and size of buckets: // How the key is derived dictates the number and size of buckets:
// - more bits = more buckets = higher memory footprint // - more bits = more buckets = higher memory footprint
// - less bits = less buckets = lower memory footprint // - less bits = less buckets = lower memory footprint
@ -887,7 +895,7 @@ FilterHostnameDict.prototype.add = function(hn) {
return true; return true;
} }
if ( typeof bucket === 'string' ) { if ( typeof bucket === 'string' ) {
bucket = this.dict[key] = this.meltBucket(hn.len, bucket); bucket = this.dict[key] = this.meltBucket(hn.length, bucket);
} }
if ( bucket.hasOwnProperty(hn) ) { if ( bucket.hasOwnProperty(hn) ) {
return false; return false;
@ -899,19 +907,13 @@ FilterHostnameDict.prototype.add = function(hn) {
FilterHostnameDict.prototype.freeze = function() { FilterHostnameDict.prototype.freeze = function() {
var buckets = this.dict; var buckets = this.dict;
var bucket, hostnames, len; var bucket;
for ( var key in buckets ) { for ( var key in buckets ) {
bucket = buckets[key]; bucket = buckets[key];
if ( typeof bucket !== 'object' ) { if ( typeof bucket !== 'object' ) {
continue; continue;
} }
hostnames = Object.keys(bucket); buckets[key] = this.freezeBucket(bucket);
len = hostnames[0].length * hostnames.length;
if ( hostnames[0].length * hostnames.length < this.cutoff ) {
buckets[key] = ' ' + hostnames.join(' ') + ' ';
} else {
buckets[key] = hostnames.sort().join('');
}
} }
}; };
@ -924,7 +926,7 @@ FilterHostnameDict.prototype.matchesExactly = function(hn) {
return false; return false;
} }
if ( typeof bucket === 'object' ) { if ( typeof bucket === 'object' ) {
return bucket.hasOwnProperty(hn); bucket = this.dict[key] = this.freezeBucket(bucket);
} }
if ( bucket.charAt(0) === ' ' ) { if ( bucket.charAt(0) === ' ' ) {
return bucket.indexOf(' ' + hn + ' ') !== -1; return bucket.indexOf(' ' + hn + ' ') !== -1;

View File

@ -152,22 +152,26 @@
/******************************************************************************/ /******************************************************************************/
µBlock.appendUserFilters = function(filter) { µBlock.appendUserFilters = function(filters) {
if ( filter.length === 0 ) { if ( filters.length === 0 ) {
return; return;
} }
var µb = this; var µb = this;
var onCompiledListLoaded = function(details) { var onCompiledListLoaded = function() {
var compiledFilters = µb.compileFilters(filters);
var snfe = µb.staticNetFilteringEngine; var snfe = µb.staticNetFilteringEngine;
var cfe = µb.cosmeticFilteringEngine; var cfe = µb.cosmeticFilteringEngine;
var acceptedCount = snfe.acceptedCount + cfe.acceptedCount; var acceptedCount = snfe.acceptedCount + cfe.acceptedCount;
var duplicateCount = snfe.duplicateCount + cfe.duplicateCount; var duplicateCount = snfe.duplicateCount + cfe.duplicateCount;
µb.applyCompiledFilters(details.content); µb.applyCompiledFilters(compiledFilters);
var entry = µb.remoteBlacklists[µb.userFiltersPath]; var entry = µb.remoteBlacklists[µb.userFiltersPath];
entry.entryCount = snfe.acceptedCount + cfe.acceptedCount - acceptedCount; var deltaEntryCount = snfe.acceptedCount + cfe.acceptedCount - acceptedCount;
entry.entryUsedCount = entry.entryCount - snfe.duplicateCount - cfe.duplicateCount + duplicateCount; var deltaEntryUsedCount = deltaEntryCount - (snfe.duplicateCount + cfe.duplicateCount - duplicateCount);
entry.entryCount += deltaEntryCount;
entry.entryUsedCount += deltaEntryUsedCount;
vAPI.storage.set({ 'remoteBlacklists': µb.remoteBlacklists });
µb.staticNetFilteringEngine.freeze(); µb.staticNetFilteringEngine.freeze();
µb.cosmeticFilteringEngine.freeze(); µb.cosmeticFilteringEngine.freeze();
}; };
@ -187,7 +191,7 @@
// If we reached this point, the filter quite probably needs to be // If we reached this point, the filter quite probably needs to be
// added for sure: do not try to be too smart, trying to avoid // added for sure: do not try to be too smart, trying to avoid
// duplicates at this point may lead to more issues. // duplicates at this point may lead to more issues.
µb.saveUserFilters(details.content.trim() + '\n\n' + filter.trim(), onSaved); µb.saveUserFilters(details.content.trim() + '\n\n' + filters.trim(), onSaved);
}; };
this.loadUserFilters(onLoaded); this.loadUserFilters(onLoaded);