From 7a7a97e4d0e6d439388daacd6b6334509203bfed Mon Sep 17 00:00:00 2001 From: SukkaW Date: Mon, 12 Sep 2022 23:27:15 +0800 Subject: [PATCH] Perf: improve reject set dedupe worker performance --- Build/worker/build-reject-domainset-worker.js | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/Build/worker/build-reject-domainset-worker.js b/Build/worker/build-reject-domainset-worker.js index f15452a1..9871d88e 100644 --- a/Build/worker/build-reject-domainset-worker.js +++ b/Build/worker/build-reject-domainset-worker.js @@ -1,25 +1,32 @@ 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); module.exports = ({ chunk }) => { const chunkLength = chunk.length; - const outputToBeRemoved = new Int32Array(chunkLength); + const outputToBeRemoved = new Int8Array(chunkLength); 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; + // domainFromFullSet is now startsWith a "." + const domainFromFullSet = workerData[j]; if (domainFromFullSet === domainFromInput) continue; - if (domainFromFullSet.charCodeAt(0) !== 46) continue; - // domainFromFullSet is now startsWith a "." + + const domainFromInputLen = domainFromInput.length; if (domainFromInput.charCodeAt(0) !== 46) { let shouldBeRemoved = true; - for (let k = 0, l2 = domainFromInput.length; k < l2; k++) { + for (let k = 0; k < domainFromInputLen; k++) { if (domainFromFullSet.charCodeAt(k + 1) !== domainFromInput.charCodeAt(k)) { shouldBeRemoved = false; break; @@ -33,7 +40,7 @@ module.exports = ({ chunk }) => { } // domainFromInput is now startsWith a "." - if (domainFromInput.length >= domainFromFullSet.length) { + if (domainFromInputLen >= domainFromFullSet.length) { if (domainFromInput.endsWith(domainFromFullSet)) { outputToBeRemoved[i] = 1; break;