-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
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
perf: cache compiled glob #10037
perf: cache compiled glob #10037
Conversation
const allGlobsMatch = picomatch( | ||
result.matches.flatMap((i) => i.globsResolved) | ||
) | ||
server._importGlobMap.set(id, allGlobsMatch) |
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.
before
type glob = string
const allGlobs: glob[][] = result.matches.map((i) => i.globsResolved)
if (allGlobs.some((glob) => micromatch.isMatch(file, glob)))
modules.push(...(server.moduleGraph.getModulesByFile(id) || []))
after
type glob = string
const allGlobs: glob[] = result.matches.flatMap((i) => i.globsResolved)
const allGlobsMatch = picomatch(allGlobs)
if (allGlobsMatch(file))
modules.push(...(server.moduleGraph.getModulesByFile(id) || []))
allGlobs.some(globs => micromatch.isMatch(file, globs))
= allGlobs.some(globs => globs.some(glob => micromatch.isMatch(file, glob)))
= allGlobs.flat().some(glob => micromatch.isMatch(file, glob))
= micromatch.isMatch(allGlobs.flat(), file)
= picomatch(allGlobs.flat())(file)
https://github.com/micromatch/picomatch/blob/13efdf0c7d0e07ee30bf78d0079184aa4a0ec7d4/lib/picomatch.js#L32-L43
Great PR @sapphi-red. Leaving some references to previous related discussions. |
Thanks for the pointer ❤️ I marked this PR as draft because I found a bug in
It seems we weren't doing that. https://vitejs.dev/guide/features.html#glob-import-caveats We could pass About I'll split this PR into two parts, one for |
Oh, looks like we only did it for an some docs in the types: https://github.com/vitejs/vite/pull/5610/files#diff-9d674f574784ad7dbb518bca67092c190d677c30ccc30fb851b98ecd942b3cc7R10 |
It seems brace expansion works with Because I guess rewriting |
Description
This PR makes Vite cache compiled globs. This improved performance for ~4% on my project.
micromatch.isMatch
always callspicomatch(patterns, options)(str)
which leads compiling glob every time.https://github.com/micromatch/micromatch/blob/002d0d184c95e76775528fa1dbe0c446518879b2/index.js#L123
I've changed this to cache
picomatch(patterns, options)
to avoid compiling it.Additional context
Is it safe to rewrite
micromatch
intopicomatch
?picomatch
saysmicromatch
supports more more advanced matching with braces. But actually it currently does not for some functions (micromatch/micromatch#169 (comment)) and as you can see from the implementation I quoted above,micromatch.isMatch
is just a shorthand.Why
micromatch.scan
is rewrote intopicomatch.scan
?micromatch.scan
is also just an alias ofpicomatch.scan
. But in future the implementation might be fixed. Because I changedmicromatch.isMatch
to usepicomatch
directly, I think we should usepicomatch.scan
directly too to avoid misalignments.https://github.com/micromatch/micromatch/blob/002d0d184c95e76775528fa1dbe0c446518879b2/index.js#L403
Bundle size
By rewriting
micromatch
functions topicomatch
functions, I was able to removemicromatch
dep.Because
fast-glob
depends onmicromatch
, I thought it does not affect bundle size.But actually it slightly increased bundle size.
package size / unpacked size: 758.9kB / 3.3MB => 759.6kB / 3.3 MB
That said, I think it's acceptable as it's insignificant.
What is the purpose of this pull request?
Before submitting the PR, please make sure you do the following
fixes #123
).