Repository navigation
Proposal: Marker layers #9208
Description
Supplants #9137
Find and replace currently scales poorly with the number of results. Based on a test of searching for the e character in jquery.js that took 2932ms, the biggest bottlenecks as follows:
- 670ms ~20% – Creating and subscribing to individual decorations for each result.
- 1752ms 60% – Creating markers, of which:
- 893ms – Inserting markers into current marker index.
- 533ms – Propagating marker creation events
- 125ms ~4% – Translating character offsets to rows and columns.
Based on discussion with @maxbrunsfeld and @as-cii, this is a proposal for changes to core APIs that attacks these bottlenecks directly.
Marker Layers
TextBuffer.prototype.addMarkerLayer creates and returns a MarkerLayer object that represents a set of markers.
Creating Markers
This object has a markRange method that takes a 2D rows, column range as normal, but it also has a markCharacterIndexRange that will eliminate the need to translate character indices to points during our scan. Both marker creation methods return an integer id instead of an object, which can be passed to getMarker to retrieve the actual marker object for direct manipulation. Working in terms of ids rather than objects saves us from allocating large quantities of what are essentially "convenience" objects until they are needed. We could potentially avoid the need to ever allocate marker objects by manipulating individual markers via methods on MarkerLayer that take an id, such as updateMarkerRange(id, range) or destroyMarker(id). However, this may interact poorly with existing APIs and would need to be carefully considered.
Observing Updates
MarkerLayers will also have an asynchronous, batch-oriented change subscription function called onUpdate. For more information on our need to transition asynchronous events in the model and the naming scheme that could facilitate this transition, see #9207.
Subscribers to onUpdate will be called once for any number of marker changes occurring in a given tick of the event loop. If needed, we can emit an event object containing arrays of created, updated, and destroyed marker ids, but since the view re-queries the marker index anyway during render, we may want to wait until we have a use case. I do however have a feeling that subscribers could be interested in this information.
Decorating Marker Layers
To avoid the overhead of creating and subscribing to thousands of decorations, we can add TextEditor::decorateMarkerLayer that accepts a marker layer along with a decoration descriptor object that should apply to every range in that layer. This will allow us to create and decorate the search results layer once, and from then on just manipulate markers.
Other Methods
Markers created via TextBuffer's existing markRange method will be created in a default marker layer, which can be accessed via TextBuffer.prototype.getDefaultMarkerLayer. Otherwise, I don't think we need to provide accessors or observation methods for marker layers, as they can be explicitly passed to functions that need them.
Finally, MarkerLayer will need the utility methods clear and destroy.
Creating markers in the background
Finally, if the overhead of inserting markers into the index is still too slow using the new 1D marker index and avoiding the overhead of synchronous events, we're going to need to populate it in the background. This would add two complementary methods to the MarkerLayer, serialize and deserialize. They would delegate through to our marker index object, which we could construct directly in the scandal background process and then serialize to be populated into a highlighted marker layer after the scan completed.
Activity
maxbrunsfeld commentedon Oct 19, 2015
After a conversation with @nathansobo this morning, we're thinking of the starting with the following changes:
TextBuffer::addMarkerLayer()method.The returned
MarkerLayerwill have the same API thatTextBufferhas now for dealing with markers. For example,markRangewill still return aMarkerobject which can be observed with::onDidChange,::onDidDestroy, etc. This keeps the conceptual overhead of this feature pretty low; it simply generalizes the existing marker API by allowing multiple layers. We have already done the work to eliminate the overhead of supporting marker observation methods in the case where they aren't used.::addMarkerLayerthrough a thin wrapper method onTextEditor.TextEditor::decorateMarkerLayerThis method will return a
LayerDecorationthat the caller candestroy()orsetProperties()on.find-and-replaceto create its markers in a custom marker layer and to decorate the entire layer instead of decorating individual markers.These changes will save time when running large searches in the following ways:
Decorationobjects::onDidCreateMarkerobservers will fire when creating markers in a custom layer.nathansobo commentedon Oct 19, 2015
I don't however think layers should ever support an
onDidCreateMarkerAPI. You in agreement with that?Maybe you should update the body of the issue with our amendments so things stay clear.
nathansobo commentedon Oct 19, 2015
Also, is it worth having
MarkerLayer.prototype.clearor should we just destroy it and create a new one. Clear could be nice because we could leave the existing layer decoration.as-cii commentedon Oct 20, 2015
I agree that having a
clearmethod would be nice: I am not sure if there is any performance gaining in doing so, but it'd make the API simpler and easier to reason about. 👍Awesome work on this, guys! ❤️
nathansobo commentedon Oct 26, 2015
@abe33 This will be a solution that continues to create markers for all decorations. As part of this, we'll need to make a new API for reporting decorations from the model to the view, which presumably
minimapcould also call. However, I'm still concerned about the expense of retrieving every single decoration and translating its range to screen coordinates when there are tens of thousands of highlights. I'm wondering if we should expose agetDecorationCountmethod so you can avoid retrieving decorations above a certain threshhold until we can perform this query more efficiently. Thoughts?Edit: It's also worth noting that you'll need to subscribe to a coarse-grained
onDidUpdateDecorationsmethod, which will be asynchronous, rather than individual decoration creation events.nathansobo commentedon Oct 26, 2015
@bolinfest You may be interested in our plans here as I see Nuclide makes use of the current
decorationsForScreenRowRangeAPI. That API will no longer include decorations that we move to marker layers and we'll probably be deprecating it in favor of an API that can do so more easily... There would be no rush on its ultimate removal though.lee-dohm commentedon Dec 7, 2015
Some discussion around challenges due to marker layers:
https://discuss.atom.io/t/problem-with-findmarkers-function-of-texteditor/23416
/cc @atom/feedback
abe33 commentedon Dec 7, 2015
following the discussion on discuss
@nathansobo No problem, I should have noticed your comments here sooner (atom's notifications are just too overwhelming to follow every changes with only my spare time). Actually I think this is pretty a interesting change and I can already see the benefit I could get from the marker layers in Pigments.
But I also think that the API should be a bit more friendly for third party packages. Naming layers and making the name mandatory would allow for easier reuse of layers; minimap is one use case, the scrollbar markers as in the discuss thread is another, and I can already think to some more just for find and replace markers, but without a proper way to get the results each of these application will break at some point.
Anyway, now that I know how to detect the change and how to deal with the layer there won't be any problem when it'll get released.
nathansobo commentedon Dec 7, 2015
@abe33 I'm actually glad that packages can control access to their markers now, because it should probably be a package-by-package choice whether their markers are an implementation detail or part of the package's API. In this case, I'm thinking about adding a service for finding find and replace markers that the find and replace package can provide. I could potentially hotfix that into beta so there's a workaround that's less hacky.
nathansobo commentedon Dec 7, 2015
Hmm, after thinking about this a bit more, I think I'd like to defer adding a service until we have a chance to revisit find-and-replace in a deeper way. Currently we only create markers in a single buffer at a time, which makes it hard to give the service a decent interface. If you want to pitch some ideas on an API you'd like with the current setup, that would be good. But for now I'd like to wait until we can retrieve markers easily for any editor in the workspace, which would mean substantial changes to our approach in find-and-replace. If the hack-around is workable, I'd like to roll with it for a bit.
nathansobo commentedon Dec 8, 2015
Okay, changed my mind. Adding a service in atom/find-and-replace#614 to retrieve the results layer for any text editor. Should be a pretty straightforward fix once that's in and you shouldn't have to hack around stuff.
stale commentedon Aug 29, 2017
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
maxbrunsfeld commentedon Aug 29, 2017
This was implemented in atom/text-buffer#106
lock commentedon Mar 31, 2018
This issue has been automatically locked since there has not been any recent activity after it was closed. If you can still reproduce this issue in Safe Mode then please open a new issue and fill out the entire issue template to ensure that we have enough information to address your issue. Thanks!