Skip to content

Stop retaining a second SourceFile per file in the VFS environment#3616

Open
Gxrvish wants to merge 1 commit into
microsoft:v2from
Gxrvish:fix/vfs-duplicate-source-file-retention
Open

Stop retaining a second SourceFile per file in the VFS environment#3616
Gxrvish wants to merge 1 commit into
microsoft:v2from
Gxrvish:fix/vfs-duplicate-source-file-retention

Conversation

@Gxrvish

@Gxrvish Gxrvish commented Jul 26, 2026

Copy link
Copy Markdown

createVirtualTypeScriptEnvironment's createFile and updateFile parsed a SourceFile and passed it to the virtual compiler host, which stored it in its sourceFiles map. The language service never reads that map: it reads text through getScriptSnapshot and parses and caches its own SourceFiles. Every file written through the environment was therefore held as two full ASTs for the lifetime of the environment, and the parse which produced the second one was discarded.

The environment now writes contents through a text-only path, added as writeFileText on createVirtualCompilerHost and updateFileText on createVirtualLanguageServiceHost. writeFileText drops any cached AST for the file so a standalone compiler host cannot return stale contents. The existing updateFile(sourceFile) entry points are unchanged.

In a 40 file benchmark this cut retained heap from 318MB to 209MB and removed 80 redundant parses.

Fixes: #3614

createVirtualTypeScriptEnvironment's createFile and updateFile parsed a
SourceFile and passed it to the virtual compiler host, which stored it in
its sourceFiles map. The language service never reads that map: it reads
text through getScriptSnapshot and parses and caches its own SourceFiles.
Every file written through the environment was therefore held as two full
ASTs for the lifetime of the environment, and the parse which produced the
second one was discarded.

The environment now writes contents through a text-only path, added as
writeFileText on createVirtualCompilerHost and updateFileText on
createVirtualLanguageServiceHost. writeFileText drops any cached AST for
the file so a standalone compiler host cannot return stale contents. The
existing updateFile(sourceFile) entry points are unchanged.

In a 40 file benchmark this cut retained heap from 318MB to 209MB and
removed 80 redundant parses.
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.

@typescript/vfs: createVirtualTypeScriptEnvironment stores a duplicate SourceFile per file that the language service never reads

1 participant