-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Use RefCountedCache for ZipFs instances in Cache to avoid duplicate allocations #6723
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
Use RefCountedCache for ZipFs instances in Cache to avoid duplicate allocations #6723
Conversation
ad4801b
to
0e2e5e0
Compare
ba08db2
to
b035c40
Compare
If you think it is worth it I'd be more than happy to put this behind a feature flag: i.e. |
b035c40
to
36f966a
Compare
36f966a
to
4b4289a
Compare
Thanks for digging @dannyvv, the fix makes a lot of sense ! |
I've tried to bump from 4.7.0 to 4.8.0 on highly inefficient project :)
I think this PR could be related. |
@goloveychuk : Is this a public project or are you able to share a smaller subset of the repo (package.jsons, lock & yarn config?) Assuming it uses a public feed of course. |
No. It's private. |
@dannyvv |
@goloveychuk can you run and try |
it works |
## What's the problem this PR addresses? It seems we forgot to call `discardAndClose` in #6723. Ref: #6723 (comment) ## How did you fix it? Calls the function. ## 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.
What's the problem this PR addresses?
In pnpm mode we are seeing ZipFS instances created multiple times for the same package file in the cache. On large projects this easily leads to Out Of Memory Errors.
This is because the 'Link' phase runs worker threads and the LazyFs instance creates ZipFS on demand. Due to multipel threads accessing the FileSystem asyncroneously in the Link phase, threading issues causing the factory inside LazyFs to be called and ZipFS instances to be created multiple times.
We have a bunch of large internal packages (between 100Mb and 200Mb) that are referenced by many packages causing sometimes up to 6 instances of ZipFS for a single package. This makes the WebAssembly instance to run out of memory very fast :)
#6722 Has more details on the conclusion and a discussion of approachers
Resolves #6722
...
How did you fix it?
I created a
RefCountedCache
class that has anaddOrCreate
function that takes a key. (In our use case we pass the zip's cached path as the key) and a function to create create theZipFS
when needed. internally that class uses reference counting for handles and will return the cached object when there is already one live. It will also return a release function that will decrease the refcount and will call the destructor passed into the constructor of theRefCountedCache
. In this case the destructor called will beZipFS.discardAndClose
In the out of range cases the addOrCreate and the release call will throw an error.
I provided a unittest to cover the basic RefCountedCache implementation and relied on integration tests and local testing in our repo for validation.
...
Checklist