From c4ab468f65726ce60f971cacfc27c55fd5db0864 Mon Sep 17 00:00:00 2001 From: DeMoorJasper Date: Thu, 16 Aug 2018 17:30:24 -0700 Subject: [PATCH 1/2] split of cpuCount into a seperate module --- src/Parser.js | 2 +- src/utils/cpuCount.js | 11 +++++++++++ src/workerfarm/WorkerFarm.js | 18 +++++------------- 3 files changed, 17 insertions(+), 14 deletions(-) create mode 100644 src/utils/cpuCount.js diff --git a/src/Parser.js b/src/Parser.js index 013aecf600b..926e91bfb97 100644 --- a/src/Parser.js +++ b/src/Parser.js @@ -83,4 +83,4 @@ class Parser { } } -module.exports = Parser; \ No newline at end of file +module.exports = Parser; diff --git a/src/utils/cpuCount.js b/src/utils/cpuCount.js new file mode 100644 index 00000000000..d0d5613075a --- /dev/null +++ b/src/utils/cpuCount.js @@ -0,0 +1,11 @@ +const os = require('os'); + +module.exports = function() { + let cores; + try { + cores = require('physical-cpu-count'); + } catch (err) { + cores = os.cpus().length; + } + return cores || 1; +}; diff --git a/src/workerfarm/WorkerFarm.js b/src/workerfarm/WorkerFarm.js index 316c9942450..ca6d3651c20 100644 --- a/src/workerfarm/WorkerFarm.js +++ b/src/workerfarm/WorkerFarm.js @@ -1,7 +1,7 @@ const {EventEmitter} = require('events'); -const os = require('os'); const errorUtils = require('./errorUtils'); const Worker = require('./Worker'); +const cpuCount = require('../utils/cpuCount'); let shared = null; class WorkerFarm extends EventEmitter { @@ -258,17 +258,9 @@ class WorkerFarm extends EventEmitter { } static getNumWorkers() { - if (process.env.PARCEL_WORKERS) { - return parseInt(process.env.PARCEL_WORKERS, 10); - } - - let cores; - try { - cores = require('physical-cpu-count'); - } catch (err) { - cores = os.cpus().length; - } - return cores || 1; + return process.env.PARCEL_WORKERS + ? parseInt(process.env.PARCEL_WORKERS, 10) + : cpuCount(); } static callMaster(request, awaitResponse = true) { @@ -285,7 +277,7 @@ class WorkerFarm extends EventEmitter { } static getConcurrentCallsPerWorker() { - return parseInt(process.env.PARCEL_MAX_CONCURRENT_CALLS) || 5; + return parseInt(process.env.PARCEL_MAX_CONCURRENT_CALLS, 10) || 5; } } From 7484ab902751050e829d7c659df4ccab5652ed0f Mon Sep 17 00:00:00 2001 From: DeMoorJasper Date: Tue, 21 Aug 2018 16:28:54 -0700 Subject: [PATCH 2/2] remove useless bindings --- src/workerfarm/Worker.js | 2 +- src/workerfarm/WorkerFarm.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/workerfarm/Worker.js b/src/workerfarm/Worker.js index 8c9524984ae..e51cffc1460 100644 --- a/src/workerfarm/Worker.js +++ b/src/workerfarm/Worker.js @@ -41,7 +41,7 @@ class Worker extends EventEmitter { this.child = childProcess.fork(childModule, process.argv, options); - this.child.on('message', this.receive.bind(this)); + this.child.on('message', data => this.receive(data)); this.child.once('exit', code => { this.exitCode = code; diff --git a/src/workerfarm/WorkerFarm.js b/src/workerfarm/WorkerFarm.js index ca6d3651c20..f9e82e20b4d 100644 --- a/src/workerfarm/WorkerFarm.js +++ b/src/workerfarm/WorkerFarm.js @@ -60,7 +60,7 @@ class WorkerFarm extends EventEmitter { } mkhandle(method) { - return function(...args) { + return (...args) => { // Child process workers are slow to start (~600ms). // While we're waiting, just run on the main thread. // This significantly speeds up startup time. @@ -73,7 +73,7 @@ class WorkerFarm extends EventEmitter { return this.localWorker[method](...args, false); } - }.bind(this); + }; } onError(error, worker) {