You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Resolve all files in a service folder (if package.excludeDevDepencies: true avoid traversing into dev dependencies) - To ensure better performance construct logic with callbacks not promises.
Show warning if symlinks are approached (we may consider adding support for them in next PR)
Filter obtained files list against eventual package patterns, but intelligently, do not test to be included filenames against include patterns, and do not test excluded filenames against exclude patterns.
Great thanks @ryan-roemer I definitely planned to look into how you solved that on your side. It's definitely outstanding work. It's likely we will just depend on this library, it all depends on obtained numbers and how that fits us organization wise
btw. instead of taking only prod dependencies, have you considered other approach, where it's all but dev deps that are taken? Logically it's more on pair with our excludeDevDepencies option (which we anyway plan to deprecate)
For a different discussion, long term, I'm not sure if serverless-jetpack is going to continue supporting this current approach (e.g. the drop-in serverless packaging replacement)
Assuming we will internally fix this problem with this issue, there may no longer be a point in serverless-jetpack for addressing that.
and will probably commit more to tracing actually used dependencies like webpack/etc.
That's the next step, we would also envision to have in a core, but it's nowhere near, so having serverless-jetpack handling that is definitely very helpful. Anyway we'll definitely check all those tracing libraries you've listed
instead of taking only prod dependencies, have you considered other approach, where it's all but dev deps that are taken?
My apologies for not being a bit more specific 😄 inspectdep considers "production" dependencies to be dependencies and optionalDependencies. But not devDependencies or peerDependencies (reasoning for the latter of which should be a prod dependency elsewhere in the project).
Assuming we will internally fix this problem with this issue, there may no longer be a point in serverless-jetpack for addressing that.
It uses globby to match all the files given the patterns (patterns is simply exclude + include) and then uses micromatch on the globby results to remove the excludes.
Apart from adding an extra unnecessary dependency, this approach involves doing the pattern matching twice: one by globby and a second one done manually pattern by pattern using micromatch.
My suggestion is to do everything in a single pass with globby as I did in #8581 since the whole micromatch filtering is filtering over already filtered results.
In code, I suggest to change:
returnglobby(['**'].concat(params.include),{cwd: path.join(this.serverless.config.servicePath,prefix||''),dot: true,silent: true,follow: true,nodir: true,expandDirectories: false,}).then(allFilePaths=>{constfilePathStates=allFilePaths.reduce((p,c)=>Object.assign(p,{[c]: true}),{});patterns// micromatch only does / style path delimiters, so convert them if on windows .map(p=>{returnprocess.platform==='win32' ? p.replace(/\\/g,'/') : p;}).forEach(p=>{constexclude=p.startsWith('!');constpattern=exclude ? p.slice(1) : p;micromatch(allFilePaths,[pattern],{dot: true}).forEach(key=>{filePathStates[key]=!exclude;});});constfilePaths=Object.entries(filePathStates).filter(r=>r[1]===true).map(r=>r[0]);if(filePaths.length!==0)returnfilePaths;thrownewthis.serverless.classes.Error('No file matches include / exclude patterns');});
I love the idea for getting rid of include/exclude and doing just patterns straight for globby(). One suggestion that I've made elsewhere before is to get rid of that initial mandatory '**' and start with either nothing or maybe node_modules/**. My original comment is here with this being the first suggestion. The second suggestion is doing what you're doing and getting rid of the extra pattern matching outside the globby() phase so yay 🎉 !
Totally agree with removing '**' and only use it if there are no include patterns provided.
That was my next suggestion once we agree on the globby issue.
@juanjoDiaz thanks for looking into that. Problem with globby is that for every pattern it does another file search (it's at least how it looks from it's internals). It's highly ineffective, and due to that I would lean towards approach I've outlined in description which in short is:
(1) with most efficient (not promise based) approach resolve all service files (eventually excluding dev dependencies)
(2) Apply glob patterns via micromatch on them.
It might be of course a bit more intelligent, e.g. while traversing the files we may take into account patterns config and do not dive into directories which we know are to be fully excluded.
One suggestion that I've made elsewhere before is to get rid of that initial mandatory '**'
I think that approach could be applied only to cases where user explicitly configured !**, as otherwise Framework implies to package everything (it means that any inclusion rules are ineffective, it's just eventual exclusions that play part).
Anyway that makes a great point, that we probably should have two very different packaging paths. One when no !** is provided, and one where there is (e.g. in that case, we probably should not dive into traversing service folder).
Regarding ./**, the easiest but breaking change (so next major version) would be to use ./** only if no patterns are provided. If patterns are provided, then the user needs to add ./** if he really wants it all.
But let's keep the ./** discussion for once we solve the globbing problem.
globby(['**'].concat(params.include)) is essentially the same as globby(['**']) unless there are negated patterns or parent directories. Even for negated directories, since we are doing the micromatch filtering on all the patterns, the negated patterns are applied there. So we are applying them twice and, if micromatch is really faster than globby we would be better off just doing globby(['**']) adding there any possible ../ directory and then using micromatch.
So my question is: why is globby any slower than globbing each pattern against each and every file using micromatch?
As a related aside, my intuition is that globby() vs a more specific library for pure pattern matching of already known file names isn't that big of a difference. The huge difference comes from file I/O. The fastest solution is likely whatever minimizes disk reads...
Yeah. globby read disk whereas micromatch does not.
I would expect that something like globby does a recursive traverse of the cwd using some implementation similar to micromatch internally to see if it should include or not the current file/dir.
The brute-force approach would be to get the whole list of files recursively and then apply the matching.
The smart way would be to consolidate duplicate patterns, stop on folders that you know won't require a deeper inspection, etc. (however that's not trivial).
The current "optimization" points out that brute-forcing is faster than globby which is really surprising for me.
The author in #5574 claims that using micromatch is faster because glob uses minimatch. However, we don't use glob. We use globby which uses fast-glob under the hood which uses micromatch and picomatch.
Both fast-glob and tiny-glob provide benchmarks but none of them really help much choosing.
Having a test would really help here.
I tried globbing some random patterns in the codebase of this project (30558 files include node_modules) and it always took a few ms...
So my question is: why is globby any slower than globbing each pattern against each and every file using micromatch?
@juanjoDiazglobby runs actual FS search (fast-glob) for each pattern, while with micromatch we're testing pattern against retrieved filename (no FS access involved)
Would using fast-glob directly be faster?
It doesn't support multiple patterns well. At least as I tested ['!foo', 'foo/bar/**'] wouldn't resolve files from foo/bar folder
The huge difference comes from file I/O.
@ryan-roemer that's mostly true, but that depends on what setup you work on, e.g. on recent MBPs if you traverse folder with thousands of files and for each file you retrieve stats, you'll find that this operation would be ca 40% faster when constructed with callbacks interface than with promises (which introduce tons of artificial ticks and promise objects to be garbage collected),
but ofc the slower the FS, the more bottleneck it is to care about
globby runs actual FS search (fast-glob) for each pattern, while with micromatch we're testing pattern against retrieved filename (no FS access involved)
That's right. However, to run micromatch you need the file names first.
The current optimization and the discussion here imply that it's faster to just get all the possible file names for all includes and then simply run micromatch over all of them which still sounds strange to me...
I recently experienced a performance issue related to this when packaging my project. In serverless.yml, we specify:
package:
exclude:
'**'
This disables the default behaviour of including all files in the current directory. However, globby is only passed params.include, not params.exclude. So, globby will return all files in the project directory only for micromatch to later filter out most of them. This means that using exclude: '**' instead of include: '!**' has a big performance impact.
To fix this, globby needs to consider excluded patterns too. The fix for this should be simple: instead of
globby(['**'].concat(params.include),/* ... */)
use
globby(['**'].concat(patterns),/* ... */)
since patterns contains params.include and the negation of params.exclude.
Yeah @BenLanglois -- I've got a long comment and suggestion for future packaging tweaks here that also discusses the performance problems you've also identified: #6007 (comment)
Activity
ryan-roemer commentedon Dec 7, 2020
In case it helps @medikoo the means by which
serverless-jetpackdoes this faster production dependency inference fromnode_modulesis entirely encapsulated in a small library: https://github.com/FormidableLabs/inspectdep that just finds all prod dependencies from a starting point with a few other options.For a different discussion, long term, I'm not sure if
serverless-jetpackis going to continue supporting this current approach (e.g. the drop-in serverless packaging replacement) and will probably commit more to tracing actually used dependencies like webpack/etc. does just with real files not a mutated single bundle. In case you're interested, we have a similar separate library for just tracing (I know you have created a similar one too -- ours uses acorn and handles a lot of common non-static-import cases): https://github.com/FormidableLabs/trace-deps. We actually just produced a tool to package withtrace-depsbut without using serverless at all (for our projects doing Lambda with straight Terraform): https://github.com/FormidableLabs/trace-pkgmedikoo commentedon Dec 8, 2020
Great thanks @ryan-roemer I definitely planned to look into how you solved that on your side. It's definitely outstanding work. It's likely we will just depend on this library, it all depends on obtained numbers and how that fits us organization wise
btw. instead of taking only prod dependencies, have you considered other approach, where it's all but dev deps that are taken? Logically it's more on pair with our
excludeDevDepenciesoption (which we anyway plan to deprecate)Assuming we will internally fix this problem with this issue, there may no longer be a point in
serverless-jetpackfor addressing that.That's the next step, we would also envision to have in a core, but it's nowhere near, so having
serverless-jetpackhandling that is definitely very helpful. Anyway we'll definitely check all those tracing libraries you've listedryan-roemer commentedon Dec 8, 2020
My apologies for not being a bit more specific 😄
inspectdepconsiders "production" dependencies to bedependenciesandoptionalDependencies. But notdevDependenciesorpeerDependencies(reasoning for the latter of which should be a prod dependency elsewhere in the project).Yep! That sounds right.
juanjoDiaz commentedon Dec 9, 2020
Bringing the discussion here as suggested by @medikoo
The
resolveFilePathsFromPatternsfunction (https://github.com/serverless/serverless/blob/master/lib/plugins/package/lib/packageService.js#L221-268) is the function that receives the include/exclude props and returns the files to include.It uses
globbyto match all the files given thepatterns(patternsis simplyexclude + include) and then usesmicromatchon theglobbyresults to remove the excludes.Apart from adding an extra unnecessary dependency, this approach involves doing the pattern matching twice: one by
globbyand a second one done manually pattern by pattern usingmicromatch.My suggestion is to do everything in a single pass with
globbyas I did in #8581 since the wholemicromatchfiltering is filtering over already filtered results.In code, I suggest to change:
by
Let me know if you have any comments or if I should make the PR.
ryan-roemer commentedon Dec 9, 2020
I love the idea for getting rid of include/exclude and doing just
patternsstraight forglobby(). One suggestion that I've made elsewhere before is to get rid of that initial mandatory'**'and start with either nothing or maybenode_modules/**. My original comment is here with this being the first suggestion. The second suggestion is doing what you're doing and getting rid of the extra pattern matching outside theglobby()phase so yay 🎉 !juanjoDiaz commentedon Dec 9, 2020
Totally agree with removing '**' and only use it if there are no include patterns provided.
That was my next suggestion once we agree on the
globbyissue.medikoo commentedon Dec 10, 2020
@juanjoDiaz thanks for looking into that. Problem with
globbyis that for every pattern it does another file search (it's at least how it looks from it's internals). It's highly ineffective, and due to that I would lean towards approach I've outlined in description which in short is:(1) with most efficient (not promise based) approach resolve all service files (eventually excluding dev dependencies)
(2) Apply glob patterns via
micromatchon them.It might be of course a bit more intelligent, e.g. while traversing the files we may take into account patterns config and do not dive into directories which we know are to be fully excluded.
medikoo commentedon Dec 10, 2020
I think that approach could be applied only to cases where user explicitly configured
!**, as otherwise Framework implies to package everything (it means that any inclusion rules are ineffective, it's just eventual exclusions that play part).Anyway that makes a great point, that we probably should have two very different packaging paths. One when no
!**is provided, and one where there is (e.g. in that case, we probably should not dive into traversing service folder).juanjoDiaz commentedon Dec 10, 2020
I see.
Regarding
./**, the easiest but breaking change (so next major version) would be to use./**only if nopatternsare provided. If patterns are provided, then the user needs to add./**if he really wants it all.But let's keep the
./**discussion for once we solve the globbing problem.globby(['**'].concat(params.include))is essentially the same asglobby(['**'])unless there are negated patterns or parent directories. Even for negated directories, since we are doing themicromatchfiltering on all the patterns, the negated patterns are applied there. So we are applying them twice and, ifmicromatchis really faster thanglobbywe would be better off just doingglobby(['**'])adding there any possible../directory and then using micromatch.So my question is: why is
globbyany slower than globbing each pattern against each and every file usingmicromatch?globbyis justfast-globwith 2 extra options:gitignoreandexpandDirectoriesand we not using either of those.Would using fast-glob directly be faster?
Should benchmark a faster alternative like https://github.com/terkelg/tiny-glob?
Should we just write a function to list all files under a folder recursively and use
micromatchagainst that if it's really faster?Btw, is there any benchmark or example to test performance here? Or how are we measuring the potential improvements?
ryan-roemer commentedon Dec 10, 2020
As a related aside, my intuition is that
globby()vs a more specific library for pure pattern matching of already known file names isn't that big of a difference. The huge difference comes from file I/O. The fastest solution is likely whatever minimizes disk reads...juanjoDiaz commentedon Dec 10, 2020
Yeah.
globbyread disk whereasmicromatchdoes not.I would expect that something like
globbydoes a recursive traverse of the cwd using some implementation similar tomicromatchinternally to see if it should include or not the current file/dir.The brute-force approach would be to get the whole list of files recursively and then apply the matching.
The smart way would be to consolidate duplicate patterns, stop on folders that you know won't require a deeper inspection, etc. (however that's not trivial).
The current "optimization" points out that brute-forcing is faster than
globbywhich is really surprising for me.The author in #5574 claims that using
micromatchis faster becauseglobusesminimatch. However, we don't useglob. We useglobbywhich usesfast-globunder the hood which usesmicromatchandpicomatch.Both
fast-globandtiny-globprovide benchmarks but none of them really help much choosing.Having a test would really help here.
I tried globbing some random patterns in the codebase of this project (30558 files include node_modules) and it always took a few ms...
medikoo commentedon Dec 11, 2020
@juanjoDiaz
globbyruns actual FS search (fast-glob) for each pattern, while with micromatch we're testing pattern against retrieved filename (no FS access involved)It doesn't support multiple patterns well. At least as I tested
['!foo', 'foo/bar/**']wouldn't resolve files fromfoo/barfolder@ryan-roemer that's mostly true, but that depends on what setup you work on, e.g. on recent MBPs if you traverse folder with thousands of files and for each file you retrieve stats, you'll find that this operation would be ca 40% faster when constructed with callbacks interface than with promises (which introduce tons of artificial ticks and promise objects to be garbage collected),
but ofc the slower the FS, the more bottleneck it is to care about
juanjoDiaz commentedon Dec 11, 2020
That's right. However, to run
micromatchyou need the file names first.The current optimization and the discussion here imply that it's faster to just get all the possible file names for all includes and then simply run
micromatchover all of them which still sounds strange to me...BenLanglois commentedon Feb 24, 2021
I recently experienced a performance issue related to this when packaging my project. In serverless.yml, we specify:
This disables the default behaviour of including all files in the current directory. However, globby is only passed
params.include, notparams.exclude. So, globby will return all files in the project directory only for micromatch to later filter out most of them. This means that usingexclude: '**'instead ofinclude: '!**'has a big performance impact.To fix this, globby needs to consider excluded patterns too. The fix for this should be simple: instead of
use
since patterns contains
params.includeand the negation ofparams.exclude.ryan-roemer commentedon Feb 24, 2021
Yeah @BenLanglois -- I've got a long comment and suggestion for future packaging tweaks here that also discusses the performance problems you've also identified: #6007 (comment)