Skip to content

Commit

Permalink
perf: use fast-glob instead of globby (#5606)
Browse files Browse the repository at this point in the history
**What's the problem this PR addresses?**
<!-- Describe the rationale of your PR. -->
<!-- Link all issues that it closes. (Closes/Resolves #xxxx.) -->

`globby` is built on top of `fast-glob` (which we already use in
`@yarnpkg/shell`), but the only useful feature it adds for us is
`expandDirectories` which [has been broken for 3
years](sindresorhus/globby#147) so we can't
use it anyways.

In addition, `globby` is maintained by a person that handles reasonable
criticism by blocking our lead maintainer on both Twitter and GitHub,
which makes it harder than necessary to communicate ¯\\_(ツ)_/¯.

**How did you fix it?**
<!-- A detailed description of your implementation. -->

Replaced `globby` with `fast-glob`.

Bundle size:
- Before: `2730152 bytes`
- After: `2720218 bytes`
- Improvement: `0.36%`

Benchmarks:
- Before:
```
Benchmark 1: YARN_IGNORE_PATH=1 node packages/yarnpkg-cli/bundles/yarn.js run
  Time (mean ± σ):     293.0 ms ±   4.2 ms    [User: 387.2 ms, System: 91.8 ms]
  Range (min … max):   285.8 ms … 299.5 ms    10 runs
```
- After:
```
Benchmark 1: YARN_IGNORE_PATH=1 node packages/yarnpkg-cli/bundles/yarn.js run
  Time (mean ± σ):     291.9 ms ±   2.4 ms    [User: 387.6 ms, System: 87.7 ms]
  Range (min … max):   286.6 ms … 294.3 ms    10 runs  
```

**Checklist**
<!--- Don't worry if you miss something, chores are automatically
tested. -->
<!--- This checklist exists to help you remember doing the chores when
you submit a PR. -->
<!--- Put an `x` in all the boxes that apply. -->
- [X] I have read the [Contributing
Guide](https://yarnpkg.com/advanced/contributing).

<!-- See
https://yarnpkg.com/advanced/contributing#preparing-your-pr-to-be-released
for more details. -->
<!-- Check with `yarn version check` and fix with `yarn version check
-i` -->
- [X] I have set the packages that need to be released for my changes to
be effective.

<!-- The "Testing chores" workflow validates that your PR follows our
guidelines. -->
<!-- If it doesn't pass, click on it to see details as to what your PR
might be missing. -->
- [X] I will check that all automated PR checks pass before the PR gets
reviewed.

---------

Co-authored-by: Kristoffer K <[email protected]>
  • Loading branch information
paul-soporan and merceyz authored Jul 26, 2023
1 parent 197d5b1 commit b3acb59
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .pnp.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions .yarn/versions/aad3d359.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
releases:
"@yarnpkg/cli": patch
"@yarnpkg/core": patch

declined:
- "@yarnpkg/plugin-compat"
- "@yarnpkg/plugin-constraints"
- "@yarnpkg/plugin-dlx"
- "@yarnpkg/plugin-essentials"
- "@yarnpkg/plugin-exec"
- "@yarnpkg/plugin-file"
- "@yarnpkg/plugin-git"
- "@yarnpkg/plugin-github"
- "@yarnpkg/plugin-http"
- "@yarnpkg/plugin-init"
- "@yarnpkg/plugin-interactive-tools"
- "@yarnpkg/plugin-link"
- "@yarnpkg/plugin-nm"
- "@yarnpkg/plugin-npm"
- "@yarnpkg/plugin-npm-cli"
- "@yarnpkg/plugin-pack"
- "@yarnpkg/plugin-patch"
- "@yarnpkg/plugin-pnp"
- "@yarnpkg/plugin-pnpm"
- "@yarnpkg/plugin-stage"
- "@yarnpkg/plugin-typescript"
- "@yarnpkg/plugin-version"
- "@yarnpkg/plugin-workspace-tools"
- "@yarnpkg/builder"
- "@yarnpkg/doctor"
- "@yarnpkg/extensions"
- "@yarnpkg/nm"
- "@yarnpkg/pnpify"
- "@yarnpkg/sdks"
2 changes: 1 addition & 1 deletion packages/yarnpkg-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"cross-spawn": "7.0.3",
"diff": "^5.1.0",
"dotenv": "^16.3.1",
"globby": "^11.0.1",
"fast-glob": "^3.2.2",
"got": "^11.7.0",
"lodash": "^4.17.15",
"micromatch": "^4.0.2",
Expand Down
8 changes: 3 additions & 5 deletions packages/yarnpkg-core/sources/Workspace.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {PortablePath, npath, ppath, xfs, Filename} from '@yarnpkg/fslib';
import globby from 'globby';
import fastGlob from 'fast-glob';

import {HardDependencies, Manifest} from './Manifest';
import {Project} from './Project';
Expand Down Expand Up @@ -60,15 +60,13 @@ export class Workspace {
if (patterns.length === 0)
return;

const relativeCwds = await globby(patterns, {
const relativeCwds = await fastGlob(patterns, {
cwd: npath.fromPortablePath(this.cwd),
expandDirectories: false,
onlyDirectories: true,
onlyFiles: false,
ignore: [`**/node_modules`, `**/.git`, `**/.yarn`],
});

// It seems that the return value of globby isn't in any guaranteed order - not even the directory listing order
// fast-glob returns results in arbitrary order
relativeCwds.sort();

await relativeCwds.reduce(async (previousTask, relativeCwd) => {
Expand Down
15 changes: 6 additions & 9 deletions packages/yarnpkg-core/sources/hashUtils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {PortablePath, xfs, npath, FakeFS} from '@yarnpkg/fslib';
import {createHash, BinaryLike} from 'crypto';
import globby from 'globby';
import fastGlob from 'fast-glob';

export function makeHash<T extends string = string>(...args: Array<BinaryLike | null>): T {
const hash = createHash(`sha512`);
Expand Down Expand Up @@ -45,29 +45,26 @@ export async function checksumFile(path: PortablePath, {baseFs, algorithm}: {bas
}

export async function checksumPattern(pattern: string, {cwd}: {cwd: PortablePath}) {
// Note: We use a two-pass glob instead of using the expandDirectories option
// from globby, because the native implementation is broken.
// Note: We use a two-pass glob instead of using globby with the expandDirectories
// option, because the native implementation is broken.
//
// Ref: https://github.com/sindresorhus/globby/issues/147

const dirListing = await globby(pattern, {
const dirListing = await fastGlob(pattern, {
cwd: npath.fromPortablePath(cwd),
expandDirectories: false,
onlyDirectories: true,
unique: true,
});

const dirPatterns = dirListing.map(entry => {
return `${entry}/**/*`;
});

const listing = await globby([pattern, ...dirPatterns], {
const listing = await fastGlob([pattern, ...dirPatterns], {
cwd: npath.fromPortablePath(cwd),
expandDirectories: false,
onlyFiles: false,
unique: true,
});

// fast-glob returns results in arbitrary order
listing.sort();

const hashes = await Promise.all(listing.map(async entry => {
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7083,7 +7083,7 @@ __metadata:
diff: "npm:^5.1.0"
dotenv: "npm:^16.3.1"
esbuild: "npm:esbuild-wasm@^0.15.15"
globby: "npm:^11.0.1"
fast-glob: "npm:^3.2.2"
got: "npm:^11.7.0"
lodash: "npm:^4.17.15"
micromatch: "npm:^4.0.2"
Expand Down

0 comments on commit b3acb59

Please sign in to comment.