Repository navigation
Use new native text-buffer implementation - #14435
Conversation
ffe27b4 to
abc9b36
Compare
abc9b36 to
a8594d4
Compare
a8594d4 to
6c0b70f
Compare
We used to emit buffer added events before the buffers were loaded, so we would end up fetching their git status soon afterwords, when they emitted the did-reload event.
|
@atom/maintainers I think this PR is ready for others to test. @ungb @Ben3eeE Some areas that could use testing: Loading and saving buffers. This includes actual I/O as well as encoding/decoding. Areas in which I could imagine regressions:
Working with buffers with large numbers of unsaved edits. All the operations that I've tested perform as well or better than before, but because of the new buffer representation, I could imagine some operations getting slower. We need to make sure that doesn't happen. Some types of operations I'm thinking of:
Editing very large files. This PR expands the size of files that can be reasonably edited in Atom. But there are still issues with files that are sufficiently large. I'm aware of some issues, like the fact that cursors and scrollbars don't work after a certain number of lines (#13459). Overall, what I care about is that this PR significantly increases the file size that we can work with and does not make any behaviors worse with large files. 32-bit platforms. I did some testing on a 32-bit linux VM, but possibly not enough. |
|
Download links for convenience:
|
Resolves #12 Refs atom/atom#14435 Signed-off-by: Mike Fiedler <[email protected]>
Context: the `TextBuffer.save` method became async in atom/atom#14435; instead of doing a synchronous write, it initiates an asynchronous write and return a promise when the write completes.
| @destroyItem(item) for item in @getItems() | ||
| return | ||
| Promise.all( | ||
| @getItems().map(@destroyItem.bind(this)) |
There was a problem hiding this comment.
@maxbrunsfeld
Noticed while I read code, using bind here is bad.
Since Array.prototype.map pass item index as 2nd arg, which goes to @destroyItems which treat 2nd arg as force switch. Which is not intentional I believe.
There was a problem hiding this comment.
Wow, thanks for catching that @t9md! Are you interested in opening a PR fixing that bug?
| Promise.all( | ||
| @getItems() | ||
| .filter((item) => item isnt @activeItem) | ||
| .map(@destroyItem.bind(this)) |
|
@Glavin001 @joefitzgerald The buffer.onWillSave(async () => {
await somethingSlow()
})See atom/text-buffer#254. This change has been released in Atom v1.21.0-beta1. |
|
Awesome! Thank you, @maxbrunsfeld! |
Summary
This pull request completely reimplements the
TextBufferclass - Atom's core data structure representing editable text. The new implementation differs from the old one in several major ways:Motivation
For many purposes, the existing array-of-lines implementation was actually very fast; we've never observed slowness associated with the querying or mutating of the text itself. This is a testament to V8's highly efficient String operations. However, there are many operations in Atom that use the
TextBufferand would benefit from being dropped to C++ and/or being moved to a background thread.Here are some examples of this that have already been addressed in this PR:
TextBufferrequired its entire contents to be represented as a single JavaScript String. V8 has a hard limit on string length, so this would cause errors (Uncaught RangeError: Invalid string length #7210). Now, saving and loading are done in a background thread in C++, with minimal copying of data.TextBufferchanges on disk, we diff the file's new contents with the buffer's contents in order to update the buffer in a way that preserves marker positions. Previously, this diff computation caused unresponsiveness in large files (Add file-size threshold for diff-based reload text-buffer#153). Now the diff is performed in C++ and on a background thread.TextBufferuses. Because we support files with mixed line endings, this entails scanning the entire buffer. Previously, this would take upwards of 50ms for a large file. Now, the search is performed asynchronously so that it causes no noticeable lag.In addition to these improvements, this PR will make it very easy to optimize other features. Here are some follow-up PRs that I have in mind:
DisplayLayertakes a long time. This is one of the main reasons that pasting a huge amount of text is slow. Some of this character-by-character computation could probably be done much faster in C++.SymbolStorecan take a very long time for large files, so we had to disable it for large files. Fuzzy matching should be able to be performed in C++ on a background thread, eliminating this restriction.Implementation details
Our new representation of the buffer:
Suppose I create a
TextBufferwith some initial text and then make three edits:This buffer is now represented internally like this:
The buffer is composed of two layers. The first layer stores the buffer's initial text as a flat, immutable string. This makes freshly-opened files very compact in memory. The second layer stores the unsaved edits in an object that we call a
Patch. ThePatchis structured as a splay tree so that mutations and queries are very efficient, especially in areas that have recently been accessed.Then, suppose I begin some asynchronous operation like saving the buffer to disk, or searching
the buffer for a regex. Then, while that operation is still happening, I make some additional edits:
At that point, the buffer's internal state would look like this:
There are now three layers. Because the asynchronous
saveis still in progress, further changes that we make to the buffer are saved in a new Patch. When thesavecompletes, we flatten the text down into one layer again.Effect on packages
I don't believe making
saveasync has affected any of our bundled packages' functionality, though it has caused some test failures that I've had to fix:Depends on atom/text-buffer#225
Depends on atom/superstring#5
Depends on atom/node-pathwatcher#121
Depends on atom/whitespace#157
Closes #7210
Closes #11554
Closes #11012