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: Model change observation should be batch-oriented and asynchronous #9207

Closed

Description

@nathansobo

We've gained a lot of performance in Atom by switching from processing events synchronously and one-at-a-time, to processing events asynchronously and in batches.

Asynchrony and batching in the view layer

Originally, we mapped individual changes to the editor model directly to individual updates to the DOM, synchronously.

Now, when the text editor model changes, we schedule an asynchronous batched update of the view on the next animation frame. We write this view update as a straightforward function of the underlying data model, adding indexes to the model where necessary to ensure we can recompute the view efficiently.

An asynchronous approach means that model updates have very predictable performance. For any given operation, we update a small chunk of essential state, along with any indexes, and both of these operations can be made O(log(n)) or better in the size of the essential state of the model. Importantly, these update operations do not synchronously fire event handlers that interact with the DOM, eliminating the risk of a subscriber unexpectedly adding to the cost of a particular update operation. Code that responds to changes is decoupled from code that makes changes, meaning that once we make our operations fast, they will stay that way.

The cost of updating the view is also easier to reason about in a batched model. We can write a computation that performs predictably, regardless of how many times the model has been updated since the last animation frame. Redundant DOM updates are avoided, and we can reason about the state of the view holistically and declaratively rather than concerning ourselves with the interactions of lots of individual updates.

Making all model events asynchronous

So far, we've really only applied this approach to the view layer, but it makes sense at the model layer as well. Currently, we expose synchronous event hooks all over the system. For example, we emit events when individual text changes are made to the buffer, when individual cursors move, and for each individual change of the active pane. These synchronous events make the cost of model changes open-ended and encourage redundant computation in layers of derived state. For example, commenting out 1000 lines currently causes 1000 individual text changes, each of which is processed through the tokenized buffer and display buffer as a separate event, and that's not even accounting for the cost of third-party event handlers.

The solution is to make event propagation at the model layer asynchronous and batch-oriented in the same way we've done at the view layer. Instead of events synchronously reporting on individual changes such as Marker.prototype.onDidUpdate and Marker.prototype.onDidDestroy, events should instead asynchronously describe groups of changes that have occurred within a given frame of time, such as a hypothetical MarkerLayer.prototype.onUpdate that emitted events with a list of created, updated, and destroyed markers.

To achieve and maintain performance in Atom, we need to systematically replace all synchronous event subscription points in the system, starting with the hottest code paths. Highest on my list are Atom's synchronous handling of individual marker events, which degrades the scalability of find and replace, along with our synchronous handling of buffer changes, which causes bad performance in the face of large numbers of text changes.

Scheduling model change events

Going asynchronous means we can deliver change events at any time, so when should we do so? Updating the view is the ultimate goal of most model changes, meaning that the frequency with which we emit change events should be bounded by 60 frames per second, or roughly 16 milliseconds. However, since model-level change events often themselves result in additional model changes, emitting events at a higher frequency will be necessary to allow chained model updates to quiesce before the end of the frame budget. Simply emitting events on nextTick may be sufficient.

Transitioning to asynchronous model change events

A ton of code has been written against Atom's synchronous model change subscription methods, so dropping them any time soon is pretty much out of the question. New asynchronous APIs will need to be added alongside them, and after we transition bundled packages over we can deprecate the old synchronous versions.

A good way to slot these methods in may be with a shift in our naming. Previously, we adopted the onDid* and onWill* nomenclature to distinguish events occurring before or after a specific moment in time. Since asynchronous events always occur after, it may make sense to just drop the "did" from the name, adopting a scheme like TextBuffer.prototype.onChange or TextBuffer.prototype.onUpdateMarkers. There will be inconsistency during the transition, but eventually only a few onWill* special methods that actually allow synchronous cancellation will remain, alongside a host of on* methods.

Activity

  1. lock commented on Mar 30, 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!

  2. locked and limited conversation to collaborators on Mar 30, 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