You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Explore using ArrayBuffer as backend for editor text contents #167719
Explore to not use string but ArrayBuffer for the contents of a text editor which brings higher memory limits. See #167719 (comment)
Original Issue (now outdated)
I see how https://github.com//issues/42839 introduced a way to avoid opening large files by telling the user to quit and restart with a larger `--max-memory`. First of all, I think asking the user to confirm opening a very large file is good because it may result in freezes or slowdown. However, I am no longer able to reproduce the endless freeze, even when opening a very large file (see comment [1] how to create a large file). Maybe given our work on sandboxing this issue is now different or not existent anymore?
validate that our original assumptions for adding this still apply
see if --max-memory still works with Electron 22
and if not, maybe we change the UX to still block opening the file at first by asking the user for confirmation (similar to how we ask for confirmation before opening a binary file)
[1] How to generate a large file
Inspired by this page, the following script that you can run from a macOS terminal produces a 4.23 GB random file for me after ~2min:
The real challenge is currently most our editor internal parts eagerly read line contents (through getLineContent). If we cache lineContent, it defeats the purpose of using ArrayBuffer. If we don't, then a component attempts to read every line content of the text model, might block the whole UI if the file is large. I have already moved getLineContent to getLineCharCode if possible in https://github.com/microsoft/vscode/tree/rebornix/anonymous-donkey but following components are not easy to move off line content:
Hi Everyone, I still see this issue happening to me whenever I try to open big files (Example: Larger than 10 mb) will all installed plugin and settings. Any update on this?
Fyi I have removed support for --max-memory and related editor integrations from our product via 6d5b854. Turns out that max_old_space_size is unsupported since a very long time (electron/electron#31330).
changed the title [-]Revisit large file handling with `max-memory`[/-][+]Explore using `ArrayBuffer` as backend for editor text contents[/+]on Apr 18, 2023
However, if you make such a large editor dirty, we seem to write the contents entirely in one buffer to disk as backup
Benjamin Pasero (@bpasero) this is exactly what I was seeing, backup kicks in and attempts to save the whole concatenated buffer. I'll give it another check once it's fixed.
It means for file opening, we don't have to use array buffer as the backing store as piecetree never modify the string loaded so they will always be ExternlString, modifications will be stored in a new separate string buffer (as an internal string). I'll try to trigger OOM through editing large files and see if I can generate internal string objects > 4GB.
Robo (@deepak1556)Benjamin Pasero (@bpasero) are you aware of any duplicate issues where users run into crashes due to heap limit (based on the crash dump they shared)? My current hypothesis is users ran into trouble as we have editor contributions which try to read every line (for example, intent guide used to read every line content) and trigger massive internal string objects to be created on heap. Now I have all of them disabled, the chances of running into the OOMs is very low.
We discussed offline on latest learnings and issues we found, here is a gist of them:
✅ When VS Code opens large file, Nodejs decoder creates External*String which are not allocated on V8's heap. So technically we are not limited by the 4GB cap for just reading the file.
VS Code can still crash if there is enough memory on the system
Currently all core and contrib features that are reading line content are disabled, including word wrap, codelens, folding, indent guide, sticky scroll, word highlighting and link detection. This ensures a smooth scrolling experience once the file is loaded into PieceTree
We might want to add some checks or tests to ensure that we don't regress this in the future.
In our codebase, we are using | 0 in various places to ensure numbers are all integer (no float, undefined, nan), but it limits the maximum line count. A file with >120M lines of code can not be opened, tracked in Editor is broken when opening files with >120M lines #193164
Editing. Modifying the large text buffer can still lead to OOMs as edits are allocated on heap, we have two potential solutions:
Open large files (>1GB) in readonly mode, and when users attempt to edit it, warn users that this might not work (freeze or crash VS Code) and ask for confirmation if they want to continue
Adopt ArrayBuffer for the editing buffer in PieceTree, so we could support modification up to 16GB
We want to ensure that all saving operations are using streaming. Currently backup concatenates all buffers from the snapshot, so a snapshot larger than 4GB can not be saved. This also blocks the UI for seconds to minutes. Issue is tracked in Backup file system provider can not do buffered write #193151
I agree we shold keep the PieceTree unchanged given we can open files >4GB
The | 0 thing is in there since perhaps 10 years, so I don't think we ever could open more than 120M lines of code :)
IIRC the used memory for editing grows directly proportional with the edit size. So small edits in such a large text buffer should work fine, right? I guess things like Find/Replace All will not work so well because they could end up allocating a lot of pieces and then running out of memory. Maybe there's a cheap way to detect this case before the crash before applying an edit? We could just refuse to apply it and inform the user.
The | 0 thing is in there since perhaps 10 years, so I don't think we ever could open more than 120M lines of code :)
agree, it's not worth it to optimize for such corner case.
Maybe there's a cheap way to detect this case before the crash before applying an edit? We could just refuse to apply it and inform the user.
Great suggestion. I added an additional check in the text model via #193309 and validate if the model is too large for any expensive heap operations:
Find contrib notifies users that the model is too large so we can't do a Replace All otherwise it might crash
It now throws error when we use getValue or getLinesContent on a model that's larger than 512MB, this will use 512MB memory on Heap the moment they are invoked.
We could potentially check the heap usage for each edit on the model, and validate if they might exceed the heap limit like what you did in
Benjamin Pasero (@bpasero) not just ArrayBuffer -- VSCode should use SharedArrayBuffer for editors and terminals so that it can actually do long-running work on background threads.
Activity
bpasero commentedon Dec 21, 2022
Some interesting discoveries:
4gbeach random files without problems from the E22 branchArrayBufferis granted a larger space than 4 GB because (as far as I understand) they are not allocated on the heapmaxFileSizeandmaxHeapSizedriving the decision if such a large file can open and I had to disable that [1][1]
vscode/src/vs/platform/files/common/files.ts
Lines 1321 to 1322 in 9718242
rebornix commentedon Dec 31, 2022
We discussed offline about this task and one of the idea is using
ArrayBufferdirectly as the backing store for the text buffer instead of using string so we won't be affected by the heap size limit. I experimented a bit (in https://github.com/microsoft/vscode/tree/rebornix/anonymous-donkey) and found moving from string to array buffer for PieceTree is pretty easy.The real challenge is currently most our editor internal parts eagerly read line contents (through
getLineContent). If we cachelineContent, it defeats the purpose of using ArrayBuffer. If we don't, then a component attempts to read every line content of the text model, might block the whole UI if the file is large. I have already movedgetLineContenttogetLineCharCodeif possible in https://github.com/microsoft/vscode/tree/rebornix/anonymous-donkey but following components are not easy to move off line content:vscode/src/vs/editor/contrib/folding/browser/indentRangeProvider.ts
Line 155 in 4786a49
vscode/src/vs/editor/common/viewModel/viewModelLines.ts
Line 133 in 4786a49
bpasero commentedon Dec 31, 2022
👏 for pushing this forward, would be great if we can look into the 2 remaining ones, I am happy to help if possible.
tahmidbintaslim commentedon Feb 10, 2023
Hi Everyone, I still see this issue happening to me whenever I try to open big files (Example: Larger than 10 mb) will all installed plugin and settings. Any update on this?
bpasero commentedon Feb 10, 2023
What issue?
bpasero commentedon Apr 18, 2023
Fyi I have removed support for
--max-memoryand related editor integrations from our product via 6d5b854. Turns out thatmax_old_space_sizeis unsupported since a very long time (electron/electron#31330).[-]Revisit large file handling with `max-memory`[/-][+]Explore using `ArrayBuffer` as backend for editor text contents[/+]6 remaining items
Re #167719. Disable word highlighting, codelens, sticky provider when…
deepak1556 commentedon Sep 14, 2023
Peng Lyu (@rebornix) were you able to find a file test case that triggers a OOM crash due to the heap limit ?
rebornix commentedon Sep 14, 2023
Benjamin Pasero (@bpasero) this is exactly what I was seeing, backup kicks in and attempts to save the whole concatenated buffer. I'll give it another check once it's fixed.
Robo (@deepak1556) I could not find a test case that can exceed heap limit, it seems nodejs decoder always creates
External*strings (as shown in below screenshot) for performance reasons(string_bytes.cc). That's why when we open a 4GB file, VS Code doesn't crash at all (Benjamin Pasero (@bpasero) pointed out the same in #167719 (comment))It means for file opening, we don't have to use array buffer as the backing store as
piecetreenever modify the string loaded so they will always beExternlString, modifications will be stored in a new separate string buffer (as an internal string). I'll try to trigger OOM through editing large files and see if I can generate internal string objects > 4GB.Robo (@deepak1556) Benjamin Pasero (@bpasero) are you aware of any duplicate issues where users run into crashes due to heap limit (based on the crash dump they shared)? My current hypothesis is users ran into trouble as we have editor contributions which try to read every line (for example, intent guide used to read every line content) and trigger massive internal string objects to be created on heap. Now I have all of them disabled, the chances of running into the OOMs is very low.
deepak1556 commentedon Sep 14, 2023
Most recent one I got was #184298, I will check for other similar issues in my list.
Re microsoft#167719. Disable word highlighting, codelens, sticky prov…
rebornix commentedon Sep 15, 2023
We discussed offline on latest learnings and issues we found, here is a gist of them:
External*Stringwhich are not allocated on V8's heap. So technically we are not limited by the 4GB cap for just reading the file.PieceTree| 0in various places to ensure numbers are all integer (no float, undefined, nan), but it limits the maximum line count. A file with >120M lines of code can not be opened, tracked in Editor is broken when opening files with >120M lines #193164ArrayBufferfor the editing buffer inPieceTree, so we could support modification up to 16GBalexdima commentedon Sep 15, 2023
Great finds Peng Lyu (@rebornix) !
PieceTreeunchanged given we can open files >4GB| 0thing is in there since perhaps 10 years, so I don't think we ever could open more than 120M lines of code :)Re #167719. Avoid heap operations when dealing with large files.
Re #167719. Avoid fetching whole editor content if the model is too l…
rebornix commentedon Sep 17, 2023
Thanks Alexandru Dima (@alexdima)!
agree, it's not worth it to optimize for such corner case.
Great suggestion. I added an additional check in the text model via #193309 and validate if the model is too large for any expensive heap operations:
Replace Allotherwise it might crashgetValueorgetLinesContenton a model that's larger than 512MB, this will use 512MB memory on Heap the moment they are invoked.vscode/src/vs/editor/common/services/modelService.ts
Lines 305 to 323 in 3b60c12
_disposedModelsHeapSize. Didn't implement this yet as it might be overkill.Re #167719. Avoid heap operations when dealing with large files. (#19…
Re microsoft#167719. Disable word highlighting, codelens, sticky prov…
jedwards1211 commentedon Jun 27, 2024
Benjamin Pasero (@bpasero) not just
ArrayBuffer-- VSCode should useSharedArrayBufferfor editors and terminals so that it can actually do long-running work on background threads.