Skip to content

Efficiently apply style changes - #1348

Closed
scothis wants to merge 5 commits into
masterfrom
style-diff
Closed

scothis wants to merge 5 commits into
masterfrom
style-diff

Conversation

@scothis

@scothis scothis commented Jun 29, 2015

Copy link
Copy Markdown
Contributor

This PR contains five discreet commits:

  • Throw on style mutation before load
  • Track the stylesheet as a pure JSON object
  • Batch mode for efficiently mutating Style instances
  • Utility to diff stylesheets
  • Use styleDiff to apply setStyle changes incrementally

Issue: #1341

scothis added 5 commits June 29, 2015 10:08
A user should never attempt to mutate the style before it has loaded.
Throw an error when a mutation is attempted.
Adds `style.json()` which returns the stylesheet as a pure JSON object
that is equivalent to the stylesheet necessary to recreate the style
object.

Mutations to the style object are reflected by subsequent calls to
`.json()`

Issue: #1341
Mutation operations on a Style instance are designed to be atomic.
Depending on the operation, there is a significant amount of work that
must be preformed to flush and recreate state. This work is wasted when
multiple mutation methods are invokes sequentially.

Batching these mutations allows deferring this work until the batch is
complete and deduping the work.

    style.batch(function() {
        style.addLayer(layer1);
        style.addLayer(layer2);
        ...
        style.addLayer(layerN);
    });

Internally, calls to four methods are deferred:
- style._groupLayers (will only fire once)
- style._broadcastLayers (will only fire once)
- style._reloadSource (will fire once per source)
- style.fire (each call will fire)

A casual benchmark shows the time to add 80 layers dropping from ~800 ms
to ~20 ms when run as a batch.

Issue: #1341
Two plain JS stylesheets are accepted as inputs, the output is an array
of operations necessary to bring the first stylesheet to be equivalent to
the second.

    var operations = styleDiff.diff(oldStylesheet, stylesheet);

Operations are a command and args. A command is the name of the method on
the style to invoke with the args. Operations are JSON serializable.

The diff for a stylesheet can be apply to a style object via patch. Patch
will throw if it is unable to fully apply the diff.

    styleDiff.patch(map.style, operations);

Issue: #1341
Previously, calls to map.setStyle() created a new Style instance.
Creating a new Style instance is an expensive operation that abandons the
current layers and sources. For simple updates, this is very wasteful.

Instead of creating a Style object for every call of map.setStyle() we
now diff the new style with the current style and apply the delta within
a batch update.

Situations where the non-incremental setStyle is used:
- initial style for a map
- the new stylesheet is undefined
- the new stylesheet is already a Style instance
- the current style is not done loading
- the style diff cannot be applied incrementally

Issue: #1341
@jfirebaugh

Copy link
Copy Markdown
Contributor

I really like this API:

    style.batch(function() {
        style.addLayer(layer1);
        style.addLayer(layer2);
        ...
        style.addLayer(layerN);
    });

The parts I'm concerned about with this PR as it stands are twofold:

  • We should reduce the complexity in Style by inverting the relationship between style.batch and the non-batched mutation methods. That is, make the non-batched mutation methods simple wrappers around style.batch. And instead of temporarily overwriting selected style methods within style.batch, pass a helper object to the work function:

      style.batch(function(batch) {
          batch.addLayer(layer1);
          batch.addLayer(layer2);
          ...
          batch.addLayer(layerN);
      });
    

    And then:

    Style.prototype.addLayer = function(layer) {
        this.batch(function(batch) {
            batch.addLayer(layer);
        });
    };
    
    // etc for removeLayer, ...
    
  • A diffing setStyle requires two representations of the stylesheet to be kept in sync: the pure JSON representation and the internal representation. This adds complexity, and moreover, whenever you try to keep keep two representations in sync, it's virtually inevitable that you will miss some case and they will end up not in sync. We need to find a way to have a single representation.

    • Eliminate the internal representation: calculate everything on the fly from the pure JSON representation. Probably not feasible.
    • Eliminate the pure JSON representation, and introduce the constraint that you can use either the built-in style mutation APIs (including style.batch), or diffing setStyle, but not both. This is obviously a pretty big API gotcha. To make it more palatable, we could consider moving style-diff to an external module, have the default setStyle be non-diffing, and present style.batch as the only built-in method for making batched changes.

@scothis

scothis commented Jun 29, 2015

Copy link
Copy Markdown
Contributor Author

I agree around keeping two models in sync. I was worried the rich objects would introduce noise into the diff. I'll play around with generating a plain object model from the rich objects to see how feasible it is.

Inverting the mutation methods to leverage the batch is interesting. It would end up being more code, but should be cleaner. I'll play with it.

@scothis

scothis commented Jun 30, 2015

Copy link
Copy Markdown
Contributor Author

Exporting the json stylesheet from a rich object is mostly possible, however, it will create significant noise in the diff. Layout and paint layer properties are particularly tricky as they don't distinguish between explicit, default and constant values.

It's worth cleaning up, but will take a good chunk of effort. Should creating a unified model block this work?

@jfirebaugh

Copy link
Copy Markdown
Contributor

Let's take the "throw on style mutation before load" and style.batch changes but not do diffing setStyle just yet.

@jfirebaugh

Copy link
Copy Markdown
Contributor

Closing -- let's revisit diffing setStyle once the constants situation in v8 resolves. That may make it easier to generate JSON from internal objects.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants