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(lsp): Don't retain SourceFileObjects in sourceFileCache longer than necessary #23258

Merged
merged 3 commits into from
Apr 7, 2024

Conversation

nathanwhit
Copy link
Member

The TS language service requests source files via getSourceFile. In that function, we unconditionally add the source file to our sourceFileCache. The issue is that we only remove things from that cache if the source file becomes out of date. For files that don't get changed, we keep them in the cache indefinitely. So sometimes we keep SourceFile objects from being GC'ed because they're retained in our cache, even though TS doesn't refer to them any more. I see this in pretty much all of the heap snapshots I've taken.


The fix here is pretty direct - just store weak references to the sourcefiles in the cache. It doesn't really change our caching behavior, it just prevents us from being the only retainer of a SourceFile. I also split the sourceFileCache into a separate cache just for assets, as we rely on those being alive.

The simpler fix is to only cache assets, but presumably that has a perf impact.


In local testing, this PR reduced the size of the JS heap by about 1 GB when using deno lsp in the Typescript repo.

@nathanwhit nathanwhit changed the title perf(lsp): Only cache SourceFile objects in sourceFileCache longer than necessary perf(lsp): Don't retain SourceFile objects in sourceFileCache longer than necessary Apr 7, 2024
@nathanwhit nathanwhit changed the title perf(lsp): Don't retain SourceFile objects in sourceFileCache longer than necessary perf(lsp): Don't retain SourceFileObjects in sourceFileCache longer than necessary Apr 7, 2024
Copy link
Collaborator

@nayeemrmn nayeemrmn left a comment

Choose a reason for hiding this comment

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

LGTM

@nathanwhit nathanwhit merged commit b74a4f2 into denoland:main Apr 7, 2024
17 checks passed
@nathanwhit nathanwhit deleted the source-file-cache branch April 7, 2024 02:22
nayeemrmn added a commit to nayeemrmn/deno that referenced this pull request Apr 8, 2024
nayeemrmn added a commit that referenced this pull request Apr 8, 2024
satyarohith pushed a commit that referenced this pull request Apr 11, 2024
…er than necessary (#23258)

The TS language service requests source files via
[getSourceFile](https://github.com/nathanwhit/deno/blob/7a25fd5ef0a82c2aac76594ccd467e9210e92b80/cli/tsc/99_main_compiler.js#L560).
In that function, we [unconditionally
add](https://github.com/nathanwhit/deno/blob/7a25fd5ef0a82c2aac76594ccd467e9210e92b80/cli/tsc/99_main_compiler.js#L613-L614)
the source file to our sourceFileCache. The issue is that we only remove
things from that cache if the source file [becomes out of
date](https://github.com/nathanwhit/deno/blob/7a25fd5ef0a82c2aac76594ccd467e9210e92b80/cli/tsc/99_main_compiler.js#L777-L783).
For files that don't get changed, we keep them in the cache
indefinitely. So sometimes we keep SourceFile objects from being GC'ed
because they're retained in our cache, even though TS doesn't refer to
them any more. I see this in pretty much all of the heap snapshots I've
taken.

---

The fix here is pretty direct - just store weak references to the
sourcefiles in the cache. It doesn't really change our caching behavior,
it just prevents us from being the only retainer of a `SourceFile`. I
also split the `sourceFileCache` into a separate cache just for assets,
as we rely on those being alive.

The simpler fix is to only cache assets, but presumably that has a perf
impact.

---
In local testing, this PR reduced the size of the JS heap by about 1 GB
when using `deno lsp` in the Typescript repo.
satyarohith pushed a commit that referenced this pull request Apr 11, 2024
littledivy pushed a commit to littledivy/deno that referenced this pull request Apr 19, 2024
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