reduce baseline memory at selfie-load time

This commit is contained in:
Raymond Hill 2018-06-01 07:54:31 -04:00
parent a56b0c8925
commit 798f8dab9d
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
5 changed files with 53 additions and 73 deletions

View File

@ -137,8 +137,8 @@ var µBlock = (function() { // jshint ignore:line
// Read-only // Read-only
systemSettings: { systemSettings: {
compiledMagic: 2, // Increase when compiled format changes compiledMagic: 3, // Increase when compiled format changes
selfieMagic: 2 // Increase when selfie format changes selfieMagic: 3 // Increase when selfie format changes
}, },
restoreBackupSettings: { restoreBackupSettings: {

View File

@ -408,12 +408,11 @@ RedirectEngine.prototype.toSelfie = function() {
} }
rules.push(rule); rules.push(rule);
} }
var µb = µBlock;
return { return {
rules: rules, rules: rules,
ruleTypes: µb.arrayFrom(this.ruleTypes), ruleTypes: Array.from(this.ruleTypes),
ruleSources: µb.arrayFrom(this.ruleSources), ruleSources: Array.from(this.ruleSources),
ruleDestinations: µb.arrayFrom(this.ruleDestinations) ruleDestinations: Array.from(this.ruleDestinations)
}; };
}; };
@ -508,7 +507,7 @@ RedirectEngine.prototype.selfieFromResources = function() {
vAPI.cacheStorage.set({ vAPI.cacheStorage.set({
resourcesSelfie: { resourcesSelfie: {
version: resourcesSelfieVersion, version: resourcesSelfieVersion,
resources: µBlock.arrayFrom(this.resources) resources: Array.from(this.resources)
} }
}); });
}; };

View File

