1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-11-07 03:12:33 +01: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.userScripts = new Map();
this.userScriptCache = new µb.MRUCache(32);
// Short-lived: content is valid only during one function call. These // Short-lived: content is valid only during one function call. These
// is to prevent repeated allocation/deallocation overheads -- the // is to prevent repeated allocation/deallocation overheads -- the
@ -722,7 +723,9 @@ FilterContainer.prototype.reset = function() {
this.scriptTagFilters = {}; this.scriptTagFilters = {};
this.scriptTagFilterCount = 0; this.scriptTagFilterCount = 0;
this.userScripts.clear(); this.userScripts.clear();
this.userScriptCache.reset();
}; };
/******************************************************************************/ /******************************************************************************/
@ -1734,19 +1737,26 @@ FilterContainer.prototype.retrieveUserScripts = function(
FilterContainer.prototype._lookupUserScript = function(raw, reng, toInject) { FilterContainer.prototype._lookupUserScript = function(raw, reng, toInject) {
if ( toInject.has(raw) ) { return; } if ( toInject.has(raw) ) { return; }
var token, args, if ( this.userScriptCache.resetTime < reng.modifyTime ) {
pos = raw.indexOf(','); this.userScriptCache.reset();
if ( pos === -1 ) {
token = raw;
} else {
token = raw.slice(0, pos).trim();
args = raw.slice(pos + 1).trim();
} }
var content = reng.resourceContentFromName(token, 'application/javascript'); var content = this.userScriptCache.lookup(raw);
if ( !content ) { return; } if ( content === undefined ) {
if ( args ) { var token, args,
content = this._fillupUserScript(content, 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 ( !content ) { return; }
if ( args ) {
content = this._fillupUserScript(content, args);
if ( !content ) { return; }
}
this.userScriptCache.add(raw, content);
} }
toInject.set(raw, content); toInject.set(raw, content);
}; };

View File

@ -1,7 +1,7 @@
/******************************************************************************* /*******************************************************************************
uBlock Origin - a browser extension to block requests. 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 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 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.ruleTypes = new Set();
this.ruleSources = new Set(); this.ruleSources = new Set();
this.ruleDestinations = 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); entries = this.rules.get(key);
if ( entries === undefined ) { if ( entries === undefined ) {
this.rules.set(key, [ { tok: redirect, pat: pattern } ]); this.rules.set(key, [ { tok: redirect, pat: pattern } ]);
this.modifyTime = Date.now();
return; return;
} }
var entry; var entry;
@ -363,6 +365,7 @@ RedirectEngine.prototype.fromSelfie = function(selfie) {
this.ruleTypes = new Set(selfie.ruleTypes); this.ruleTypes = new Set(selfie.ruleTypes);
this.ruleSources = new Set(selfie.ruleSources); this.ruleSources = new Set(selfie.ruleSources);
this.ruleDestinations = new Set(selfie.ruleDestinations); this.ruleDestinations = new Set(selfie.ruleDestinations);
this.modifyTime = Date.now();
return true; return true;
}; };

View File

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