-
-
Notifications
You must be signed in to change notification settings - Fork 35.7k
fs: cache glob matcher functions in path.matchesGlob #63915
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| 'use strict'; | ||
| const common = require('../common.js'); | ||
| const { posix } = require('path'); | ||
| const assert = require('assert'); | ||
|
|
||
| const bench = common.createBenchmark(main, { | ||
| path: [ | ||
| 'src/index.ts', | ||
| 'src/index.test.ts', | ||
| 'src/foo/bar/biz/baz/index.test.ts', | ||
| ], | ||
| pattern: [ | ||
| 'src/**/baz/*.test.ts', | ||
| 'src/**/baz/*.ts', | ||
| 'test/**/*.ts', | ||
| ], | ||
| n: [1e5], | ||
| }); | ||
|
|
||
| function main({ path, pattern, n }) { | ||
| bench.start(); | ||
| let a; | ||
| for (let i = 0; i < n; i++) { | ||
| a = posix.matchesGlob(path, pattern); | ||
| } | ||
| bench.end(n); | ||
| assert(a + 'a'); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| 'use strict'; | ||
| const common = require('../common.js'); | ||
| const { win32 } = require('path'); | ||
| const assert = require('assert'); | ||
|
|
||
| const bench = common.createBenchmark(main, { | ||
| path: [ | ||
| 'src/index.ts', | ||
| 'src\\index.ts', | ||
| 'src/foo/bar/biz/baz/index.test.ts', | ||
| 'src\\foo\\bar\\biz\\baz\\index.test.ts', | ||
| ], | ||
| pattern: [ | ||
| 'src/**/baz/*.test.ts', | ||
| 'src/**/baz/*.ts', | ||
| 'test/**/*.ts', | ||
| ], | ||
| n: [1e5], | ||
| }); | ||
|
|
||
| function main({ path, pattern, n }) { | ||
| bench.start(); | ||
| let a; | ||
| for (let i = 0; i < n; i++) { | ||
| a = win32.matchesGlob(path, pattern); | ||
| } | ||
| bench.end(n); | ||
| assert(a + 'a'); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -922,6 +922,8 @@ class Glob { | |
| } | ||
| } | ||
|
|
||
| let patternFnCache; | ||
|
|
||
| /** | ||
| * Check if a path matches a glob pattern | ||
| * @param {string} path the path to check | ||
|
|
@@ -932,16 +934,25 @@ class Glob { | |
| function matchGlobPattern(path, pattern, windows = isWindows) { | ||
| validateString(path, 'path'); | ||
| validateString(pattern, 'pattern'); | ||
| return lazyMinimatch().minimatch(path, pattern, { | ||
| kEmptyObject, | ||
| nocase: isMacOS || isWindows, | ||
| windowsPathsNoEscape: true, | ||
| nonegate: true, | ||
| nocomment: true, | ||
| optimizationLevel: 2, | ||
| platform: windows ? 'win32' : 'posix', | ||
| nocaseMagicOnly: true, | ||
| }); | ||
|
|
||
| patternFnCache ??= new SafeMap(); | ||
|
|
||
| let matcher; | ||
| if (patternFnCache.has(pattern)) { | ||
| matcher = patternFnCache.get(pattern); | ||
| } else { | ||
| matcher = createMatcher(pattern, { | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i re-used i thought about putting the caching in that function instead, but then we would need to base the caching on all the options it accepts, so i decided to keep it here. |
||
| kEmptyObject, | ||
| platform: windows ? 'win32' : 'posix', | ||
| }); | ||
| patternFnCache.set(pattern, matcher); | ||
|
|
||
| if (patternFnCache.size >= 250) { | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. randomly picked number, i'm not sure how to find a "good" value though. |
||
| patternFnCache.delete(patternFnCache.keys().next().value); | ||
| } | ||
| } | ||
|
|
||
| return matcher.match(path); | ||
| } | ||
|
|
||
| module.exports = { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i'm not sure if it's worth it to delay the map instantiation like this, but did it just in case.