 Issue 29994555:
  Issue 7250 - Optimize third-party request check  (Closed) 
  Base URL: https://hg.adblockplus.org/adblockpluscore/
    
  
    Issue 29994555:
  Issue 7250 - Optimize third-party request check  (Closed) 
  Base URL: https://hg.adblockplus.org/adblockpluscore/| Index: lib/domain.js | 
| =================================================================== | 
| --- a/lib/domain.js | 
| +++ b/lib/domain.js | 
| @@ -14,16 +14,39 @@ | 
| * You should have received a copy of the GNU General Public License | 
| * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 
| */ | 
| "use strict"; | 
| const publicSuffixes = require("../data/publicSuffixList.json"); | 
| +let publicSuffixMap = buildPublicSuffixMap(); | 
| 
Manish Jethani
2019/01/31 10:14:15
Maps are faster for dynamic lookups (where the pro
 | 
| + | 
| +function buildPublicSuffixMap() | 
| +{ | 
| + let map = new Map(); | 
| + | 
| + for (let key in publicSuffixes) | 
| + map.set(key, publicSuffixes[key]); | 
| + | 
| + return map; | 
| +} | 
| + | 
| +function* suffixes(domain) | 
| 
Manish Jethani
2019/01/31 10:14:15
We will reuse this function in other places (e.g.
 | 
| +{ | 
| + while (domain != "") | 
| + { | 
| + yield domain; | 
| + | 
| + let dotIndex = domain.indexOf("."); | 
| + domain = dotIndex == -1 ? "" : domain.substr(dotIndex + 1); | 
| + } | 
| +} | 
| + | 
| /** | 
| * Checks whether the given hostname is a domain. | 
| * | 
| * @param {string} hostname | 
| * @returns {boolean} | 
| */ | 
| function isDomain(hostname) | 
| { | 
| @@ -39,48 +62,60 @@ | 
| /** | 
| * Gets the base domain for the given hostname. | 
| * | 
| * @param {string} hostname | 
| * @returns {string} | 
| */ | 
| function getDomain(hostname) | 
| 
Manish Jethani
2019/01/31 10:14:15
At least twice as fast as the previous implementat
 | 
| { | 
| - let bits = hostname.split("."); | 
| - let cutoff = bits.length - 2; | 
| + let slices = []; | 
| + let cutoff = null; | 
| - for (let i = 0; i < bits.length; i++) | 
| + for (let suffix of suffixes(hostname)) | 
| { | 
| - let offset = publicSuffixes[bits.slice(i).join(".")]; | 
| + slices.push(suffix); | 
| + | 
| + let offset = publicSuffixMap.get(suffix); | 
| if (typeof offset != "undefined") | 
| { | 
| - cutoff = i - offset; | 
| + cutoff = slices.length - 1 - offset; | 
| break; | 
| } | 
| } | 
| + if (cutoff == null) | 
| + return slices.length > 2 ? slices[slices.length - 2] : hostname; | 
| + | 
| if (cutoff <= 0) | 
| return hostname; | 
| - return bits.slice(cutoff).join("."); | 
| + return slices[cutoff]; | 
| } | 
| +exports.getDomain = getDomain; | 
| + | 
| /** | 
| * Checks whether a request's origin is different from its document's origin. | 
| * | 
| * @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] == ".") | 
| + requestHostname = requestHostname.replace(/\.+$/, ""); | 
| + | 
| + if (documentHostname[documentHostname.length - 1] == ".") | 
| + documentHostname = documentHostname.replace(/\.+$/, ""); | 
| if (requestHostname == documentHostname) | 
| return false; | 
| if (!isDomain(requestHostname) || !isDomain(documentHostname)) | 
| return true; | 
| return getDomain(requestHostname) != getDomain(documentHostname); |