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

Add maxWorkers option #2655

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion debug/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@
zoom: 12.5,
center: [-77.01866, 38.888],
style: 'mapbox://styles/mapbox/streets-v8',
hash: true
hash: true,
workerCount: 3
});

map.addControl(new mapboxgl.Navigation());
Expand Down
4 changes: 2 additions & 2 deletions js/style/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ var StyleFunction = require('./style_function');

module.exports = Style;

function Style(stylesheet, animationLoop) {
function Style(stylesheet, animationLoop, workerCount) {
this.animationLoop = animationLoop || new AnimationLoop();
this.dispatcher = new Dispatcher(Math.max(browser.hardwareConcurrency - 1, 1), this);
this.dispatcher = new Dispatcher(workerCount || 1, this);
this.spriteAtlas = new SpriteAtlas(512, 512);
this.lineAtlas = new LineAtlas(256, 512);

Expand Down
12 changes: 10 additions & 2 deletions js/ui/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ var defaultOptions = {
failIfMajorPerformanceCaveat: false,
preserveDrawingBuffer: false,

trackResize: true
trackResize: true,
workerCount: Math.max(browser.hardwareConcurrency - 1, 1)
};

/**
Expand Down Expand Up @@ -97,6 +98,7 @@ var defaultOptions = {
* @param {number} [options.zoom] The zoom level of the map's initial viewport.
* @param {number} [options.bearing] The bearing (rotation) of the map's initial viewport measured in degrees counter-clockwise from north.
* @param {number} [options.pitch] The pitch of the map's initial viewport measured in degrees.
* @param {number} [options.workerCount=navigator.hardwareConcurrency] The number of WebWorkers the map should use to process vector tile data.
* @example
* var map = new mapboxgl.Map({
* container: 'map',
Expand All @@ -109,10 +111,16 @@ var defaultOptions = {
var Map = module.exports = function(options) {

options = util.extend({}, defaultOptions, options);

if (options.workerCount < 1) {
throw new Error('workerCount must an integer greater than or equal to 1.');
}

this._interactive = options.interactive;
this._failIfMajorPerformanceCaveat = options.failIfMajorPerformanceCaveat;
this._preserveDrawingBuffer = options.preserveDrawingBuffer;
this._trackResize = options.trackResize;
this._workerCount = options.workerCount;

if (typeof options.container === 'string') {
this._container = document.getElementById(options.container);
Expand Down Expand Up @@ -586,7 +594,7 @@ util.extend(Map.prototype, /** @lends Map.prototype */{
} else if (style instanceof Style) {
this.style = style;
} else {
this.style = new Style(style, this.animationLoop);
this.style = new Style(style, this.animationLoop, this._workerCount);
}

this.style
Expand Down
5 changes: 2 additions & 3 deletions test/js/style/style.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ var Style = require('../../../js/style/style');
var VectorTileSource = require('../../../js/source/vector_tile_source');
var StyleLayer = require('../../../js/style/style_layer');
var util = require('../../../js/util/util');
var browser = require('../../../js/util/browser');

function createStyleJSON(properties) {
return util.extend({
Expand Down Expand Up @@ -1123,8 +1122,8 @@ test('Style#query*Features', function(t) {
});

test('Style creates correct number of workers', function(t) {
var style = new Style(createStyleJSON());
t.equal(style.dispatcher.actors.length, browser.hardwareConcurrency - 1);
var style = new Style(createStyleJSON(), null, 3);
t.equal(style.dispatcher.actors.length, 3);
t.ok(style);
t.end();
});
16 changes: 16 additions & 0 deletions test/js/ui/map.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var window = require('../../../js/util/browser').window;
var Map = require('../../../js/ui/map');
var Style = require('../../../js/style/style');
var LngLat = require('../../../js/geo/lng_lat');
var browser = require('../../../js/util/browser');
var sinon = require('sinon');

var fixed = require('../../testutil/fixed');
Expand Down Expand Up @@ -930,6 +931,21 @@ test('Map', function(t) {
t.end();
});

t.test('workerCount option', function(t) {
var map = createMap({});
// TODO: it's not great to check this private member here; better would
// be to mock Style and then just check that the correct workerCount
// param is being passed.
t.equal(map._workerCount, browser.hardwareConcurrency - 1, 'workerCount defaults to hardwareConcurrency - 1');
map = createMap({ workerCount: 3 });
t.equal(map._workerCount, 3, 'workerCount option is used');
t.throws(function () {
createMap({ workerCount: 0 });
});
t.end();
});


t.end();
});

Expand Down