Perf: reduce condition when output to strategy

This commit is contained in:
SukkaW 2025-05-12 00:35:40 +08:00
parent 02e07668d0
commit f926970b42
3 changed files with 67 additions and 84 deletions

View File

@ -13,7 +13,7 @@ import { SurgeMitmSgmodule } from '../writing-strategy/surge';
* This class is not about format, instead it will call the class that does * This class is not about format, instead it will call the class that does
*/ */
export class FileOutput { export class FileOutput {
protected strategies: Array<BaseWriteStrategy | false> = []; protected strategies: BaseWriteStrategy[] = [];
public domainTrie = new HostnameSmolTrie(null); public domainTrie = new HostnameSmolTrie(null);
protected domainKeywords = new Set<string>(); protected domainKeywords = new Set<string>();
@ -57,16 +57,14 @@ export class FileOutput {
return this; return this;
} }
public withStrategies(strategies: Array<BaseWriteStrategy | false>) { public withStrategies(strategies: BaseWriteStrategy[]) {
this.strategies = strategies; this.strategies = strategies;
return this; return this;
} }
withExtraStrategies(strategy: BaseWriteStrategy | false) { withExtraStrategies(strategy: BaseWriteStrategy) {
if (strategy) {
this.strategies.push(strategy); this.strategies.push(strategy);
} }
}
protected description: string[] | readonly string[] | null = null; protected description: string[] | readonly string[] | null = null;
withDescription(description: string[] | readonly string[]) { withDescription(description: string[] | readonly string[]) {
@ -304,27 +302,24 @@ export class FileOutput {
throw new Error('No strategies to write ' + this.id); throw new Error('No strategies to write ' + this.id);
} }
const strategiesLen = this.strategies.length;
this.domainTrie.dumpWithoutDot((domain, includeAllSubdomain) => { this.domainTrie.dumpWithoutDot((domain, includeAllSubdomain) => {
if (kwfilter(domain)) { if (kwfilter(domain)) {
return; return;
} }
for (let i = 0, len = this.strategies.length; i < len; i++) { for (let i = 0; i < strategiesLen; i++) {
const strategy = this.strategies[i]; const strategy = this.strategies[i];
if (strategy) {
if (includeAllSubdomain) { if (includeAllSubdomain) {
strategy.writeDomainSuffix(domain); strategy.writeDomainSuffix(domain);
} else { } else {
strategy.writeDomain(domain); strategy.writeDomain(domain);
} }
} }
}
}, true); }, true);
for (let i = 0, len = this.strategies.length; i < len; i++) { for (let i = 0, len = this.strategies.length; i < len; i++) {
const strategy = this.strategies[i]; const strategy = this.strategies[i];
if (!strategy) continue;
if (this.domainKeywords.size) { if (this.domainKeywords.size) {
strategy.writeDomainKeywords(this.domainKeywords); strategy.writeDomainKeywords(this.domainKeywords);
} }
@ -348,16 +343,12 @@ export class FileOutput {
if (this.sourceIpOrCidr.size) { if (this.sourceIpOrCidr.size) {
const sourceIpOrCidr = Array.from(this.sourceIpOrCidr); const sourceIpOrCidr = Array.from(this.sourceIpOrCidr);
for (let i = 0, len = this.strategies.length; i < len; i++) { for (let i = 0, len = this.strategies.length; i < len; i++) {
const strategy = this.strategies[i]; this.strategies[i].writeSourceIpCidrs(sourceIpOrCidr);
if (strategy) {
strategy.writeSourceIpCidrs(sourceIpOrCidr);
}
} }
} }
for (let i = 0, len = this.strategies.length; i < len; i++) { for (let i = 0, len = this.strategies.length; i < len; i++) {
const strategy = this.strategies[i]; const strategy = this.strategies[i];
if (strategy) {
if (this.sourcePort.size) { if (this.sourcePort.size) {
strategy.writeSourcePorts(this.sourcePort); strategy.writeSourcePorts(this.sourcePort);
} }
@ -371,7 +362,6 @@ export class FileOutput {
strategy.writeUrlRegexes(this.urlRegex); strategy.writeUrlRegexes(this.urlRegex);
} }
} }
}
let ipcidr: string[] | null = null; let ipcidr: string[] | null = null;
let ipcidrNoResolve: string[] | null = null; let ipcidrNoResolve: string[] | null = null;
@ -393,7 +383,6 @@ export class FileOutput {
for (let i = 0, len = this.strategies.length; i < len; i++) { for (let i = 0, len = this.strategies.length; i < len; i++) {
const strategy = this.strategies[i]; const strategy = this.strategies[i];
if (strategy) {
// no-resolve // no-resolve
if (ipcidrNoResolve?.length) { if (ipcidrNoResolve?.length) {
strategy.writeIpCidrs(ipcidrNoResolve, true); strategy.writeIpCidrs(ipcidrNoResolve, true);
@ -423,7 +412,6 @@ export class FileOutput {
} }
} }
} }
}
write(): Promise<unknown> { write(): Promise<unknown> {
return this.span.traceChildAsync('write all', async (childSpan) => { return this.span.traceChildAsync('write all', async (childSpan) => {
@ -435,7 +423,7 @@ export class FileOutput {
for (let i = 0, len = this.strategies.length; i < len; i++) { for (let i = 0, len = this.strategies.length; i < len; i++) {
const strategy = this.strategies[i]; const strategy = this.strategies[i];
if (strategy) {
const basename = (strategy.overwriteFilename || this.id) + '.' + strategy.fileExtension; const basename = (strategy.overwriteFilename || this.id) + '.' + strategy.fileExtension;
promises.push( promises.push(
childSpan.traceChildAsync('write ' + strategy.name, (childSpan) => Promise.resolve(strategy.output( childSpan.traceChildAsync('write ' + strategy.name, (childSpan) => Promise.resolve(strategy.output(
@ -452,7 +440,6 @@ export class FileOutput {
))) )))
); );
} }
}
return Promise.all(promises); return Promise.all(promises);
}); });
@ -464,11 +451,7 @@ export class FileOutput {
this.writeToStrategies(); this.writeToStrategies();
return this.strategies.reduce<Array<string[] | null>>((acc, strategy) => { return this.strategies.reduce<Array<string[] | null>>((acc, strategy) => {
if (strategy) {
acc.push(strategy.content); acc.push(strategy.content);
} else {
acc.push(null);
}
return acc; return acc;
}, []); }, []);
} }

View File

@ -7,7 +7,7 @@ import { SurgeDomainSet } from '../writing-strategy/surge';
import { FileOutput } from './base'; import { FileOutput } from './base';
export class DomainsetOutput extends FileOutput { export class DomainsetOutput extends FileOutput {
strategies: Array<false | BaseWriteStrategy> = [ strategies: BaseWriteStrategy[] = [
new SurgeDomainSet(), new SurgeDomainSet(),
new ClashDomainSet(), new ClashDomainSet(),
new SingboxSource('domainset') new SingboxSource('domainset')
@ -15,7 +15,7 @@ export class DomainsetOutput extends FileOutput {
} }
export class AdGuardHomeOutput extends FileOutput { export class AdGuardHomeOutput extends FileOutput {
strategies: Array<false | BaseWriteStrategy>; strategies: BaseWriteStrategy[];
constructor( constructor(
span: Span, span: Span,

View File

@ -6,7 +6,7 @@ import { SurgeRuleSet } from '../writing-strategy/surge';
import { FileOutput } from './base'; import { FileOutput } from './base';
export class IPListOutput extends FileOutput { export class IPListOutput extends FileOutput {
strategies: Array<false | BaseWriteStrategy>; strategies: BaseWriteStrategy[];
constructor(span: Span, id: string, private readonly clashUseRule = true) { constructor(span: Span, id: string, private readonly clashUseRule = true) {
super(span, id); super(span, id);