3 ms·
Atom uses a piece-table-inspired data structure to represent text[0]. We store the file's original contents in a single contiguous buffer. All of the user's uns
by maxbrunsfeld 9y ago
Atom uses a piece-table-inspired data structure to represent text[0]. We store the file's original contents in a single contiguous buffer. All of the user's unsaved edits are then stored in a separate mutable structure called a Patch, which we represent as a splay tree. When we need to read from the buffer in a background thread, we can 'freeze' the patch, and store any subsequent writes in a new patch which is layered on top of the previous one.
Even though we do load the entire file into memory (as opposed to mmap-ing the file), the piece table design is still very useful. It makes it very cheap to compute the buffer's current unsaved change set, which is a value that we periodically serialize for crash recovery purposes (similar to vim's `.swp` files).
It's also just a very compact way to store a large chunk of text, which is good from a cache-locality and memory usage perspective.
[0] https://github.com/atom/superstring/blob/master/src/core/text-buffer.h https://github.com/atom/superstring/blob/master/src/core/tex...