Skip to content
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

[POC] Improve pnp loader speed and memory #6671

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions .pnp.cjs

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

Binary file not shown.
36 changes: 36 additions & 0 deletions bench/run-resolve.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

Check failure on line 1 in bench/run-resolve.ts

View workflow job for this annotation

GitHub Actions / Testing chores

Too many blank lines at the beginning of file. Max of 0 allowed
import * as fs from 'fs'

Check warning on line 2 in bench/run-resolve.ts

View workflow job for this annotation

GitHub Actions / Testing chores

'fs' is defined but never used

Check failure on line 2 in bench/run-resolve.ts

View workflow job for this annotation

GitHub Actions / Testing chores

Unaligned import statement

Check failure on line 2 in bench/run-resolve.ts

View workflow job for this annotation

GitHub Actions / Testing chores

Missing semicolon
import { performance } from 'perf_hooks';

Check failure on line 3 in bench/run-resolve.ts

View workflow job for this annotation

GitHub Actions / Testing chores

There should be no space after '{'

Check failure on line 3 in bench/run-resolve.ts

View workflow job for this annotation

GitHub Actions / Testing chores

There should be no space before '}'

Check failure on line 3 in bench/run-resolve.ts

View workflow job for this annotation

GitHub Actions / Testing chores

Unaligned import statement
import { ZipFS } from '@yarnpkg/libzip';

Check failure on line 4 in bench/run-resolve.ts

View workflow job for this annotation

GitHub Actions / Testing chores

Expected '@yarnpkg/libzip' to be imported before 'fs' (lexicographic order)

Check failure on line 4 in bench/run-resolve.ts

View workflow job for this annotation

GitHub Actions / Testing chores

There should be no space after '{'

Check warning on line 4 in bench/run-resolve.ts

View workflow job for this annotation

GitHub Actions / Testing chores

'ZipFS' is defined but never used

Check failure on line 4 in bench/run-resolve.ts

View workflow job for this annotation

GitHub Actions / Testing chores

There should be no space before '}'

Check failure on line 4 in bench/run-resolve.ts

View workflow job for this annotation

GitHub Actions / Testing chores

Unaligned import statement
import { MiniZipFS } from '@yarnpkg/minizip';

Check warning on line 5 in bench/run-resolve.ts

View workflow job for this annotation

GitHub Actions / Testing chores

'MiniZipFS' is defined but never used
import { PortablePath } from '@yarnpkg/fslib';

Check warning on line 6 in bench/run-resolve.ts

View workflow job for this annotation

GitHub Actions / Testing chores

'PortablePath' is defined but never used
import {createRequire} from 'module'
import path from 'path'
import {ResolverFactory} from 'enhanced-resolve'

Check warning on line 9 in bench/run-resolve.ts

View workflow job for this annotation

GitHub Actions / Testing chores

'ResolverFactory' is defined but never used



const formatMemoryUsage = (data: number) => `${Math.round(data / 1024 / 1024 * 100) / 100} MB`;

Check warning on line 13 in bench/run-resolve.ts

View workflow job for this annotation

GitHub Actions / Testing chores

'formatMemoryUsage' is assigned a value but never used

const from = require.resolve('micromatch');
// const baseFs= new ZipFS(from as PortablePath)
// const resolver = ResolverFactory.createResolver({fileSystem: {readFile: baseFs.readFileSync.bind(baseFs)}})
console.log(from)
const req = createRequire(path.dirname(from))

globalThis.Error = class {

Check failure on line 21 in bench/run-resolve.ts

View workflow job for this annotation

GitHub Actions / Testing chores

Property 'stackTraceLimit' is missing in type 'typeof Error' but required in type 'ErrorConstructor'.
constructor() {}
static captureStackTrace = () => {}
}


const was = performance.now()
for (let i = 0; i < 50_000; i++) {
// resolver.resolveSync({}, from, `./fi${i}`)
try {
req.resolve(`./file${i}`)
} catch {

}
}
console.log(performance.now() - was)
80 changes: 80 additions & 0 deletions bench/run.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@

import * as fs from 'fs'
import { performance } from 'perf_hooks';
import { ZipFS } from '@yarnpkg/libzip';
import { MiniZipFS } from '@yarnpkg/minizip';
import { PortablePath } from '@yarnpkg/fslib';





const cwd = '/Users/vadymh/.yarn/berry/cache';
const files = fs.readdirSync(cwd).filter(f => f.endsWith('.zip'))

let totalSize = 0
for (const f of files) {
const file = `${cwd}/${f}`;
const stats = fs.statSync(file)
totalSize += stats.size
}


const formatMemoryUsage = (data: number) => `${Math.round(data / 1024 / 1024 * 100) / 100} MB`;

console.log('files count: ' + files.length, 'size', formatMemoryUsage(totalSize))

const mem = process.argv.includes('--mem')


