From 74d627009bec1ce9e3f75b8dadc1d690a88a1c1f Mon Sep 17 00:00:00 2001 From: SukkaW Date: Fri, 16 Sep 2022 17:48:04 +0800 Subject: [PATCH] Perf: improve reject set domain worker performance --- Build/worker/build-reject-domainset-worker.js | 39 +++++++++++-------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/Build/worker/build-reject-domainset-worker.js b/Build/worker/build-reject-domainset-worker.js index 9871d88e..cccbccab 100644 --- a/Build/worker/build-reject-domainset-worker.js +++ b/Build/worker/build-reject-domainset-worker.js @@ -1,9 +1,11 @@ const { workerData, move } = require('piscina'); -const len = workerData.length; // pre check if fullset domain is starts with a "." // This avoid calling chatCodeAt repeatedly -const fullsetDomainStartsWithADot = workerData.map(domain => domain.charCodeAt(0) === 46); + +// workerData is an array of string. Sort it by length, short first: +const fullsetDomainStartsWithADot = workerData.sort((a, b) => a.length - b.length).filter(domain => domain.charCodeAt(0) === 46); +const totalLen = fullsetDomainStartsWithADot.length; module.exports = ({ chunk }) => { const chunkLength = chunk.length; @@ -12,35 +14,38 @@ module.exports = ({ chunk }) => { for (let i = 0; i < chunkLength; i++) { const domainFromInput = chunk[i]; - for (let j = 0; j < len; j++) { - // Check if domainFromFullset starts with a "." - if (!fullsetDomainStartsWithADot[j]) continue; + for (let j = 0; j < totalLen; j++) { + const domainFromFullSet = fullsetDomainStartsWithADot[j]; // domainFromFullSet is now startsWith a "." - const domainFromFullSet = workerData[j]; - if (domainFromFullSet === domainFromInput) continue; const domainFromInputLen = domainFromInput.length; + const domainFromFullSetLen = domainFromFullSet.length; + // !domainFromInput.starsWith('.') && `.${domainFromInput}` === domainFromFullSet if (domainFromInput.charCodeAt(0) !== 46) { - let shouldBeRemoved = true; + if (domainFromInputLen + 1 === domainFromFullSetLen) { - for (let k = 0; k < domainFromInputLen; k++) { - if (domainFromFullSet.charCodeAt(k + 1) !== domainFromInput.charCodeAt(k)) { - shouldBeRemoved = false; + let shouldBeRemoved = true; + + for (let k = 0; k < domainFromInputLen; k++) { + if (domainFromFullSet.charCodeAt(k + 1) !== domainFromInput.charCodeAt(k)) { + shouldBeRemoved = false; + break; + } + } + + if (shouldBeRemoved) { + outputToBeRemoved[i] = 1; break; } } - - if (shouldBeRemoved) { - outputToBeRemoved[i] = 1; - break; - } } + // domainFromInput is now startsWith a "." - if (domainFromInputLen >= domainFromFullSet.length) { + if (domainFromInputLen >= domainFromFullSetLen) { if (domainFromInput.endsWith(domainFromFullSet)) { outputToBeRemoved[i] = 1; break;