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

View File

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

View File

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