Skip to content

Commit

Permalink
perf: optimizing the patterns set matching by exiting early
Browse files Browse the repository at this point in the history
  • Loading branch information
mrmlnc committed Jan 4, 2025
1 parent ea113fd commit 55c7b33
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions src/providers/filters/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,21 +70,39 @@ export default class EntryFilter {
}

private _isMatchToPatternsSet(filepath: string, patterns: PatternsRegexSet, isDirectory: boolean): boolean {
let fullpath = filepath;
const isMatched = this._isMatchToPatterns(filepath, patterns.positive.all, isDirectory);
if (!isMatched) {
return false;
}

const isMatchedByRelativeNegative = this._isMatchToPatterns(filepath, patterns.negative.relative, isDirectory);
if (isMatchedByRelativeNegative) {
return false;
}

if (patterns.negative.absolute.length > 0) {
fullpath = utils.path.makeAbsolute(this._settings.cwd, filepath);
const isMatchedByAbsoluteNegative = this._isMatchToAbsoluteNegative(filepath, patterns.negative.absolute, isDirectory);
if (isMatchedByAbsoluteNegative) {
return false;
}

const isMatched = this._isMatchToPatterns(filepath, patterns.positive.all, isDirectory);
return true;
}

private _isMatchToAbsoluteNegative(filepath: string, patternsRe: PatternRe[], isDirectory: boolean): boolean {
if (patternsRe.length === 0) {
return false;
}

return isMatched && !(
this._isMatchToPatterns(filepath, patterns.negative.relative, isDirectory) ||
this._isMatchToPatterns(fullpath, patterns.negative.absolute, isDirectory)
);
const fullpath = utils.path.makeAbsolute(this._settings.cwd, filepath);

return this._isMatchToPatterns(fullpath, patternsRe, isDirectory);
}

private _isMatchToPatterns(filepath: string, patternsRe: PatternRe[], isDirectory: boolean): boolean {
if (patternsRe.length === 0) {
return false;
}

// Trying to match files and directories by patterns.
const isMatched = utils.pattern.matchAny(filepath, patternsRe);

Expand Down

0 comments on commit 55c7b33

Please sign in to comment.