Skip to content

Commit 214f9c4

Browse files
authored
perf: use buffer.isUtf8 in NodePathFS (#5516)
**What's the problem this PR addresses?** <!-- Describe the rationale of your PR. --> <!-- Link all issues that it closes. (Closes/Resolves #xxxx.) --> Node 18.14.0 added a new function: [`buffer.isUtf8`](https://nodejs.org/api/buffer.html#bufferisutf8input) **How did you fix it?** <!-- A detailed description of your implementation. --> I've tested it and it's 75 times faster than our current implementation. For an `80 MB` buffer: - old check: `315.447ms` - new check: `4.06ms` **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.
1 parent c202c8b commit 214f9c4

File tree

9 files changed

+119
-72
lines changed

9 files changed

+119
-72
lines changed

.pnp.cjs

+59-59
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.yarn/versions/98a02d9c.yml

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
releases:
2+
"@yarnpkg/cli": patch
3+
"@yarnpkg/core": patch
4+
"@yarnpkg/fslib": patch
5+
"@yarnpkg/plugin-pnp": patch
6+
"@yarnpkg/pnp": patch
7+
8+
declined:
9+
- "@yarnpkg/plugin-compat"
10+
- "@yarnpkg/plugin-constraints"
11+
- "@yarnpkg/plugin-dlx"
12+
- "@yarnpkg/plugin-essentials"
13+
- "@yarnpkg/plugin-exec"
14+
- "@yarnpkg/plugin-file"
15+
- "@yarnpkg/plugin-git"
16+
- "@yarnpkg/plugin-github"
17+
- "@yarnpkg/plugin-http"
18+
- "@yarnpkg/plugin-init"
19+
- "@yarnpkg/plugin-interactive-tools"
20+
- "@yarnpkg/plugin-link"
21+
- "@yarnpkg/plugin-nm"
22+
- "@yarnpkg/plugin-npm"
23+
- "@yarnpkg/plugin-npm-cli"
24+
- "@yarnpkg/plugin-pack"
25+
- "@yarnpkg/plugin-patch"
26+
- "@yarnpkg/plugin-pnpm"
27+
- "@yarnpkg/plugin-stage"
28+
- "@yarnpkg/plugin-typescript"
29+
- "@yarnpkg/plugin-version"
30+
- "@yarnpkg/plugin-workspace-tools"
31+
- vscode-zipfs
32+
- "@yarnpkg/builder"
33+
- "@yarnpkg/doctor"
34+
- "@yarnpkg/extensions"
35+
- "@yarnpkg/libzip"
36+
- "@yarnpkg/nm"
37+
- "@yarnpkg/pnpify"
38+
- "@yarnpkg/sdks"
39+
- "@yarnpkg/shell"

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"@babel/preset-react": "^7.18.6",
1313
"@babel/preset-typescript": "^7.18.6",
1414
"@types/jest": "^28.1.6",
15-
"@types/node": "^18.11.11",
15+
"@types/node": "^18.15.11",
1616
"@yarnpkg/cli": "workspace:^",
1717
"@yarnpkg/core": "workspace:^",
1818
"@yarnpkg/eslint-config": "workspace:^",

packages/yarnpkg-core/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
"@types/diff": "^5.0.0",
4646
"@types/lodash": "^4.14.136",
4747
"@types/micromatch": "^4.0.1",
48-
"@types/node": "^18.11.11",
48+
"@types/node": "^18.15.11",
4949
"@types/tar": "^4.0.4",
5050
"@types/tunnel": "^0.0.0",
5151
"@yarnpkg/cli": "workspace:^",

packages/yarnpkg-core/sources/nodeUtils.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,7 @@ export function getCaller() {
140140

141141
export function availableParallelism() {
142142
// TODO: Use os.availableParallelism directly when dropping support for Node.js < 19.4.0
143-
if (`availableParallelism` in os)
144-
// @ts-expect-error - No types yet
143+
if (typeof os.availableParallelism !== `undefined`)
145144
return os.availableParallelism();
146145

147146
return Math.max(1, os.cpus().length);

packages/yarnpkg-fslib/sources/NodePathFS.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import buffer from 'buffer';
12
import {URL, fileURLToPath} from 'url';
23
import {inspect} from 'util';
34

@@ -36,7 +37,7 @@ export class NodePathFS extends ProxiedFS<NativePath, NativePath> {
3637

3738
if (Buffer.isBuffer(path)) {
3839
const str = path.toString();
39-
if (Buffer.byteLength(str) !== path.byteLength)
40+
if (!isUtf8(path, str))
4041
throw new Error(`Non-utf8 buffers are not supported at the moment. Please upvote the following issue if you encounter this error: https://github.com/yarnpkg/berry/issues/4942`);
4142

4243
return str;
@@ -45,3 +46,11 @@ export class NodePathFS extends ProxiedFS<NativePath, NativePath> {
4546
throw new Error(`Unsupported path type: ${inspect(path)}`);
4647
}
4748
}
49+
50+
// TODO: remove the fallback when dropping support for Node.js < 18.14.0
51+
function isUtf8(buf: Buffer, str: string) {
52+
if (typeof buffer.isUtf8 !== `undefined`)
53+
return buffer.isUtf8(buf);
54+
55+
return Buffer.byteLength(str) === buf.byteLength;
56+
}

packages/yarnpkg-pnp/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"./package.json": "./package.json"
1111
},
1212
"dependencies": {
13-
"@types/node": "^18.11.11",
13+
"@types/node": "^18.15.11",
1414
"@yarnpkg/fslib": "workspace:^"
1515
},
1616
"devDependencies": {

yarn.lock

+7-7
Original file line numberDiff line numberDiff line change
@@ -5896,10 +5896,10 @@ __metadata:
58965896
languageName: node
58975897
linkType: hard
58985898

5899-
"@types/node@npm:*, @types/node@npm:^18.11.11":
5900-
version: 18.11.11
5901-
resolution: "@types/node@npm:18.11.11"
5902-
checksum: 514ba7f9e2ab971afd3c5405dbee305e3abba4d62d4500d65e72ec341456635766d43ed7dbea4bb62e88b70922a1c9abc697c2d67014bf377100d97cdf23b384
5899+
"@types/node@npm:*, @types/node@npm:^18.15.11":
5900+
version: 18.16.18
5901+
resolution: "@types/node@npm:18.16.18"
5902+
checksum: 4692b4c927bf63efbc3aed6e18c1a264eb8b5673fa7cc0649a38f394ab34d4d4d0e7a2b98f4235bc8dc7615e88080a1b443a06eac9324f67e426398ed857b4a1
59035903
languageName: node
59045904
linkType: hard
59055905

@@ -7061,7 +7061,7 @@ __metadata:
70617061
"@types/diff": "npm:^5.0.0"
70627062
"@types/lodash": "npm:^4.14.136"
70637063
"@types/micromatch": "npm:^4.0.1"
7064-
"@types/node": "npm:^18.11.11"
7064+
"@types/node": "npm:^18.15.11"
70657065
"@types/semver": "npm:^7.1.0"
70667066
"@types/tar": "npm:^4.0.4"
70677067
"@types/treeify": "npm:^1.0.0"
@@ -7343,7 +7343,7 @@ __metadata:
73437343
"@babel/preset-react": "npm:^7.18.6"
73447344
"@babel/preset-typescript": "npm:^7.18.6"
73457345
"@types/jest": "npm:^28.1.6"
7346-
"@types/node": "npm:^18.11.11"
7346+
"@types/node": "npm:^18.15.11"
73477347
"@yarnpkg/cli": "workspace:^"
73487348
"@yarnpkg/core": "workspace:^"
73497349
"@yarnpkg/eslint-config": "workspace:^"
@@ -7843,7 +7843,7 @@ __metadata:
78437843
dependencies:
78447844
"@rollup/plugin-commonjs": "npm:^21.0.1"
78457845
"@rollup/plugin-node-resolve": "npm:^11.0.1"
7846-
"@types/node": "npm:^18.11.11"
7846+
"@types/node": "npm:^18.15.11"
78477847
"@yarnpkg/fslib": "workspace:^"
78487848
"@yarnpkg/libzip": "workspace:^"
78497849
arg: "npm:^5.0.2"

0 commit comments

Comments
 (0)