Repository navigation
Optimize markers via new layers API and treap-based index - #9426
Conversation
|
💥 |
|
Hi @nathansobo, I have a feeling that maybe we can use splay tree instead of treap since operations have locality. How do you think? If you think it may be a good idea. I would like to give it a shot. |
b2beadd to
01f7e47
Compare
@EasyHard I considered using splay trees early on, but my concern is that a splay tree might not deliver consistent performance. While it is true that the amortized cost of access is optimal, splay trees can get in really unbalanced states such that a given operation would take longer. For batch workloads, this isn't a problem, but since Atom is an interactive application it's important that all operations have a fairly uniform cost. Consider the example of populating the marker index in find and replace... We'll insert a bunch of markers in order, which without rebalancing will create a reversed linked-list structure. Then when we query the list for the first time, we'll have to walk the entire list to get to the beginning of the list. At this point we'll rebalance, but this first operation will be expensive. It may be the case that we'll always find a way to amortize the cost without users noticing, but this wasn't obvious enough to me to want to allow our access patterns to be the only thing balancing the tree. Let me know if you think I'm missing something, but I'd prefer to stick to something more predictable. |
57a462f to
1f35bb6
Compare
|
I converted |
66f4822 to
db63e55
Compare
|
Really wondering what's going on with the build. Worked out most of the flakiness but I'm still timing out on animation frames that never fire on CI. From what I can tell there is a pending frame when I time out, but for some reason it's not occurring. Now testing with an animation loop that logs if there's a frame > 500ms. May throw in the towel and go back to mocking the event loop, but I want to spend a bit more time understanding what the heck is going on with the event loop. |
47aaf0a to
1efe7d9
Compare
|
Okay, I have at least one green build on our internal CI, which hasn't happened yet. So either I got lucky or have found the fix. Now I need to narrow down which of the many things I tried are necessary for stable passing builds. |
584ab5c to
abc9e5f
Compare
Signed-off-by: Max Brunsfeld <[email protected]>
This will make it much easier to test asynchronous update logic.
It tests functionality that no longer matters since we don’t subscribe to decorations being destroyed in DisplayBuffer
It's much easier to reason about async/await than Jasmine's built-in queuing system, and using them made it easier to debug flaky async tests.
From what I understand, nextTick callbacks actually happen at the end of the current event loop cycle rather than the next one. The naming is confusing.
This gives any nextTick callbacks in the model a chance to complete before proceeding with tests.
abc9e5f to
525d4a5
Compare
525d4a5 to
c97ecf9
Compare
Optimize markers via new layers API and treap-based index
|
Refs #9208 |
This PR improves the responsiveness of find-and-replace and other marker-intensive tasks by adding a layer-oriented API for working with collections of related markers and upgrading to a new implementation of the marker index. Searching for
einjquery.jsnow blocks the UI thread for ~1 second instead of ~3.2 seconds. Inserting newlines in this file with the markers present now takes ~10 ms instead of ~35 ms.I still need to make the changes to find-and-replace backwards compatible and merge them after merging these changes.
API Changes
Marker Layers
A huge problem with our existing implementation is that creating markers emits synchronous events to support existing packages and core APIs. We needed a way to opt out of these event handlers for high-volume use cases, and marker layers were a good way to do this.
TextBuffer.addMarkerLayer({maintainHistory})Creates and returns aMarkerLayerobject. IfmaintainHistoryistrue, markers will be restored on calls to undo/redo..getMarkerLayerRetrieve a marker layer based on its id.MarkerLayerSome highlights:.markRangeCreates a marker..getMarkerGets a marker by id..findMarkersQueries the marker layer..onDidUpdateCalled asynchronously whenever markers are created, updated, or destroyed. Can be used without being a drag on performance, but you'll have to re-query any state you're interested in between calls..onDidCreateMarkerThis is needed to support our current synchronous selection APIs, but it shouldn't be used on layers that will contain a large number of markers because it slows down insertion..copyCreate and return a new marker layer with all the same markers.TextEditor.addMarkerLayer({maintainHistory})Creates and returns aTextEditorMarkerLayer..getMarkerLayerRetrieve an editor-level marker layer based on its id.TextEditorMarkerLayerThis is a wrapper around a buffer-levelMarkerLayerobject and shares the same id..markBufferRangeCreate a marker..markScreenRangeCreate a marker..findMarkersQuery markers with the same API asTextEditor.prototype.findMarkersMarker Layer Decorations
Another huge source of slowness in find and replace is that we decorated each marker individually. Now that we have marker layers, we can decorate an entire layer in the same way, avoiding a ton of allocations.
TextEditor.decorateMarkerLayer(layer, properties)Accepts aMarkerLayerorTextEditorMarkerLayerand the same properties thatdecorateMarkertakes. It creates a decoration for every marker in the layer based on these parameters. This won't work for custom gutter decorations or overlay decorations, but will work for highlights, line, and line number decorations. Returns aLayerDecoration.LayerDecorationsetPropertiesForMarker(marker, properties)This can be used to override the properties of a particular marker in the layer. We needed this to support the "current result" in find and replace without major changes to the package code. Passnullto clear the override.New Marker Index
This PR also incorporates a new binary-tree-based marker-index, which replaces the previous b-tree-based index in text-buffer. It offers better and more predictable performance... about 33% faster on my insertion tests (from 1.6 second to 1 second) and an order of magnitude faster at splicing. 1 second is still too long to block the UI thread, so we may still need to background the index population. I would like to experiment with a C++ implementation and see what it buys us, since it's a straightforward implementation that should be fairly easy to port.
Performance Improvements
Now on to the good part... the improvement. This shows results from 3 implementations. The current version, marker layers with the old index, and marker layers with the new index.
Without marker layers (current implementation)
Inserting ~20k markers:
~3.2 seconds

#### Inserting newlines~35 ms per keystroke

### Old marker index #### Inserting ~20k markers:1.6 seconds

#### Inserting newlines~30 ms per keystroke
Zooming in, the newline insertion contains 3 ~5ms calls to
MarkerIndex.prototype.splice:
#### Memory consumed36.2 MB

### New marker index #### Inserting ~20k markers~1 second

#### Inserting newlines~7-10 ms per keystroke
Only 1 significant splice operation, 0.8ms.

#### Memory consumed34.8 MB (not a huge savings)