for (const zipper of [MiniZipFS, ZipFS]) {
for (let i = 0; i < 3; i++) {
let allFiles = 0
const was = performance.now()
const memArr = []
for (const f of files) {
const file = `${cwd}/${f}`;

let fi = new zipper(file as PortablePath)
allFiles += fi.getAllFiles().length
if (mem){
const memoryData = process.memoryUsage();
memArr.push(memoryData.rss)
}

fi.discardAndClose()
}

// const memoryUsage = {
// rss: `${formatMemoryUsage(memoryData.rss)} -> Resident Set Size - total memory allocated for the process execution`,
// heapTotal: `${formatMemoryUsage(memoryData.heapTotal)} -> total size of the allocated heap`,
// heapUsed: `${formatMemoryUsage(memoryData.heapUsed)} -> actual memory used during the execution`,
// external: `${formatMemoryUsage(memoryData.external)} -> V8 external memory`,
// };

console.log(`${zipper.name}: try ${i + 1}`)

if (mem) {
memArr.sort((a, b) => a-b)
console.log({
p50: formatMemoryUsage(memArr[Math.floor(memArr.length / 2)]),
p90: formatMemoryUsage(memArr[Math.floor(memArr.length * 0.9)]),
p95: formatMemoryUsage(memArr[Math.floor(memArr.length * 0.95)]),
p99: formatMemoryUsage(memArr[Math.floor(memArr.length * 0.99)]),
})
} else {
const took = performance.now() - was
console.log({
took,
allFiles
})
}

// console.log(memoryUsage)
}
}





2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
"@yarnpkg/eslint-config": "workspace:^",
"@yarnpkg/fslib": "workspace:^",
"@yarnpkg/libzip": "workspace:^",
"@yarnpkg/minizip": "workspace:^",
"@yarnpkg/sdks": "workspace:^",
"clipanion": "^4.0.0-rc.2",
"enhanced-resolve": "^5.18.0",
"esbuild": "npm:esbuild-wasm@^0.23.0",
"eslint": "^8.57.0",
"jest": "^29.2.1",
Expand Down
1 change: 1 addition & 0 deletions packages/plugin-pnp/sources/PnpLinker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ export class PnpInstaller implements Installer {
fallbackExclusionList,
fallbackPool,
ignorePattern,
zipImplementation: this.opts.project.configuration.get(`minizip`) ? `minizip` : `libzip`,
packageRegistry,
shebang,
});
Expand Down
6 changes: 6 additions & 0 deletions packages/plugin-pnp/sources/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ declare module '@yarnpkg/core' {
nodeLinker: string;
winLinkType: string;
pnpMode: string;
minizip: boolean
pnpShebang: string;
pnpIgnorePatterns: Array<string>;
pnpEnableEsmLoader: boolean;
Expand All @@ -90,6 +91,11 @@ const plugin: Plugin<CoreHooks & StageHooks> = {
type: SettingsType.STRING,
default: `pnp`,
},
minizip: {
description: `Whether Yarn should use minizip to extract archives`,
type: SettingsType.BOOLEAN,
default: false
},
winLinkType: {
description: `Whether Yarn should use Windows Junctions or symlinks when creating links on Windows.`,
type: SettingsType.STRING,
Expand Down
5 changes: 5 additions & 0 deletions packages/yarnpkg-minizip/.babelrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
ignore: [
`./sources/libzip.js`,
],
};
3 changes: 3 additions & 0 deletions packages/yarnpkg-minizip/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# `@yarnpkg/minizip`

Lightweight, fast, readonly zip fs for pnp runtime.
50 changes: 50 additions & 0 deletions packages/yarnpkg-minizip/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"name": "@yarnpkg/minizip",
"version": "3.1.0",
"license": "BSD-2-Clause",
"main": "./sources/index.ts",
"exports": {
".": {
"default": "./sources/index.ts"
},
"./package.json": "./package.json"
},
"scripts": {
"postpack": "rm -rf lib",
"prepack": "run build:compile \"$(pwd)\"",
"release": "yarn npm publish"
},
"publishConfig": {
"main": "./lib/index.js",
"browser": "./lib/index.js",
"exports": {
".": {
"default": "./lib/index.js"
},
"./package.json": "./package.json"
}
},
"files": [
"/lib/**/*"
],
"repository": {
"type": "git",
"url": "ssh://git@github.com/yarnpkg/berry.git",
"directory": "packages/yarnpkg-minizip"
},
"devDependencies": {
"@types/prettier": "1.19.0",
"prettier": "^1.19.1"
},
"dependencies": {
"@yarnpkg/fslib": "workspace:^",
"tslib": "^2.4.0"
},
"peerDependencies": {
"@yarnpkg/fslib": "workspace:^"
},
"engines": {
"node": ">=18.12.0"
},
"stableVersion": "3.1.0"
}
Loading
Loading