From 3b1bb0417dfbe385994669292f651112f5c92117 Mon Sep 17 00:00:00 2001 From: Benjamin Pasero Date: Thu, 21 Apr 2016 10:27:46 +0100 Subject: [PATCH] glob - cache check for pattern being trivia since this call is being made an order of magnitude --- src/vs/base/common/glob.ts | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/vs/base/common/glob.ts b/src/vs/base/common/glob.ts index b56e0dce020..5b29b353417 100644 --- a/src/vs/base/common/glob.ts +++ b/src/vs/base/common/glob.ts @@ -247,9 +247,19 @@ function toRegExp(regEx: string): RegExp { } } +function testWithCache(glob: string, pattern: RegExp, cache: { [glob: string]: boolean }): boolean { + if (typeof cache[glob] !== 'boolean') { + cache[glob] = pattern.test(glob); + } + + return cache[glob]; +} + // regexes to check for trival glob patterns that just check for String#endsWith const trivia1 = /^\*\*\/\*\.\w+$/; const trivia2 = /^{\*\*\/\*\.\w+(,\*\*\/\*\.\w+)*}$/; +const T1_CACHE: { [glob: string]: boolean } = Object.create(null); +const T2_CACHE: { [glob: string]: boolean } = Object.create(null); /** * Simplified glob matching. Supports a subset of glob patterns: @@ -269,16 +279,18 @@ export function match(arg1: string | IExpression, path: string, siblings?: strin // Glob with String if (typeof arg1 === 'string') { - if (trivia1.test(arg1)) { - // common pattern: **/*.txt just need endsWith check + // common pattern: **/*.txt just need endsWith check + if (testWithCache(arg1, trivia1, T1_CACHE)) { return strings.endsWith(path, arg1.substr(4)); // '**/*'.length === 4 + } - } else if (trivia2.test(arg1)) { - // repetition of common patterns (see above) {**/*.txt,**/*.png} + // repetition of common patterns (see above) {**/*.txt,**/*.png} + if (testWithCache(arg1, trivia2, T2_CACHE)) { return arg1.slice(1, -1).split(',').some(pattern => match(pattern, path)); } - var regExp = globToRegExp(arg1); + const regExp = globToRegExp(arg1); + return regExp && regExp.test(path); } -- GitLab