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

Proposal: Marker layers #9208

Closed
Closed

Description

@nathansobo

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

  1. self-assigned this
    on Oct 19, 2015
  2. maxbrunsfeld commented on Oct 19, 2015

    @maxbrunsfeld
    Contributor

    After a conversation with @nathansobo this morning, we're thinking of the starting with the following changes:

    • Add the TextBuffer::addMarkerLayer() method.

    The returned MarkerLayer will have the same API that TextBuffer has now for dealing with markers. For example, markRange will still return a Marker object 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.

    • Expose ::addMarkerLayer through a thin wrapper method on TextEditor.
    • Add TextEditor::decorateMarkerLayer

    This method will return a LayerDecoration that the caller can destroy() or setProperties() on.

    • Convert find-and-replace to 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:

    • We won't have to construct and subscribe to changes to a large number of Decoration objects
    • No ::onDidCreateMarker observers will fire when creating markers in a custom layer.
    • When the search is updated, the entire layer can be deleted, instead of having to individually delete the markers from a shared layer.
  3. nathansobo commented on Oct 19, 2015

    @nathansobo
    ContributorAuthor

    We have already done the work to eliminate the overhead of supporting marker observation methods in the case where they aren't used.

    I don't however think layers should ever support an onDidCreateMarker API. You in agreement with that?

    Maybe you should update the body of the issue with our amendments so things stay clear.

  4. nathansobo commented on Oct 19, 2015

    @nathansobo
    ContributorAuthor

    Also, is it worth having MarkerLayer.prototype.clear or should we just destroy it and create a new one. Clear could be nice because we could leave the existing layer decoration.

  5. as-cii commented on Oct 20, 2015

    @as-cii
    Contributor

    Also, is it worth having MarkerLayer.prototype.clear or should we just destroy it and create a new one. Clear could be nice because we could leave the existing layer decoration.

    I agree that having a clear method 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! ❤️

  6. nathansobo commented on Oct 26, 2015

    @nathansobo
    ContributorAuthor

    @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 minimap could 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 a getDecorationCount method 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 onDidUpdateDecorations method, which will be asynchronous, rather than individual decoration creation events.

  7. nathansobo commented on Oct 26, 2015

    @nathansobo
    ContributorAuthor

    @bolinfest You may be interested in our plans here as I see Nuclide makes use of the current decorationsForScreenRowRange API. 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.

  8. removed their assignment
    on Nov 11, 2015
  9. lee-dohm commented on Dec 7, 2015

    @lee-dohm
    Contributor
  10. abe33 commented on Dec 7, 2015

    @abe33
    Contributor

    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.

  11. nathansobo commented on Dec 7, 2015

    @nathansobo
    ContributorAuthor

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

  12. nathansobo commented on Dec 7, 2015

    @nathansobo
    ContributorAuthor

    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.

  13. nathansobo commented on Dec 8, 2015

    @nathansobo
    ContributorAuthor

    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.

  14. stale commented on Aug 29, 2017

    @stale

    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.

  15. maxbrunsfeld commented on Aug 29, 2017

    @maxbrunsfeld
    Contributor

    This was implemented in atom/text-buffer#106

  16. lock commented on Mar 31, 2018

    @lock

    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!

  17. locked and limited conversation to collaborators on Mar 31, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions