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

perf(plugin-pnp): only reload pnpapi when it changes #2942

Merged
merged 6 commits into from
Jul 18, 2021

Conversation

merceyz
Copy link
Member

@merceyz merceyz commented May 27, 2021

What's the problem this PR addresses?

The cache introduced in #2564 only survives as long as the linker instance does, this means it doesn't affect running multiple scripts (i.e. postinstalls) since new linkers are created every time.

How did you fix it?

A dynamicRequireFresh (naming suggestions welcome) function that caches the module globally and only reloads it if the file changed

Result

Tested on the gatsby benchmark

bench install-cache-and-lock \
--prepare 'rm -rf .yarn .pnp.*' \
'yarn install'

$ YARN_IGNORE_PATH=1 hyperfine -w 5 --prepare="rm -rf .yarn" 'node before.cjs' 'node after.cjs'
Benchmark #1: node before.cjs
  Time (mean ± σ):      8.931 s ±  0.194 s    [User: 11.532 s, System: 4.739 s]
  Range (min … max):    8.760 s …  9.325 s    10 runs

Benchmark #2: node after.cjs
  Time (mean ± σ):      8.290 s ±  0.072 s    [User: 10.918 s, System: 4.517 s]
  Range (min … max):    8.216 s …  8.457 s    10 runs

Summary
  'node after.cjs' ran
    1.08 ± 0.03 times faster than 'node before.cjs'

Checklist

  • I have read the Contributing Guide.
  • I have set the packages that need to be released for my changes to be effective.
  • I will check that all automated PR checks pass before the PR gets reviewed.

@merceyz merceyz requested a review from arcanis as a code owner May 27, 2021 17:26
/**
* Requires a module without using the cache if it has changed since the last time it was loaded
*/
export function dynamicRequireFresh(path: PortablePath) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I read "fresh" as implying the require is always up-to-date. Perhaps instead the public API could be:

type RequireCache = 'mtime' | null;

export function dynamicRequire(path: PortablePath, opts: {cache: RequireCache}) {

Also, perhaps dynamicRequireNoCache should burst the entry in dynamicRequireFreshCache?

@arcanis arcanis merged commit ad00c70 into master Jul 18, 2021
@arcanis arcanis deleted the merceyz/perf/pnp-builds branch July 18, 2021 15:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants