From f6ad57c2edbd22dcf84a5b0ec5b4f480ad27f5b1 Mon Sep 17 00:00:00 2001 From: Joe Lencioni Date: Fri, 5 Jun 2020 08:26:58 -0500 Subject: [PATCH] Cache micromatch in SearchSource globsToMatcher I was profiling some Jest runs at Airbnb and noticed that on my MacBook Pro, we can spend over 2 seconds at Jest startup time in SearchSource getTestPaths. I believe that this will grow as the size of the codebase increases. Looking at the call stacks, it appears to be calling micromatch repeatedly, which calls picomatch, which builds a regex out of the globs. It seems that the parsing and regex building also triggers the garbage collector frequently. Upon testing, I noticed that the globs don't actually change between these calls, so we can safe a bunch of work by making a micromatch matcher and reusing that function if the globs haven't changed. In my basic testing, it is always called with the same set of globs, so I went with a very simple last-used memoization instead of a more robust caching solution. In my profiling of this change locally, this brings down the time of startRun from about 2000ms to about 200ms. --- CHANGELOG.md | 2 + packages/jest-core/src/SearchSource.ts | 53 +++++++++++++++++++++++++- 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 85c66a5d976f..f881d9a48448 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,8 @@ ### Performance +- `[jest-core]` Cache micromatch in SearchSource globsToMatcher + ## 26.0.1 ### Fixes diff --git a/packages/jest-core/src/SearchSource.ts b/packages/jest-core/src/SearchSource.ts index 48c630f2adb4..763e868eba14 100644 --- a/packages/jest-core/src/SearchSource.ts +++ b/packages/jest-core/src/SearchSource.ts @@ -37,8 +37,57 @@ export type TestSelectionConfig = { watch?: boolean; }; -const globsToMatcher = (globs: Array) => (path: Config.Path) => - micromatch([replacePathSepForGlob(path)], globs, {dot: true}).length > 0; +const globsMatchers = new Map< + string, + { + isMatch: (str: string) => boolean; + negated: boolean; + } +>(); + +const globsToMatcher = (globs: Array) => { + const matchers = globs.map(glob => { + if (!globsMatchers.has(glob)) { + const state = micromatch.scan(glob, {dot: true}); + const matcher = { + isMatch: micromatch.matcher(glob, {dot: true}), + negated: state.negated, + }; + globsMatchers.set(glob, matcher); + } + return globsMatchers.get(glob); + }); + + return (path: Config.Path) => { + const omit = new Set(); + const keep = new Set(); + let negatives = 0; + const replacedPath = replacePathSepForGlob(path); + + for (let i = 0; i < matchers.length; i++) { + const matcher = matchers[i]; + if (!matcher) continue; + const {isMatch, negated} = matcher; + if (negated) negatives++; + + const matched = isMatch(replacedPath); + + const match = negated ? !matched : matched; + if (!match) continue; + + if (negated) { + omit.add(replacedPath); + } else { + omit.delete(replacedPath); + keep.add(replacedPath); + } + } + + const result = negatives === matchers.length ? [replacedPath] : [...keep]; + + return result.some(item => !omit.has(item)); + }; +}; const regexToMatcher = (testRegex: Config.ProjectConfig['testRegex']) => ( path: Config.Path,