Perf: improve adguard filter parsing speed

This commit is contained in:
SukkaW 2025-07-27 23:50:52 +08:00
parent 31b36ee8f1
commit e9567f6488

View File

@ -165,7 +165,7 @@ const kwfilter = createKeywordFilter([
'(', '(',
']', ']',
')', ')',
',', ',', // $3p,doc
'#', '#',
'%', '%',
'&', '&',
@ -208,13 +208,14 @@ export class AdGuardFilterIgnoreUnsupportedLinesStream extends TransformStream<s
constructor() { constructor() {
super({ super({
transform(line, controller) { transform(line, controller) {
let firstCharCode = line.charCodeAt(0);
if ( if (
// bail out path-like/cosmetic very early, even before trim
firstCharCode === 47 // /
|| firstCharCode === 35 // #
// doesn't include // doesn't include
!line.includes('.') // rule with out dot can not be a domain || !line.includes('.') // rule with out dot can not be a domain
// includes || kwfilter(line) // filter out some symbols/modifiers
|| kwfilter(line)
// note that this can only excludes $redirect but not $3p,redirect, so we still need to parse it
// this is only an early bail out
) { ) {
return; return;
} }
@ -225,7 +226,7 @@ export class AdGuardFilterIgnoreUnsupportedLinesStream extends TransformStream<s
return; return;
} }
const firstCharCode = line.charCodeAt(0); firstCharCode = line.charCodeAt(0);
const lastCharCode = line.charCodeAt(line.length - 1); const lastCharCode = line.charCodeAt(line.length - 1);
if ( if (
@ -273,7 +274,7 @@ export function parse(line: string, result: [string, ParseType], includeThirdPar
// return result; // return result;
// } // }
const firstCharCode = line.charCodeAt(0); // const firstCharCode = line.charCodeAt(0);
// const lastCharCode = line.charCodeAt(line.length - 1); // const lastCharCode = line.charCodeAt(line.length - 1);
// if ( // if (
@ -401,7 +402,7 @@ export function parse(line: string, result: [string, ParseType], includeThirdPar
let includeAllSubDomain = false; let includeAllSubDomain = false;
if ( if (
firstCharCode === 64 // 64 `@` line.charCodeAt(0) === 64 // 64 `@`
&& line.charCodeAt(1) === 64 // 64 `@` && line.charCodeAt(1) === 64 // 64 `@`
) { ) {
sliceStart += 2; sliceStart += 2;
@ -430,7 +431,7 @@ export function parse(line: string, result: [string, ParseType], includeThirdPar
break; break;
case 46: { /** | */ // line.startsWith('@@.') || line.startsWith('.') case 46: { /** . */ // line.startsWith('@@.') || line.startsWith('.')
/** /**
* `.ay.delivery^` * `.ay.delivery^`
* `.m.bookben.com^` * `.m.bookben.com^`