Perf: replace sha256 w/ xxhash

This commit is contained in:
SukkaW
2024-10-08 21:32:56 +08:00
parent 14ed4c01e5
commit 4f76dd089a
4 changed files with 30 additions and 61 deletions

View File

@@ -10,8 +10,7 @@ import picocolors from 'picocolors';
import createKeywordFilter from './aho-corasick';
import { createCacheKey, deserializeArray, fsFetchCache, serializeArray } from './cache-filesystem';
import { fastStringArrayJoin } from './misc';
import { sha256 } from 'hash-wasm';
import { stringHash } from './string-hash';
const BLACK_TLD = new Set([
'accountant', 'autos',
@@ -114,16 +113,17 @@ export const getPhishingDomains = (parentSpan: Span) => parentSpan.traceChild('g
return domainArr;
});
const cacheHash = span.traceChildSync('get hash', () => stringHash(fastStringArrayJoin(domainArr, '|')));
return span.traceChildAsync(
'process phishing domain set',
() => processPhihsingDomains(domainArr)
() => processPhihsingDomains(domainArr, cacheHash)
);
});
async function processPhihsingDomains(domainArr: string[]) {
const hash = await sha256(fastStringArrayJoin(domainArr, '|'));
async function processPhihsingDomains(domainArr: string[], cacheHash = '') {
return fsFetchCache.apply(
cacheKey('processPhihsingDomains|' + hash),
cacheKey('processPhihsingDomains|' + cacheHash),
() => {
const domainCountMap: Record<string, number> = {};
const domainScoreMap: Record<string, number> = {};

View File

@@ -48,17 +48,19 @@ export class TextLineStream extends TransformStream<string, string> {
}
}
if (lfIndex !== -1) {
let crOrLfIndex = lfIndex;
if (chunk[lfIndex - 1] === '\r') {
crOrLfIndex--;
}
controller.enqueue(chunk.slice(chunkIndex, crOrLfIndex));
chunkIndex = lfIndex + 1;
continue;
if (lfIndex === -1) {
// we can no longer find a line break in the chunk, break the current loop
break;
}
break;
// enqueue current line, and loop again to find next line
let crOrLfIndex = lfIndex;
if (chunk[lfIndex - 1] === '\r') {
crOrLfIndex--;
}
controller.enqueue(chunk.slice(chunkIndex, crOrLfIndex));
chunkIndex = lfIndex + 1;
continue;
}
__buf = chunk.slice(chunkIndex);