1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-09-15 07:22:28 +02:00

code review: cache most-recently-used pre-filled scriptlets

This commit is contained in:
Raymond Hill 2017-12-21 17:05:25 -05:00
parent 4a09c9f866
commit 607968de7f
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
3 changed files with 27 additions and 12 deletions

View File

@ -665,6 +665,7 @@ var FilterContainer = function() {
};
this.userScripts = new Map();
this.userScriptCache = new µb.MRUCache(32);
// Short-lived: content is valid only during one function call. These
// is to prevent repeated allocation/deallocation overheads -- the
@ -722,7 +723,9 @@ FilterContainer.prototype.reset = function() {
this.scriptTagFilters = {};
this.scriptTagFilterCount = 0;
this.userScripts.clear();
this.userScriptCache.reset();
};
/******************************************************************************/
@ -1734,19 +1737,26 @@ FilterContainer.prototype.retrieveUserScripts = function(
FilterContainer.prototype._lookupUserScript = function(raw, reng, toInject) {
if ( toInject.has(raw) ) { return; }
var token, args,
pos = raw.indexOf(',');
if ( pos === -1 ) {
token = raw;
} else {
token = raw.slice(0, pos).trim();
args = raw.slice(pos + 1).trim();
if ( this.userScriptCache.resetTime < reng.modifyTime ) {
this.userScriptCache.reset();
}
var content = reng.resourceContentFromName(token, 'application/javascript');
if ( !content ) { return; }
if ( args ) {
content = this._fillupUserScript(content, args);
var content = this.userScriptCache.lookup(raw);
if ( content === undefined ) {
var token, args,
pos = raw.indexOf(',');
if ( pos === -1 ) {
token = raw;
} else {
token = raw.slice(0, pos).trim();
args = raw.slice(pos + 1).trim();
}
content = reng.resourceContentFromName(token, 'application/javascript');
if ( !content ) { return; }
if ( args ) {
content = this._fillupUserScript(content, args);
if ( !content ) { return; }
}
this.userScriptCache.add(raw, content);
}
toInject.set(raw, content);
};

View File

@ -1,7 +1,7 @@
/*******************************************************************************
uBlock Origin - a browser extension to block requests.
Copyright (C) 2015-2016 Raymond Hill
Copyright (C) 2015-2017 Raymond Hill
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@ -95,6 +95,7 @@ RedirectEngine.prototype.reset = function() {
this.ruleTypes = new Set();
this.ruleSources = new Set();
this.ruleDestinations = new Set();
this.modifyTime = Date.now();
};
/******************************************************************************/
@ -189,6 +190,7 @@ RedirectEngine.prototype.addRule = function(src, des, type, pattern, redirect) {
entries = this.rules.get(key);
if ( entries === undefined ) {
this.rules.set(key, [ { tok: redirect, pat: pattern } ]);
this.modifyTime = Date.now();
return;
}
var entry;
@ -363,6 +365,7 @@ RedirectEngine.prototype.fromSelfie = function(selfie) {
this.ruleTypes = new Set(selfie.ruleTypes);
this.ruleSources = new Set(selfie.ruleSources);
this.ruleDestinations = new Set(selfie.ruleDestinations);
this.modifyTime = Date.now();
return true;
};

View File

@ -351,6 +351,7 @@
this.size = size;
this.array = [];
this.map = new Map();
this.resetTime = Date.now();
};
µBlock.MRUCache.prototype = {
@ -380,6 +381,7 @@
reset: function() {
this.array = [];
this.map.clear();
this.resetTime = Date.now();
}
};