@ -1139,7 +1139,7 @@ FilterHostnameDict.prototype.logData = function() {
}; };
FilterHostnameDict.prototype.compile = function() { FilterHostnameDict.prototype.compile = function() {
return [ this.fid, µb.arrayFrom(this.dict) ]; return [ this.fid, Array.from(this.dict) ];
}; };
FilterHostnameDict.load = function(args) { FilterHostnameDict.load = function(args) {
@ -2014,8 +2014,8 @@ FilterContainer.prototype.freeze = function() {
this.fdataLast = null; this.fdataLast = null;
this.filterLast = null; this.filterLast = null;
this.frozen = true; this.frozen = true;
//console.log(JSON.stringify(µb.arrayFrom(filterClassHistogram))); //console.log(JSON.stringify(Array.from(filterClassHistogram)));
//this.tokenHistogram = new Map(µb.arrayFrom(this.tokenHistogram).sort(function(a, b) { //this.tokenHistogram = new Map(Array.from(this.tokenHistogram).sort(function(a, b) {
// return a[0].localeCompare(b[0]) || (b[1] - a[1]); // return a[0].localeCompare(b[0]) || (b[1] - a[1]);
//})); //}));
}; };
@ -2023,27 +2023,27 @@ FilterContainer.prototype.freeze = function() {
/******************************************************************************/ /******************************************************************************/
FilterContainer.prototype.toSelfie = function() { FilterContainer.prototype.toSelfie = function() {
var categoriesToSelfie = function(categoryMap) { let categoriesToSelfie = function(categoryMap) {
var categoryEntries = []; let selfie = [];
for ( var categoryEntry of categoryMap ) { for ( let categoryEntry of categoryMap ) {
var tokenEntries = []; let tokenEntries = [];
for ( var tokenEntry of categoryEntry[1] ) { for ( let tokenEntry of categoryEntry[1] ) {
tokenEntries.push([ tokenEntry[0], tokenEntry[1].compile() ]); tokenEntries.push([ tokenEntry[0], tokenEntry[1].compile() ]);
} }
categoryEntries.push([ categoryEntry[0], tokenEntries ]); selfie.push([ categoryEntry[0], tokenEntries ]);
} }
return JSON.stringify(categoryEntries); return selfie;
}; };
var dataFiltersToSelfie = function(dataFilters) { let dataFiltersToSelfie = function(dataFilters) {
var selfie = []; let selfie = [];
for ( var entry of dataFilters.values() ) { for ( let entry of dataFilters.values() ) {
do { do {
selfie.push(entry.compile()); selfie.push(entry.compile());
entry = entry.next; entry = entry.next;
} while ( entry !== undefined ); } while ( entry !== undefined );
} }
return JSON.stringify(selfie); return selfie;
}; };
return { return {
@ -2069,28 +2069,17 @@ FilterContainer.prototype.fromSelfie = function(selfie) {
this.blockFilterCount = selfie.blockFilterCount; this.blockFilterCount = selfie.blockFilterCount;
this.discardedCount = selfie.discardedCount; this.discardedCount = selfie.discardedCount;
var entries; for ( let categoryEntry of selfie.categories ) {
let tokenMap = new Map();
var categoryMap = new Map(); for ( let tokenEntry of categoryEntry[1] ) {
entries = JSON.parse(selfie.categories);
for ( var i = 0, ni = entries.length; i < ni; i++ ) {
var categoryEntry = entries[i],
tokenMap = new Map();
var tokenEntries = categoryEntry[1];
for ( var j = 0, nj = tokenEntries.length; j < nj; j++ ) {
var tokenEntry = tokenEntries[j];
tokenMap.set(tokenEntry[0], filterFromCompiledData(tokenEntry[1])); tokenMap.set(tokenEntry[0], filterFromCompiledData(tokenEntry[1]));
} }
categoryMap.set(categoryEntry[0], tokenMap); this.categories.set(categoryEntry[0], tokenMap);
} }
this.categories = categoryMap;
entries = JSON.parse(selfie.dataFilters); for ( let dataEntry of selfie.dataFilters ) {
var entry, bucket; let entry = FilterDataHolderEntry.load(dataEntry);
i = entries.length; let bucket = this.dataFilters.get(entry.tokenHash);
while ( i-- ) {
entry = FilterDataHolderEntry.load(entries[i]);
bucket = this.dataFilters.get(entry.tokenHash);
if ( bucket !== undefined ) { if ( bucket !== undefined ) {
entry.next = bucket; entry.next = bucket;
} }

View File

@ -278,7 +278,7 @@
this.removeFilterList(oldKeys[i]); this.removeFilterList(oldKeys[i]);
} }
} }
newKeys = this.arrayFrom(newSet); newKeys = Array.from(newSet);
var bin = { var bin = {
selectedFilterLists: newKeys selectedFilterLists: newKeys
}; };
@ -358,10 +358,10 @@
} }
selectedListKeySet.add(assetKey); selectedListKeySet.add(assetKey);
} }
externalLists = this.arrayFrom(importedSet).sort().join('\n'); externalLists = Array.from(importedSet).sort().join('\n');
} }
var result = this.arrayFrom(selectedListKeySet); var result = Array.from(selectedListKeySet);
if ( externalLists !== this.userSettings.externalLists ) { if ( externalLists !== this.userSettings.externalLists ) {
this.userSettings.externalLists = externalLists; this.userSettings.externalLists = externalLists;
vAPI.storage.set({ externalLists: externalLists }); vAPI.storage.set({ externalLists: externalLists });
@ -387,7 +387,7 @@
} }
out.add(location); out.add(location);
} }
return this.arrayFrom(out); return Array.from(out);
}; };
/******************************************************************************/ /******************************************************************************/
@ -1018,23 +1018,27 @@
// some set time. // some set time.
µBlock.selfieManager = (function() { µBlock.selfieManager = (function() {
var timer = null; let µb = µBlock;
let timer = null;
var create = function() { // As of 2018-05-31:
// JSON.stringify-ing ourselves results in a better baseline
// memory usage at selfie-load time. For some reasons.
let create = function() {
timer = null; timer = null;
var selfie = { let selfie = {
magic: this.systemSettings.selfieMagic, magic: µb.systemSettings.selfieMagic,
availableFilterLists: this.availableFilterLists, availableFilterLists: JSON.stringify(µb.availableFilterLists),
staticNetFilteringEngine: this.staticNetFilteringEngine.toSelfie(), staticNetFilteringEngine: JSON.stringify(µb.staticNetFilteringEngine.toSelfie()),
redirectEngine: this.redirectEngine.toSelfie(), redirectEngine: JSON.stringify(µb.redirectEngine.toSelfie()),
staticExtFilteringEngine: this.staticExtFilteringEngine.toSelfie() staticExtFilteringEngine: JSON.stringify(µb.staticExtFilteringEngine.toSelfie())
}; };
vAPI.cacheStorage.set({ selfie: selfie }); vAPI.cacheStorage.set({ selfie: selfie });
}.bind(µBlock); };
var load = function(callback) { let load = function(callback) {
vAPI.cacheStorage.get('selfie', function(bin) { vAPI.cacheStorage.get('selfie', function(bin) {
var µb = µBlock;
if ( if (
bin instanceof Object === false || bin instanceof Object === false ||
bin.selfie instanceof Object === false || bin.selfie instanceof Object === false ||
@ -1043,22 +1047,22 @@
) { ) {
return callback(false); return callback(false);
} }
µb.availableFilterLists = bin.selfie.availableFilterLists; µb.availableFilterLists = JSON.parse(bin.selfie.availableFilterLists);
µb.staticNetFilteringEngine.fromSelfie(bin.selfie.staticNetFilteringEngine); µb.staticNetFilteringEngine.fromSelfie(JSON.parse(bin.selfie.staticNetFilteringEngine));
µb.redirectEngine.fromSelfie(bin.selfie.redirectEngine); µb.redirectEngine.fromSelfie(JSON.parse(bin.selfie.redirectEngine));
µb.staticExtFilteringEngine.fromSelfie(bin.selfie.staticExtFilteringEngine); µb.staticExtFilteringEngine.fromSelfie(JSON.parse(bin.selfie.staticExtFilteringEngine));
callback(true); callback(true);
}); });
}; };
var destroy = function() { let destroy = function() {
if ( timer !== null ) { if ( timer !== null ) {
clearTimeout(timer); clearTimeout(timer);
timer = null; timer = null;
} }
vAPI.cacheStorage.remove('selfie'); vAPI.cacheStorage.remove('selfie');
timer = vAPI.setTimeout(create, this.selfieAfter); timer = vAPI.setTimeout(create, µb.selfieAfter);
}.bind(µBlock); };
return { return {
load: load, load: load,

View File

@ -357,18 +357,6 @@
/******************************************************************************/ /******************************************************************************/
µBlock.arrayFrom = typeof Array.from === 'function'
? Array.from
: function(iterable) {
var out = [], i = 0;
for ( var value of iterable ) {
out[i++] = value;
}
return out;
};
/******************************************************************************/
µBlock.openNewTab = function(details) { µBlock.openNewTab = function(details) {
if ( details.url.startsWith('logger-ui.html') ) { if ( details.url.startsWith('logger-ui.html') ) {
if ( details.shiftKey ) { if ( details.shiftKey ) {