From d13b75794f66efb7226da04c62ea8254a65168e2 Mon Sep 17 00:00:00 2001 From: David Aurelio Date: Wed, 2 Mar 2016 07:31:11 -0800 Subject: [PATCH] limit number of workers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary:This limits the number of spawned transformation workers depending on the number of available cores (`n`): ``` n < 3 → n n < 8 → floor(3/4 * n) n < 24 → floor(3/8 * n + 3) // factor going from 3/4 to 1/2 n >= 24 → floor(n / 2) ``` Reviewed By: bestander Differential Revision: D2999894 fb-gh-sync-id: 0163240b5f066432f9ba07161c0dfa2ec767ba58 shipit-source-id: 0163240b5f066432f9ba07161c0dfa2ec767ba58 --- react-packager/src/JSTransformer/index.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/react-packager/src/JSTransformer/index.js b/react-packager/src/JSTransformer/index.js index 97679859..a25cd57d 100644 --- a/react-packager/src/JSTransformer/index.js +++ b/react-packager/src/JSTransformer/index.js @@ -12,6 +12,7 @@ const ModuleTransport = require('../lib/ModuleTransport'); const Promise = require('promise'); const declareOpts = require('../lib/declareOpts'); const fs = require('fs'); +const os = require('os'); const temp = require('temp'); const util = require('util'); const workerFarm = require('worker-farm'); @@ -60,6 +61,23 @@ const validateOpts = declareOpts({ }, }); +const maxConcurrentWorkers = ((cores, override) => { + if (override) { + return Math.min(cores, override); + } + + if (cores < 3) { + return cores; + } + if (cores < 8) { + return Math.floor(cores * 0.75); + } + if (cores < 24) { + return Math.floor(3/8 * cores + 3); // between cores *.75 and cores / 2 + } + return cores / 2; +})(os.cpus().length, process.env.REACT_NATIVE_MAX_WORKERS); + class Transformer { constructor(options) { const opts = this._opts = validateOpts(options); @@ -87,6 +105,7 @@ class Transformer { this._workers = workerFarm({ autoStart: true, maxConcurrentCallsPerWorker: 1, + maxConcurrentWorkers: maxConcurrentWorkers, maxCallsPerWorker: MAX_CALLS_PER_WORKER, maxCallTime: opts.transformTimeoutInterval, maxRetries: MAX_RETRIES,