Skip to content
This repository was archived by the owner on Mar 3, 2023. It is now read-only.

Optimize markers via new layers API and treap-based index - #9426

Merged
nathansobo merged 53 commits into
masterfrom
ns-editor-marker-layers
Nov 10, 2015
Merged

nathansobo merged 53 commits into
masterfrom
ns-editor-marker-layers

Conversation

@nathansobo

Copy link
Copy Markdown
Contributor

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 e in jquery.js now 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 a MarkerLayer object. If maintainHistory is true, markers will be restored on calls to undo/redo.
    • .getMarkerLayer Retrieve a marker layer based on its id.
  • MarkerLayer Some highlights:
    • .markRange Creates a marker.
    • .getMarker Gets a marker by id.
    • .findMarkers Queries the marker layer.
    • .onDidUpdate Called 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.
    • .onDidCreateMarker This 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.
    • .copy Create and return a new marker layer with all the same markers.
  • TextEditor
    • .addMarkerLayer({maintainHistory}) Creates and returns a TextEditorMarkerLayer.
    • .getMarkerLayer Retrieve an editor-level marker layer based on its id.
  • TextEditorMarkerLayer This is a wrapper around a buffer-level MarkerLayer object and shares the same id.
    • .markBufferRange Create a marker.
    • .markScreenRange Create a marker.
    • .findMarkers Query markers with the same API as TextEditor.prototype.findMarkers

Marker 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 a MarkerLayer or TextEditorMarkerLayer and the same properties that decorateMarker takes. 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 a LayerDecoration.
  • LayerDecoration
    • setPropertiesForMarker(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. Pass null to clear the override.

New Marker Index

marker-binary-tree

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

screen shot 2015-11-03 at 1 20 25 pm

#### Inserting newlines

~35 ms per keystroke

screen shot 2015-11-03 at 1 20 11 pm

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

1.6 seconds

screen shot 2015-11-03 at 11 48 37 am

#### Inserting newlines

~30 ms per keystroke

screen shot 2015-11-03 at 11 51 12 am

Zooming in, the newline insertion contains 3 ~5ms calls to MarkerIndex.prototype.splice:

screen shot 2015-11-03 at 12 21 51 pm

#### Memory consumed

36.2 MB

screen shot 2015-11-03 at 1 15 50 pm

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

~1 second

screen shot 2015-11-03 at 12 45 31 pm

#### Inserting newlines

~7-10 ms per keystroke

screen shot 2015-11-03 at 12 48 16 pm

Only 1 significant splice operation, 0.8ms.

screen shot 2015-11-03 at 12 52 55 pm

#### Memory consumed

34.8 MB (not a huge savings)

screen shot 2015-11-03 at 1 13 42 pm

@benogle

benogle commented Nov 4, 2015

Copy link
Copy Markdown
Contributor

💥

@EasyHard

EasyHard commented Nov 5, 2015

Copy link
Copy Markdown
Contributor

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.

@nathansobo
nathansobo force-pushed the ns-editor-marker-layers branch from b2beadd to 01f7e47 Compare November 5, 2015 19:46
@nathansobo

Copy link
Copy Markdown
Contributor Author

Hi @nathansobo, I have a feeling that maybe we can use splay tree instead of treap since operations have locality

@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.

@nathansobo
nathansobo force-pushed the ns-editor-marker-layers branch 4 times, most recently from 57a462f to 1f35bb6 Compare November 7, 2015 23:03
@nathansobo

Copy link
Copy Markdown
Contributor Author

I converted text-editor-component-spec.coffee to Babel as part of this PR. Kind of off topic, but I was having trouble understanding asynchronous sequencing in that test after removing all the mocking, and I really wanted async/await. Turns out most of my issues were probably related to throttling, but the conversion was a ton of work so I'm keeping it.

@nathansobo
nathansobo force-pushed the ns-editor-marker-layers branch 5 times, most recently from 66f4822 to db63e55 Compare November 8, 2015 22:41
@nathansobo

Copy link
Copy Markdown
Contributor Author

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.

@nathansobo
nathansobo force-pushed the ns-editor-marker-layers branch 4 times, most recently from 47aaf0a to 1efe7d9 Compare November 9, 2015 00:50
@nathansobo

Copy link
Copy Markdown
Contributor Author

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.

@nathansobo
nathansobo force-pushed the ns-editor-marker-layers branch 3 times, most recently from 584ab5c to abc9e5f Compare November 9, 2015 07:31
maxbrunsfeld and others added 7 commits November 9, 2015 00:35
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
@nathansobo
nathansobo force-pushed the ns-editor-marker-layers branch from abc9e5f to 525d4a5 Compare November 9, 2015 19:20
@nathansobo
nathansobo force-pushed the ns-editor-marker-layers branch from 525d4a5 to c97ecf9 Compare November 9, 2015 21:56
nathansobo pushed a commit that referenced this pull request Nov 10, 2015
Optimize markers via new layers API and treap-based index
@nathansobo
nathansobo merged commit 581d736 into master Nov 10, 2015
@nathansobo
nathansobo deleted the ns-editor-marker-layers branch November 10, 2015 06:02
@nathansobo

Copy link
Copy Markdown
Contributor Author

Refs #9208

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants