Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Batch mode for efficiently mutating Style instances #1352

Merged
merged 1 commit into from
Jun 30, 2015

Conversation

scothis
Copy link
Contributor

@scothis scothis commented Jun 30, 2015

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.

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

Methods on the batch object provided to the work function mirror methods
on Map and Style, and include:

  • addLayer
  • removeLayer
  • setPaintProperty
  • setLayoutProperty
  • setFilter
  • addSource
  • removeSource

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

@jfirebaugh
Copy link
Contributor

This looks great. Only adjustment I suggest is to ensure that a style change event fires only once per batch.

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.

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

Methods on the batch object provided to the work function mirror methods
on Map and Style, and include:
- addLayer
- removeLayer
- setPaintProperty
- setLayoutProperty
- setFilter
- addSource
- removeSource

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, 'change' will fire once)

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

Issue: #1341
@scothis
Copy link
Contributor Author

scothis commented Jun 30, 2015

@jfirebaugh the 'change' event is fired at most once per batch now

@jfirebaugh jfirebaugh merged commit 07d4f2c into master Jun 30, 2015
@jfirebaugh jfirebaugh deleted the batch-mutation branch June 30, 2015 17:35
@mourner
Copy link
Member

mourner commented Jul 10, 2015

A bit late to say that this is a really awesome PR.

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