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 workerCount option #2666

Merged
merged 2 commits into from
Jun 7, 2016
Merged
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
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 - 1] 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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a preference towards passing options.workerCount directly to Style. I'm trying to push Map in the direction of becoming a dumb facade class because it is currently a god object.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lucaswoj As long as Map has a setStyle method, I think it will need to keep a copy like this of the workerCount, because it needs to be able to do new Style(...) not just in the map's constructor. We could add workerCount as a parameter to setStyle, but that seems a little odd to me.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah! I didn't consider the setStyle case. I agree that this this._workerCount is necessary for now 👍

}

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();
});
13 changes: 13 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,18 @@ test('Map', function(t) {
t.end();
});

t.test('workerCount option', function(t) {
var map = createMap({ style: createStyle() });
t.equal(map.style.dispatcher.actors.length, browser.hardwareConcurrency - 1, 'workerCount defaults to hardwareConcurrency - 1');
map = createMap({ style: createStyle(), workerCount: 3 });
t.equal(map.style.dispatcher.actors.length, 3, 'workerCount option is used');
t.throws(function () {
createMap({ workerCount: 0 });
});
t.end();
});


t.end();
});

Expand Down