Skip to content

Commit

Permalink
Add maxWorkers option
Browse files Browse the repository at this point in the history
Also sets maxWorkers on the debug page to 3, on the theory that a pretty
large majority of users have 4 cpus.

Closes #2022
  • Loading branch information
Anand Thakker committed Jun 3, 2016
1 parent 09c0c96 commit c534c2e
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 8 deletions.
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,
maxWorkers: 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, maxWorkers) {
this.animationLoop = animationLoop || new AnimationLoop();
this.dispatcher = new Dispatcher(Math.max(browser.hardwareConcurrency - 1, 1), this);
this.dispatcher = new Dispatcher(maxWorkers || 1, this);
this.spriteAtlas = new SpriteAtlas(512, 512);
this.lineAtlas = new LineAtlas(256, 512);

Expand Down
11 changes: 9 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,
maxWorkers: Math.max(browser.hardwareConcurrency - 1, 1)
};

/**
Expand Down Expand Up @@ -109,10 +110,16 @@ var defaultOptions = {
var Map = module.exports = function(options) {

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

if (options.maxWorkers < 1) {
throw new Error('maxWorkers 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._maxWorkers = options.maxWorkers;

if (typeof options.container === 'string') {
this._container = document.getElementById(options.container);
Expand Down Expand Up @@ -586,7 +593,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._maxWorkers);
}

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('maxWorkers 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 maxWorkers
// param is being passed.
t.equal(map._maxWorkers, browser.hardwareConcurrency - 1, 'maxWorkers defaults to hardwareConcurrency - 1');
map = createMap({ maxWorkers: 3 });
t.equal(map._maxWorkers, 3, 'maxWorkers option is used');
t.throws(function () {
createMap({ maxWorkers: 0 });
});
t.end();
});


t.end();
});

Expand Down

0 comments on commit c534c2e

Please sign in to comment.