Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: lib/domain.js

Issue 29988555: Issue [TBD] - Maintain cache of third-party check results (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Created Jan. 23, 2019, 8:46 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/domain.js
===================================================================
--- a/lib/domain.js
+++ b/lib/domain.js
@@ -15,16 +15,28 @@
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
*/
"use strict";
const publicSuffixes = require("../data/publicSuffixList.json");
/**
+ * Maximum number of entries to keep in <code>{@link cache}</code>.
+ * @type {number}
+ */
+const maxCacheEntries = 1000;
+
+/**
+ * Cached results from previous <code>{@link isThirdParty}</code> calls.
+ * @type {Map.<string,boolean>}
+ */
+let cache = new Map();
+
+/**
* Checks whether the given hostname is a domain.
*
* @param {string} hostname
* @returns {boolean}
*/
function isDomain(hostname)
{
// No hostname or IPv4 address, also considering hexadecimal octets.
@@ -69,21 +81,37 @@
*
* @param {URL} url The request URL.
* @param {string} documentHostname The IDNA-encoded hostname of the document.
*
* @returns {boolean}
*/
function isThirdParty(url, documentHostname)
{
- let requestHostname = url.hostname.replace(/\.+$/, "");
- documentHostname = documentHostname.replace(/\.+$/, "");
+ let requestHostname = url.hostname;
+
+ if (requestHostname[requestHostname.length - 1] == ".")
Manish Jethani 2019/01/23 08:51:06 Avoid calling replace() since it's very rarely nec
+ requestHostname = requestHostname.replace(/\.+$/, "");
+
+ if (documentHostname[documentHostname.length - 1] == ".")
+ documentHostname = documentHostname.replace(/\.+$/, "");
if (requestHostname == documentHostname)
Manish Jethani 2019/01/23 08:51:06 No need to cache if the hostnames are literally eq
return false;
- if (!isDomain(requestHostname) || !isDomain(documentHostname))
- return true;
+ let key = requestHostname + " " + documentHostname;
+ let value = cache.get(key);
+
+ if (typeof value != "undefined")
+ return value;
- return getDomain(requestHostname) != getDomain(documentHostname);
+ value = !isDomain(requestHostname) || !isDomain(documentHostname) ||
+ getDomain(requestHostname) != getDomain(documentHostname);
+
+ if (cache.size >= maxCacheEntries)
+ cache.clear();
+
+ cache.set(key, value);
+
+ return value;
}
exports.isThirdParty = isThirdParty;
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld